Convert ToSlice() to Values()
This commit is contained in:
parent
32b1a23aeb
commit
af17259375
10
README.md
10
README.md
@ -33,19 +33,19 @@ Convert a struct to a `map[string]interface{}`
|
||||
```go
|
||||
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)
|
||||
```
|
||||
|
||||
#### ToSlice()
|
||||
#### Values()
|
||||
|
||||
Convert the values of a struct to a `[]interface{}`. Slice values are
|
||||
**sorted** by default according to the field names.
|
||||
|
||||
```go
|
||||
m := structure.ToSlice(s)
|
||||
m := structure.Values(s)
|
||||
|
||||
// prints: []interface {}{true, 123456, "Arslan"}
|
||||
// prints: [true, 123456, "Arslan"]
|
||||
fmt.Printf("%#v", m)
|
||||
```
|
||||
|
||||
@ -57,7 +57,7 @@ default according to the field names.
|
||||
```go
|
||||
m := structure.Fields(s)
|
||||
|
||||
// prints: []string{"Enabled", "ID", "Name"}
|
||||
// prints: ["Enabled", "ID", "Name"]
|
||||
fmt.Printf("%#v", m)
|
||||
```
|
||||
|
||||
|
||||
@ -63,7 +63,7 @@ func Map(s interface{}) map[string]interface{} {
|
||||
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
|
||||
// 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
|
||||
// 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)
|
||||
|
||||
keys := make([]string, len(m))
|
||||
|
||||
@ -56,7 +56,7 @@ func ExampleMap_tags() {
|
||||
|
||||
}
|
||||
|
||||
func ExampleToSlice() {
|
||||
func ExampleValues() {
|
||||
type Server struct {
|
||||
Name string
|
||||
ID int32
|
||||
@ -69,12 +69,12 @@ func ExampleToSlice() {
|
||||
Enabled: false,
|
||||
}
|
||||
|
||||
m := ToSlice(s)
|
||||
m := Values(s)
|
||||
|
||||
// note that the output is sorted according to the field names
|
||||
fmt.Printf("%#v\n", m)
|
||||
fmt.Printf("Values: %+v\n", m)
|
||||
// Output:
|
||||
// []interface {}{false, 135790, "Fatih"}
|
||||
// Values: [false 135790 Fatih]
|
||||
}
|
||||
|
||||
func ExampleFields() {
|
||||
@ -93,9 +93,9 @@ func ExampleFields() {
|
||||
m := Fields(s)
|
||||
|
||||
// note that the output is sorted according to the field names
|
||||
fmt.Printf("%#v\n", m)
|
||||
fmt.Printf("Fields: %+v\n", m)
|
||||
// Output:
|
||||
// []string{"LastAccessed", "Name", "Number"}
|
||||
// Fields: [LastAccessed Name Number]
|
||||
}
|
||||
|
||||
func ExampleIsValid() {
|
||||
|
||||
@ -102,7 +102,7 @@ func TestStruct(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestToSlice(t *testing.T) {
|
||||
func TestValues(t *testing.T) {
|
||||
var T = struct {
|
||||
A string
|
||||
B int
|
||||
@ -113,10 +113,10 @@ func TestToSlice(t *testing.T) {
|
||||
C: true,
|
||||
}
|
||||
|
||||
s := ToSlice(T)
|
||||
s := Values(T)
|
||||
|
||||
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 {
|
||||
@ -130,7 +130,7 @@ func TestToSlice(t *testing.T) {
|
||||
|
||||
for _, val := range []interface{}{"a-value", 2, true} {
|
||||
if !inSlice(val) {
|
||||
t.Errorf("ToSlice should have the value %v", val)
|
||||
t.Errorf("Values should have the value %v", val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user