From 32b1a23aeba510ba36370c27c86cf3236f806f5a Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Tue, 29 Jul 2014 23:01:00 +0300 Subject: [PATCH] structure: convert ToMap() to Map() --- README.md | 4 ++-- structure.go | 10 +++++----- structure_example_test.go | 8 ++++---- structure_test.go | 22 +++++++++++----------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 4f2b513..e9af0a6 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,12 @@ s := &Server{ } ``` -#### ToMap() +#### Map() Convert a struct to a `map[string]interface{}` ```go -m := structure.ToMap(s) +m := structure.Map(s) // prints: map[string]interface {}{"Name":"Arslan", "ID":123456, "Enabled":true} fmt.Printf("%#v", m) diff --git a/structure.go b/structure.go index f01baa4..92cbcbd 100644 --- a/structure.go +++ b/structure.go @@ -6,8 +6,8 @@ import ( "sort" ) -// ToMap converts the given s struct to a map[string]interface{}, where the -// keys of the map are the field names and the values of the map the associated +// Map converts the given s struct to a map[string]interface{}, where the keys +// of the map are the field names and the values of the map the associated // values of the fields. The default key string is the struct field name but // can be changed in the struct field's tag value. The "structure" key in the // struct's field tag value is the key name. Example: @@ -22,7 +22,7 @@ import ( // // 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 ToMap(s interface{}) map[string]interface{} { +func Map(s interface{}) map[string]interface{} { out := make(map[string]interface{}) v := reflect.ValueOf(s) @@ -73,7 +73,7 @@ func ToMap(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{} { - m := ToMap(s) + m := Map(s) keys := make([]string, len(m)) count := 0 @@ -152,7 +152,7 @@ func IsValid(s interface{}) bool { // Note that only exported fields of a struct can be accessed, non exported // fields will be neglected. func Fields(s interface{}) []string { - m := ToMap(s) + m := Map(s) keys := make([]string, len(m)) count := 0 diff --git a/structure_example_test.go b/structure_example_test.go index 393e9e2..ee1f3aa 100644 --- a/structure_example_test.go +++ b/structure_example_test.go @@ -5,7 +5,7 @@ import ( "time" ) -func ExampleToMap() { +func ExampleMap() { type Server struct { Name string ID int32 @@ -18,7 +18,7 @@ func ExampleToMap() { Enabled: true, } - m := ToMap(s) + m := Map(s) fmt.Printf("%#v\n", m["Name"]) fmt.Printf("%#v\n", m["ID"]) @@ -30,7 +30,7 @@ func ExampleToMap() { } -func ExampleToMap_tags() { +func ExampleMap_tags() { // Custom tags can change the map keys instead of using the fields name type Server struct { Name string `structure:"server_name"` @@ -43,7 +43,7 @@ func ExampleToMap_tags() { ID: 789012, } - m := ToMap(s) + m := Map(s) // access them by the custom tags defined above fmt.Printf("%#v\n", m["server_name"]) diff --git a/structure_test.go b/structure_test.go index 2f3887a..085221f 100644 --- a/structure_test.go +++ b/structure_test.go @@ -5,21 +5,21 @@ import ( "testing" ) -func TestToMapNonStruct(t *testing.T) { +func TestMapNonStruct(t *testing.T) { foo := []string{"foo"} defer func() { err := recover() if err == nil { - t.Error("Passing a non struct into ToMap should panic") + t.Error("Passing a non struct into Map should panic") } }() // this should panic. We are going to recover and and test it - _ = ToMap(foo) + _ = Map(foo) } -func TestToMap(t *testing.T) { +func TestMap(t *testing.T) { var T = struct { A string B int @@ -30,15 +30,15 @@ func TestToMap(t *testing.T) { C: true, } - a := ToMap(T) + a := Map(T) if typ := reflect.TypeOf(a).Kind(); typ != reflect.Map { - t.Errorf("ToMap should return a map type, got: %v", typ) + t.Errorf("Map should return a map type, got: %v", typ) } // we have three fields if len(a) != 3 { - t.Errorf("ToMap should return a map of len 3, got: %d", len(a)) + t.Errorf("Map should return a map of len 3, got: %d", len(a)) } inMap := func(val interface{}) bool { @@ -53,13 +53,13 @@ func TestToMap(t *testing.T) { for _, val := range []interface{}{"a-value", 2, true} { if !inMap(val) { - t.Errorf("ToMap should have the value %v", val) + t.Errorf("Map should have the value %v", val) } } } -func TestToMap_Tag(t *testing.T) { +func TestMap_Tag(t *testing.T) { var T = struct { A string `structure:"x"` B int `structure:"y"` @@ -70,7 +70,7 @@ func TestToMap_Tag(t *testing.T) { C: true, } - a := ToMap(T) + a := Map(T) inMap := func(key interface{}) bool { for k := range a { @@ -83,7 +83,7 @@ func TestToMap_Tag(t *testing.T) { for _, key := range []string{"x", "y", "z"} { if !inMap(key) { - t.Errorf("ToMap should have the key %v", key) + t.Errorf("Map should have the key %v", key) } }