structure: convert ToMap() to Map()
This commit is contained in:
parent
b7140a415d
commit
32b1a23aeb
@ -26,12 +26,12 @@ s := &Server{
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### ToMap()
|
#### Map()
|
||||||
|
|
||||||
Convert a struct to a `map[string]interface{}`
|
Convert a struct to a `map[string]interface{}`
|
||||||
|
|
||||||
```go
|
```go
|
||||||
m := structure.ToMap(s)
|
m := structure.Map(s)
|
||||||
|
|
||||||
// prints: map[string]interface {}{"Name":"Arslan", "ID":123456, "Enabled":true}
|
// prints: map[string]interface {}{"Name":"Arslan", "ID":123456, "Enabled":true}
|
||||||
fmt.Printf("%#v", m)
|
fmt.Printf("%#v", m)
|
||||||
|
|||||||
10
structure.go
10
structure.go
@ -6,8 +6,8 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ToMap converts the given s struct to a map[string]interface{}, where the
|
// Map converts the given s struct to a map[string]interface{}, where the keys
|
||||||
// keys of the map are the field names and the values of the map the associated
|
// 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
|
// 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
|
// 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:
|
// 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
|
// 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 ToMap(s interface{}) map[string]interface{} {
|
func Map(s interface{}) map[string]interface{} {
|
||||||
out := make(map[string]interface{})
|
out := make(map[string]interface{})
|
||||||
|
|
||||||
v := reflect.ValueOf(s)
|
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
|
// 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 ToSlice(s interface{}) []interface{} {
|
||||||
m := ToMap(s)
|
m := Map(s)
|
||||||
|
|
||||||
keys := make([]string, len(m))
|
keys := make([]string, len(m))
|
||||||
count := 0
|
count := 0
|
||||||
@ -152,7 +152,7 @@ func IsValid(s interface{}) bool {
|
|||||||
// 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.
|
// fields will be neglected.
|
||||||
func Fields(s interface{}) []string {
|
func Fields(s interface{}) []string {
|
||||||
m := ToMap(s)
|
m := Map(s)
|
||||||
|
|
||||||
keys := make([]string, len(m))
|
keys := make([]string, len(m))
|
||||||
count := 0
|
count := 0
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleToMap() {
|
func ExampleMap() {
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Name string
|
Name string
|
||||||
ID int32
|
ID int32
|
||||||
@ -18,7 +18,7 @@ func ExampleToMap() {
|
|||||||
Enabled: true,
|
Enabled: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
m := ToMap(s)
|
m := Map(s)
|
||||||
|
|
||||||
fmt.Printf("%#v\n", m["Name"])
|
fmt.Printf("%#v\n", m["Name"])
|
||||||
fmt.Printf("%#v\n", m["ID"])
|
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
|
// Custom tags can change the map keys instead of using the fields name
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Name string `structure:"server_name"`
|
Name string `structure:"server_name"`
|
||||||
@ -43,7 +43,7 @@ func ExampleToMap_tags() {
|
|||||||
ID: 789012,
|
ID: 789012,
|
||||||
}
|
}
|
||||||
|
|
||||||
m := ToMap(s)
|
m := Map(s)
|
||||||
|
|
||||||
// access them by the custom tags defined above
|
// access them by the custom tags defined above
|
||||||
fmt.Printf("%#v\n", m["server_name"])
|
fmt.Printf("%#v\n", m["server_name"])
|
||||||
|
|||||||
@ -5,21 +5,21 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestToMapNonStruct(t *testing.T) {
|
func TestMapNonStruct(t *testing.T) {
|
||||||
foo := []string{"foo"}
|
foo := []string{"foo"}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
err := recover()
|
err := recover()
|
||||||
if err == nil {
|
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
|
// 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 {
|
var T = struct {
|
||||||
A string
|
A string
|
||||||
B int
|
B int
|
||||||
@ -30,15 +30,15 @@ func TestToMap(t *testing.T) {
|
|||||||
C: true,
|
C: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
a := ToMap(T)
|
a := Map(T)
|
||||||
|
|
||||||
if typ := reflect.TypeOf(a).Kind(); typ != reflect.Map {
|
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
|
// we have three fields
|
||||||
if len(a) != 3 {
|
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 {
|
inMap := func(val interface{}) bool {
|
||||||
@ -53,13 +53,13 @@ func TestToMap(t *testing.T) {
|
|||||||
|
|
||||||
for _, val := range []interface{}{"a-value", 2, true} {
|
for _, val := range []interface{}{"a-value", 2, true} {
|
||||||
if !inMap(val) {
|
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 {
|
var T = struct {
|
||||||
A string `structure:"x"`
|
A string `structure:"x"`
|
||||||
B int `structure:"y"`
|
B int `structure:"y"`
|
||||||
@ -70,7 +70,7 @@ func TestToMap_Tag(t *testing.T) {
|
|||||||
C: true,
|
C: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
a := ToMap(T)
|
a := Map(T)
|
||||||
|
|
||||||
inMap := func(key interface{}) bool {
|
inMap := func(key interface{}) bool {
|
||||||
for k := range a {
|
for k := range a {
|
||||||
@ -83,7 +83,7 @@ func TestToMap_Tag(t *testing.T) {
|
|||||||
|
|
||||||
for _, key := range []string{"x", "y", "z"} {
|
for _, key := range []string{"x", "y", "z"} {
|
||||||
if !inMap(key) {
|
if !inMap(key) {
|
||||||
t.Errorf("ToMap should have the key %v", key)
|
t.Errorf("Map should have the key %v", key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user