structure: convert ToMap() to Map()

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

View File

@ -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)

View File

@ -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

View File

@ -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"])

View File

@ -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)
}
}