From af17259375266210c13f6e6df4baaea6df74a624 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Tue, 29 Jul 2014 23:07:59 +0300 Subject: [PATCH] Convert ToSlice() to Values() --- README.md | 10 +++++----- structure.go | 4 ++-- structure_example_test.go | 12 ++++++------ structure_test.go | 8 ++++---- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e9af0a6..8b96b4a 100644 --- a/README.md +++ b/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) ``` diff --git a/structure.go b/structure.go index 92cbcbd..c73f980 100644 --- a/structure.go +++ b/structure.go @@ -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)) diff --git a/structure_example_test.go b/structure_example_test.go index ee1f3aa..763a33a 100644 --- a/structure_example_test.go +++ b/structure_example_test.go @@ -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() { diff --git a/structure_test.go b/structure_test.go index 085221f..bcbac06 100644 --- a/structure_test.go +++ b/structure_test.go @@ -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) } } }