1 package mapstructure
2
3 import (
4 "fmt"
5 )
6
7 func ExampleDecode() {
8 type Person struct {
9 Name string
10 Age int
11 Emails []string
12 Extra map[string]string
13 }
14
15
16
17
18 input := map[string]interface{}{
19 "name": "Mitchell",
20 "age": 91,
21 "emails": []string{"one", "two", "three"},
22 "extra": map[string]string{
23 "twitter": "mitchellh",
24 },
25 }
26
27 var result Person
28 err := Decode(input, &result)
29 if err != nil {
30 panic(err)
31 }
32
33 fmt.Printf("%#v", result)
34
35
36 }
37
38 func ExampleDecode_errors() {
39 type Person struct {
40 Name string
41 Age int
42 Emails []string
43 Extra map[string]string
44 }
45
46
47
48
49 input := map[string]interface{}{
50 "name": 123,
51 "age": "bad value",
52 "emails": []int{1, 2, 3},
53 }
54
55 var result Person
56 err := Decode(input, &result)
57 if err == nil {
58 panic("should have an error")
59 }
60
61 fmt.Println(err.Error())
62
63
64
65
66
67
68
69
70 }
71
72 func ExampleDecode_metadata() {
73 type Person struct {
74 Name string
75 Age int
76 }
77
78
79
80
81 input := map[string]interface{}{
82 "name": "Mitchell",
83 "age": 91,
84 "email": "foo@bar.com",
85 }
86
87
88
89
90 var md Metadata
91 var result Person
92 config := &DecoderConfig{
93 Metadata: &md,
94 Result: &result,
95 }
96
97 decoder, err := NewDecoder(config)
98 if err != nil {
99 panic(err)
100 }
101
102 if err := decoder.Decode(input); err != nil {
103 panic(err)
104 }
105
106 fmt.Printf("Unused keys: %#v", md.Unused)
107
108
109 }
110
111 func ExampleDecode_weaklyTypedInput() {
112 type Person struct {
113 Name string
114 Age int
115 Emails []string
116 }
117
118
119
120
121 input := map[string]interface{}{
122 "name": 123,
123 "age": "42",
124 "emails": map[string]interface{}{},
125 }
126
127 var result Person
128 config := &DecoderConfig{
129 WeaklyTypedInput: true,
130 Result: &result,
131 }
132
133 decoder, err := NewDecoder(config)
134 if err != nil {
135 panic(err)
136 }
137
138 err = decoder.Decode(input)
139 if err != nil {
140 panic(err)
141 }
142
143 fmt.Printf("%#v", result)
144
145 }
146
147 func ExampleDecode_tags() {
148
149
150 type Person struct {
151 Name string `mapstructure:"person_name"`
152 Age int `mapstructure:"person_age"`
153 }
154
155 input := map[string]interface{}{
156 "person_name": "Mitchell",
157 "person_age": 91,
158 }
159
160 var result Person
161 err := Decode(input, &result)
162 if err != nil {
163 panic(err)
164 }
165
166 fmt.Printf("%#v", result)
167
168
169 }
170
171 func ExampleDecode_embeddedStruct() {
172
173
174
175
176 type Family struct {
177 LastName string
178 }
179 type Location struct {
180 City string
181 }
182 type Person struct {
183 Family `mapstructure:",squash"`
184 Location `mapstructure:",squash"`
185 FirstName string
186 }
187
188 input := map[string]interface{}{
189 "FirstName": "Mitchell",
190 "LastName": "Hashimoto",
191 "City": "San Francisco",
192 }
193
194 var result Person
195 err := Decode(input, &result)
196 if err != nil {
197 panic(err)
198 }
199
200 fmt.Printf("%s %s, %s", result.FirstName, result.LastName, result.City)
201
202
203 }
204
205 func ExampleDecode_remainingData() {
206
207
208 type Person struct {
209 Name string
210 Age int
211 Other map[string]interface{} `mapstructure:",remain"`
212 }
213
214 input := map[string]interface{}{
215 "name": "Mitchell",
216 "age": 91,
217 "email": "mitchell@example.com",
218 }
219
220 var result Person
221 err := Decode(input, &result)
222 if err != nil {
223 panic(err)
224 }
225
226 fmt.Printf("%#v", result)
227
228
229 }
230
231 func ExampleDecode_omitempty() {
232
233 type Family struct {
234 LastName string
235 }
236 type Location struct {
237 City string
238 }
239 type Person struct {
240 *Family `mapstructure:",omitempty"`
241 *Location `mapstructure:",omitempty"`
242 Age int
243 FirstName string
244 }
245
246 result := &map[string]interface{}{}
247 input := Person{FirstName: "Somebody"}
248 err := Decode(input, &result)
249 if err != nil {
250 panic(err)
251 }
252
253 fmt.Printf("%+v", result)
254
255
256 }
257
View as plain text