12 Extra map[string]string
15 // This input can come from anywhere, but typically comes from
16 // something like decoding JSON where we're not quite sure of the
18 input := map[string]interface{}{
21 "emails": []string{"one", "two", "three"},
22 "extra": map[string]string{
23 "twitter": "mitchellh",
28 err := Decode(input, &result)
33 fmt.Printf("%#v", result)
35 // mapstructure.Person{Name:"Mitchell", Age:91, Emails:[]string{"one", "two", "three"}, Extra:map[string]string{"twitter":"mitchellh"}}
38 func ExampleDecode_errors() {
43 Extra map[string]string
46 // This input can come from anywhere, but typically comes from
47 // something like decoding JSON where we're not quite sure of the
49 input := map[string]interface{}{
52 "emails": []int{1, 2, 3},
56 err := Decode(input, &result)
58 panic("should have an error")
61 fmt.Println(err.Error())
63 // 5 error(s) decoding:
65 // * 'Age' expected type 'int', got unconvertible type 'string'
66 // * 'Emails[0]' expected type 'string', got unconvertible type 'int'
67 // * 'Emails[1]' expected type 'string', got unconvertible type 'int'
68 // * 'Emails[2]' expected type 'string', got unconvertible type 'int'
69 // * 'Name' expected type 'string', got unconvertible type 'int'
72 func ExampleDecode_metadata() {
78 // This input can come from anywhere, but typically comes from
79 // something like decoding JSON where we're not quite sure of the
81 input := map[string]interface{}{
84 "email": "foo@bar.com",
87 // For metadata, we make a more advanced DecoderConfig so we can
88 // more finely configure the decoder that is used. In this case, we
89 // just tell the decoder we want to track metadata.
92 config := &DecoderConfig{
97 decoder, err := NewDecoder(config)
102 if err := decoder.Decode(input); err != nil {
106 fmt.Printf("Unused keys: %#v", md.Unused)
108 // Unused keys: []string{"email"}
111 func ExampleDecode_weaklyTypedInput() {
118 // This input can come from anywhere, but typically comes from
119 // something like decoding JSON, generated by a weakly typed language
121 input := map[string]interface{}{
122 "name": 123, // number => string
123 "age": "42", // string => number
124 "emails": map[string]interface{}{}, // empty map => empty array
128 config := &DecoderConfig{
129 WeaklyTypedInput: true,
133 decoder, err := NewDecoder(config)
138 err = decoder.Decode(input)
143 fmt.Printf("%#v", result)
144 // Output: mapstructure.Person{Name:"123", Age:42, Emails:[]string{}}
147 func ExampleDecode_tags() {
148 // Note that the mapstructure tags defined in the struct type
149 // can indicate which fields the values are mapped to.
151 Name string `mapstructure:"person_name"`
152 Age int `mapstructure:"person_age"`
155 input := map[string]interface{}{
156 "person_name": "Mitchell",
161 err := Decode(input, &result)
166 fmt.Printf("%#v", result)
168 // mapstructure.Person{Name:"Mitchell", Age:91}
171 func ExampleDecode_embeddedStruct() {
172 // Squashing multiple embedded structs is allowed using the squash tag.
173 // This is demonstrated by creating a composite struct of multiple types
174 // and decoding into it. In this case, a person can carry with it both
175 // a Family and a Location, as well as their own FirstName.
179 type Location struct {
183 Family `mapstructure:",squash"`
184 Location `mapstructure:",squash"`
188 input := map[string]interface{}{
189 "FirstName": "Mitchell",
190 "LastName": "Hashimoto",
191 "City": "San Francisco",
195 err := Decode(input, &result)
200 fmt.Printf("%s %s, %s", result.FirstName, result.LastName, result.City)
202 // Mitchell Hashimoto, San Francisco