This repository has been archived by the owner on Sep 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
examples_test.go
135 lines (127 loc) · 4.19 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package reflector_test
import (
. "."
"fmt"
)
func ExampleStructToMap() {
type T struct {
Uint8 uint8
Float32 float32 `json:"f32"` // tag will be used
String string
Pstring *string // pointer will be followed
foo int // not exported
}
pstr := "pstr"
s := T{8, 3.14, "str", &pstr, 13}
m := make(map[string]interface{})
StructToMap(&s, m, "json")
fmt.Printf("%#v %#v %#v %#v %#v\n", m["Uint8"], m["f32"], m["String"], m["Pstring"], m["foo"])
// Output:
// 0x8 3.14 "str" "pstr" <nil>
}
func ExampleStructValueToMap() {
type T struct {
Uint8 uint8
Float32 float32 `json:"f32"` // tag will be used
String string
Pstring *string // nil will be present in map
foo int // not exported
}
s := T{8, 3.14, "str", nil, 13}
m := make(map[string]interface{})
StructValueToMap(s, m, "json")
fmt.Printf("%#v %#v %#v %#v %#v\n", m["Uint8"], m["f32"], m["String"], m["Pstring"], m["foo"])
_, ok := m["Pstring"]
fmt.Println(ok)
// Output:
// 0x8 3.14 "str" <nil> <nil>
// true
}
func ExampleStructsToMaps() {
type T struct {
Uint8 uint8
Float32 float32 `json:"f32"` // tag will be used
String string
Pstring *string // pointer will be followed
foo int // not exported
}
pstr := "pstr"
s := []T{{8, 3.14, "str", &pstr, 13}}
var m []map[string]interface{}
StructsToMaps(s, &m, "json")
fmt.Printf("%#v %#v %#v %#v %#v\n", m[0]["Uint8"], m[0]["f32"], m[0]["String"], m[0]["Pstring"], m[0]["foo"])
// Output:
// 0x8 3.14 "str" "pstr" <nil>
}
func ExampleMapToStruct_noConvert() {
type T struct {
Uint8 uint8 // no automatic type conversion
Float32 float32 `json:"f32"` // tag will be used
String string // not present in map, will not be set
Pstring *string // value will be deep-copied
foo int // not exported, will not be set
}
var s T
m := map[string]interface{}{"Uint8": uint8(8), "f32": float32(3.14), "Pstring": "pstr", "foo": 13}
MapToStruct(m, &s, NoConvert, "json")
m["Pstring"] = "foo"
fmt.Printf("%#v %#v %#v %#v %#v\n", s.Uint8, s.Float32, s.String, *s.Pstring, s.foo)
// Output:
// 0x8 3.14 "" "pstr" 0
}
func ExampleMapToStruct_strconv() {
type T struct {
Uint8 uint8 // type conversion via strconv
Float32 float32 `json:"f32"` // tag will be used
String string // not present in map, will not be set
Pstring *string // value will be deep-copied
foo int // not exported, will not be set
}
var s T
m := map[string]interface{}{"Uint8": 8, "f32": 3, "Pstring": "pstr", "foo": 13}
MapToStruct(m, &s, Strconv, "json")
m["Pstring"] = "foo"
fmt.Printf("%#v %#v %#v %#v %#v\n", s.Uint8, s.Float32, s.String, *s.Pstring, s.foo)
// Output:
// 0x8 3 "" "pstr" 0
}
func ExampleMapsToStructs_noConvert() {
type T struct {
Uint8 uint8 // no automatic type conversion
Float32 float32 `json:"f32"` // tag will be used
String string // not present in first map, will not be set
Pstring *string // value will be deep-copied
foo int // not exported, will not be set
}
var s []T
maps := []map[string]interface{}{
{"Uint8": uint8(8), "Pstring": "pstr"},
{"f32": float32(3.14), "String": "str", "foo": 13},
}
MapsToStructs(maps, &s, NoConvert, "json")
fmt.Printf("%#v %#v %#v %#v %#v\n", s[0].Uint8, s[0].Float32, s[0].String, *s[0].Pstring, s[0].foo)
fmt.Printf("%#v %#v %#v %#v %#v\n", s[1].Uint8, s[1].Float32, s[1].String, s[1].Pstring, s[1].foo)
// Output:
// 0x8 0 "" "pstr" 0
// 0x0 3.14 "str" (*string)(nil) 0
}
func ExampleMapsToStructs_strconv() {
type T struct {
Uint8 uint8 // type conversion via strconv
Float32 float32 `json:"f32"` // tag will be used
String string // not present in first map, will not be set
Pstring *string // value will be deep-copied
foo int // not exported, will not be set
}
var s []T
maps := []map[string]interface{}{
{"Uint8": 8, "f32": 3, "foo": 13, "Pstring": "pstr"},
{"Uint8": "9", "f32": "4", "String": "43", "foo": "13"},
}
MapsToStructs(maps, &s, Strconv, "json")
fmt.Printf("%#v %#v %#v %#v %#v\n", s[0].Uint8, s[0].Float32, s[0].String, *s[0].Pstring, s[0].foo)
fmt.Printf("%#v %#v %#v %#v %#v\n", s[1].Uint8, s[1].Float32, s[1].String, s[1].Pstring, s[1].foo)
// Output:
// 0x8 3 "" "pstr" 0
// 0x9 4 "43" (*string)(nil) 0
}