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
Add support for squashing embedded struct pointers #205
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,11 @@ type EmbeddedSquash struct { | |
Vunique string | ||
} | ||
|
||
type EmbeddedPointerSquash struct { | ||
*Basic `mapstructure:",squash"` | ||
Vunique string | ||
} | ||
|
||
type EmbeddedAndNamed struct { | ||
Basic | ||
Named Basic | ||
|
@@ -655,6 +660,56 @@ func TestDecodeFrom_EmbeddedSquash(t *testing.T) { | |
} | ||
} | ||
|
||
func TestDecode_EmbeddedPointerSquash_FromStructToMap(t *testing.T) { | ||
t.Parallel() | ||
|
||
input := EmbeddedPointerSquash{ | ||
Basic: &Basic{ | ||
Vstring: "foo", | ||
}, | ||
Vunique: "bar", | ||
} | ||
|
||
var result map[string]interface{} | ||
err := Decode(input, &result) | ||
if err != nil { | ||
t.Fatalf("got an err: %s", err.Error()) | ||
} | ||
|
||
if result["Vstring"] != "foo" { | ||
t.Errorf("vstring value should be 'foo': %#v", result["Vstring"]) | ||
} | ||
|
||
if result["Vunique"] != "bar" { | ||
t.Errorf("vunique value should be 'bar': %#v", result["Vunique"]) | ||
} | ||
} | ||
|
||
func TestDecode_EmbeddedPointerSquash_FromMapToStruct(t *testing.T) { | ||
t.Parallel() | ||
|
||
input := map[string]interface{}{ | ||
"Vstring": "foo", | ||
"Vunique": "bar", | ||
} | ||
|
||
result := EmbeddedPointerSquash{ | ||
Basic: &Basic{}, | ||
} | ||
err := Decode(input, &result) | ||
if err != nil { | ||
t.Fatalf("got an err: %s", err.Error()) | ||
} | ||
|
||
if result.Vstring != "foo" { | ||
t.Errorf("vstring value should be 'foo': %#v", result.Vstring) | ||
} | ||
|
||
if result.Vunique != "bar" { | ||
t.Errorf("vunique value should be 'bar': %#v", result.Vunique) | ||
} | ||
} | ||
|
||
func TestDecode_EmbeddedSquashConfig(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
@@ -2334,7 +2389,26 @@ func TestDecode_StructTaggedWithOmitempty_KeepNonEmptyValues(t *testing.T) { | |
"visible-map": emptyMap, | ||
"omittable-map": map[string]interface{}{"k": "v"}, | ||
"visible-nested": emptyNested, | ||
"omittable-nested": &Nested{}, | ||
"omittable-nested": map[string]interface{}{ | ||
"Vbar": map[string]interface{}{ | ||
"Vbool": false, | ||
"Vdata": interface{}(nil), | ||
"Vextra": "", | ||
"Vfloat": float64(0), | ||
"Vint": 0, | ||
"Vint16": int16(0), | ||
"Vint32": int32(0), | ||
"Vint64": int64(0), | ||
"Vint8": int8(0), | ||
"VjsonFloat": float64(0), | ||
"VjsonInt": 0, | ||
"VjsonNumber": json.Number(""), | ||
"VjsonUint": uint(0), | ||
"Vstring": "", | ||
"Vuint": uint(0), | ||
}, | ||
"Vfoo": "", | ||
}, | ||
Comment on lines
+2392
to
+2411
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test change was masking the bug described in #231 This PR should not have changed any tests with omitempty, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you're probably right. I thought this would be safe because this only added tests but I see this is a change. |
||
} | ||
|
||
actual := &map[string]interface{}{} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is what is causing the bug in #231
If I remove this line my tests pass, but of course the new tested added in this PR fail.
I attempted to move this check to lower down, but I have not got it working yet. It seems like we should not be changing
v
, but we may need to deref the ptr in a few places to make this work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it to pass, one sec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#232