Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 58fb9bd

Browse files
committed
Merge pull request #21 from ryanuber/f-squash
Add squash example
2 parents 740c764 + 4e86876 commit 58fb9bd

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

mapstructure_examples_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,37 @@ func ExampleDecode_tags() {
167167
// Output:
168168
// mapstructure.Person{Name:"Mitchell", Age:91}
169169
}
170+
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.
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+
// Output:
202+
// Mitchell Hashimoto, San Francisco
203+
}

0 commit comments

Comments
 (0)