Convert ToSlice() to Values()

This commit is contained in:
Fatih Arslan 2014-07-29 23:07:59 +03:00
parent 32b1a23aeb
commit af17259375
4 changed files with 17 additions and 17 deletions

View File

@ -33,19 +33,19 @@ Convert a struct to a `map[string]interface{}`
```go ```go
m := structure.Map(s) m := structure.Map(s)
// prints: map[string]interface {}{"Name":"Arslan", "ID":123456, "Enabled":true} // prints: {"Name":"Arslan", "ID":123456, "Enabled":true}
fmt.Printf("%#v", m) fmt.Printf("%#v", m)
``` ```
#### ToSlice() #### Values()
Convert the values of a struct to a `[]interface{}`. Slice values are Convert the values of a struct to a `[]interface{}`. Slice values are
**sorted** by default according to the field names. **sorted** by default according to the field names.
```go ```go
m := structure.ToSlice(s) m := structure.Values(s)
// prints: []interface {}{true, 123456, "Arslan"} // prints: [true, 123456, "Arslan"]
fmt.Printf("%#v", m) fmt.Printf("%#v", m)
``` ```
@ -57,7 +57,7 @@ default according to the field names.
```go ```go
m := structure.Fields(s) m := structure.Fields(s)
// prints: []string{"Enabled", "ID", "Name"} // prints: ["Enabled", "ID", "Name"]
fmt.Printf("%#v", m) fmt.Printf("%#v", m)
``` ```

View File

@ -63,7 +63,7 @@ func Map(s interface{}) map[string]interface{} {
return out return out
} }
// ToSlice converts the given s struct's field values to a []interface{}. // Values converts the given s struct's field values to a []interface{}.
// Values are inserted and sorted according to the field names. A struct tag // Values are inserted and sorted according to the field names. A struct tag
// with the content of "-" ignores the that particular field. Example: // with the content of "-" ignores the that particular field. Example:
// //
@ -72,7 +72,7 @@ func Map(s interface{}) map[string]interface{} {
// //
// Note that only exported fields of a struct can be accessed, non exported // Note that only exported fields of a struct can be accessed, non exported
// fields will be neglected. It panics if s's kind is not struct. // fields will be neglected. It panics if s's kind is not struct.
func ToSlice(s interface{}) []interface{} { func Values(s interface{}) []interface{} {
m := Map(s) m := Map(s)
keys := make([]string, len(m)) keys := make([]string, len(m))

View File

@ -56,7 +56,7 @@ func ExampleMap_tags() {
} }
func ExampleToSlice() { func ExampleValues() {
type Server struct { type Server struct {
Name string Name string
ID int32 ID int32
@ -69,12 +69,12 @@ func ExampleToSlice() {
Enabled: false, Enabled: false,
} }
m := ToSlice(s) m := Values(s)
// note that the output is sorted according to the field names // note that the output is sorted according to the field names
fmt.Printf("%#v\n", m) fmt.Printf("Values: %+v\n", m)
// Output: // Output:
// []interface {}{false, 135790, "Fatih"} // Values: [false 135790 Fatih]
} }
func ExampleFields() { func ExampleFields() {
@ -93,9 +93,9 @@ func ExampleFields() {
m := Fields(s) m := Fields(s)
// note that the output is sorted according to the field names // note that the output is sorted according to the field names
fmt.Printf("%#v\n", m) fmt.Printf("Fields: %+v\n", m)
// Output: // Output:
// []string{"LastAccessed", "Name", "Number"} // Fields: [LastAccessed Name Number]
} }
func ExampleIsValid() { func ExampleIsValid() {

View File

@ -102,7 +102,7 @@ func TestStruct(t *testing.T) {
} }
func TestToSlice(t *testing.T) { func TestValues(t *testing.T) {
var T = struct { var T = struct {
A string A string
B int B int
@ -113,10 +113,10 @@ func TestToSlice(t *testing.T) {
C: true, C: true,
} }
s := ToSlice(T) s := Values(T)
if typ := reflect.TypeOf(s).Kind(); typ != reflect.Slice { if typ := reflect.TypeOf(s).Kind(); typ != reflect.Slice {
t.Errorf("ToSlice should return a slice type, got: %v", typ) t.Errorf("Values should return a slice type, got: %v", typ)
} }
inSlice := func(val interface{}) bool { inSlice := func(val interface{}) bool {
@ -130,7 +130,7 @@ func TestToSlice(t *testing.T) {
for _, val := range []interface{}{"a-value", 2, true} { for _, val := range []interface{}{"a-value", 2, true} {
if !inSlice(val) { if !inSlice(val) {
t.Errorf("ToSlice should have the value %v", val) t.Errorf("Values should have the value %v", val)
} }
} }
} }