forked from mitchellh/mapstructure
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from go-viper/decode-remaining-fields
Fix Encoding Struct Back to Map Bug (mitchellh#279)
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,6 +228,41 @@ func ExampleDecode_remainingData() { | |
// mapstructure.Person{Name:"Mitchell", Age:91, Other:map[string]interface {}{"email":"[email protected]"}} | ||
} | ||
|
||
func ExampleDecode_remainingDataDecodeBackToMapInFlatFormat() { | ||
// Note that the mapstructure tags defined in the struct type | ||
// can indicate which fields the values are mapped to. | ||
type Person struct { | ||
Name string | ||
Age int | ||
Other map[string]interface{} `mapstructure:",remain"` | ||
} | ||
|
||
input := map[string]interface{}{ | ||
"name": "Luffy", | ||
"age": 19, | ||
"powers": []string{ | ||
"Rubber Man", | ||
"Conqueror Haki", | ||
}, | ||
} | ||
|
||
var person Person | ||
err := Decode(input, &person) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
result := make(map[string]interface{}) | ||
err = Decode(&person, &result) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("%#v", result) | ||
// Output: | ||
// map[string]interface {}{"Age":19, "Name":"Luffy", "powers":[]string{"Rubber Man", "Conqueror Haki"}} | ||
} | ||
|
||
func ExampleDecode_omitempty() { | ||
// Add omitempty annotation to avoid map keys for empty values | ||
type Family struct { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters