New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encode a struct with ",remain" back to a map? #279
Comments
For now I'm using this simple workaround: func Encode(v interface{}) ([]byte, error) {
var m map[string]interface{}
if err := mapstructure.Decode(v, &m); err != nil {
return nil, err
}
expandRemainderValues(m)
return json.Marshal(m)
}
func expandRemainderValues(m map[string]interface{}) {
for k, v := range m {
v, ok := v.(map[string]interface{})
if !ok {
continue
}
if k == "" {
for remainderK, remainderV := range v {
m[remainderK] = remainderV
}
delete(m, "")
} else {
expandRemainderValues(v)
}
}
} |
Looks like the behavior has changed in 1.5.0. Now my |
Yeah, there was a bug fixed in 1.5.0 that caused some struct fields to get an empty string in the map. So you're seeing that fix, while still retaining your bug. We just need to turn your repro into a test case, and get a fix on top of it. |
Signed-off-by: Bhargav Ravuri <vaguecoder0to.n@gmail.com>
#307 looks like a nice fix for this issue. |
@mitchellh can we merge #307? it's a tested by prod fix :) |
Fix Encoding Struct Back to Map Bug (mitchellh#279)
I have a use case where I need to decode a JSON, where I care only about a few fields, change it here and there, and then serialize it back to JSON. However, the remainder values seem to get serialized into an empty
""
JSON field. This is my code:And the output is:
Am I missing some configuration option or is
remain
just not supported when encoding a struct into a map?I'm using mapstructure version v1.4.3.
The text was updated successfully, but these errors were encountered: