Skip to content

Commit 51bf2c9

Browse files
committed
Merge branch 'alecsammon-fix_embedded'
2 parents 3faa0cd + 3854dcf commit 51bf2c9

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

sheriff.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
121121
field := t.Field(i)
122122
val := v.Field(i)
123123

124-
jsonTag, jsonOpts := parseTag(field.Tag.Get("json"))
124+
jsonTagVal, jsonTagExists := field.Tag.Lookup("json")
125+
jsonTag, jsonOpts := parseTag(jsonTagVal)
125126

126127
// If no json tag is provided, use the field Name
127128
if jsonTag == "" {
@@ -194,7 +195,7 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
194195
// when a composition field we want to bring the child
195196
// nodes to the top
196197
nestedVal, ok := v.(KVStore)
197-
if isEmbeddedField && ok {
198+
if !jsonTagExists && isEmbeddedField && ok {
198199
nestedVal.Each(func(k string, v interface{}) {
199200
dest.Set(k, v)
200201
})

sheriff_test.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,10 @@ type TestMarshal_Embedded struct {
580580
Foo string `json:"foo" groups:"test"`
581581
}
582582

583+
type TestMarshal_NamedEmbedded struct {
584+
Qux string `json:"qux" groups:"test"`
585+
}
586+
583587
// TestMarshal_EmbeddedCustom is used to test an embedded struct with a custom marshaler that is not a pointer.
584588
type TestMarshal_EmbeddedCustom struct {
585589
Val int
@@ -610,14 +614,16 @@ func (t *TestMarshal_EmbeddedCustomPtr) MarshalJSON() ([]byte, error) {
610614

611615
type TestMarshal_EmbeddedParent struct {
612616
*TestMarshal_Embedded
617+
*TestMarshal_NamedEmbedded `json:"embedded"`
613618
*TestMarshal_EmbeddedCustom `json:"value"`
614619
*TestMarshal_EmbeddedCustomPtr `json:"value_ptr"`
615-
Bar string `json:"bar" groups:"test"`
620+
Bar string `json:"bar" groups:"test"`
616621
}
617622

618623
func TestMarshal_EmbeddedField(t *testing.T) {
619624
v := TestMarshal_EmbeddedParent{
620625
&TestMarshal_Embedded{"Hello"},
626+
&TestMarshal_NamedEmbedded{"Big"},
621627
&TestMarshal_EmbeddedCustom{10, true},
622628
&TestMarshal_EmbeddedCustomPtr{20, true},
623629
"World",
@@ -630,15 +636,26 @@ func TestMarshal_EmbeddedField(t *testing.T) {
630636
actual, err := json.Marshal(actualMap)
631637
assert.NoError(t, err)
632638

633-
expected, err := json.Marshal(map[string]interface{}{
634-
"bar": "World",
635-
"foo": "Hello",
636-
"value": 10,
637-
"value_ptr": 20,
639+
t.Run("should match the original json marshal", func(t *testing.T) {
640+
expected, err := json.Marshal(v)
641+
assert.NoError(t, err)
642+
643+
assert.JSONEq(t, string(expected), string(actual))
638644
})
639-
assert.NoError(t, err)
640645

641-
assert.Equal(t, string(expected), string(actual))
646+
t.Run("should match the expected map", func(t *testing.T) {
647+
expectedMap, err := json.Marshal(map[string]interface{}{
648+
"bar": "World",
649+
"foo": "Hello",
650+
"value": 10,
651+
"value_ptr": 20,
652+
"embedded": map[string]interface{}{
653+
"qux": "Big",
654+
},
655+
})
656+
assert.NoError(t, err)
657+
assert.JSONEq(t, string(expectedMap), string(actual))
658+
})
642659
}
643660

644661
type TestMarshal_EmbeddedEmpty struct {

0 commit comments

Comments
 (0)