Skip to content

Commit

Permalink
Merge branch 'alecsammon-fix_embedded'
Browse files Browse the repository at this point in the history
  • Loading branch information
mweibel committed Oct 3, 2024
2 parents 3faa0cd + 3854dcf commit 51bf2c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
5 changes: 3 additions & 2 deletions sheriff.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
field := t.Field(i)
val := v.Field(i)

jsonTag, jsonOpts := parseTag(field.Tag.Get("json"))
jsonTagVal, jsonTagExists := field.Tag.Lookup("json")
jsonTag, jsonOpts := parseTag(jsonTagVal)

// If no json tag is provided, use the field Name
if jsonTag == "" {
Expand Down Expand Up @@ -194,7 +195,7 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
// when a composition field we want to bring the child
// nodes to the top
nestedVal, ok := v.(KVStore)
if isEmbeddedField && ok {
if !jsonTagExists && isEmbeddedField && ok {
nestedVal.Each(func(k string, v interface{}) {
dest.Set(k, v)
})
Expand Down
33 changes: 25 additions & 8 deletions sheriff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ type TestMarshal_Embedded struct {
Foo string `json:"foo" groups:"test"`
}

type TestMarshal_NamedEmbedded struct {
Qux string `json:"qux" groups:"test"`
}

// TestMarshal_EmbeddedCustom is used to test an embedded struct with a custom marshaler that is not a pointer.
type TestMarshal_EmbeddedCustom struct {
Val int
Expand Down Expand Up @@ -610,14 +614,16 @@ func (t *TestMarshal_EmbeddedCustomPtr) MarshalJSON() ([]byte, error) {

type TestMarshal_EmbeddedParent struct {
*TestMarshal_Embedded
*TestMarshal_NamedEmbedded `json:"embedded"`
*TestMarshal_EmbeddedCustom `json:"value"`
*TestMarshal_EmbeddedCustomPtr `json:"value_ptr"`
Bar string `json:"bar" groups:"test"`
Bar string `json:"bar" groups:"test"`
}

func TestMarshal_EmbeddedField(t *testing.T) {
v := TestMarshal_EmbeddedParent{
&TestMarshal_Embedded{"Hello"},
&TestMarshal_NamedEmbedded{"Big"},
&TestMarshal_EmbeddedCustom{10, true},
&TestMarshal_EmbeddedCustomPtr{20, true},
"World",
Expand All @@ -630,15 +636,26 @@ func TestMarshal_EmbeddedField(t *testing.T) {
actual, err := json.Marshal(actualMap)
assert.NoError(t, err)

expected, err := json.Marshal(map[string]interface{}{
"bar": "World",
"foo": "Hello",
"value": 10,
"value_ptr": 20,
t.Run("should match the original json marshal", func(t *testing.T) {
expected, err := json.Marshal(v)
assert.NoError(t, err)

assert.JSONEq(t, string(expected), string(actual))
})
assert.NoError(t, err)

assert.Equal(t, string(expected), string(actual))
t.Run("should match the expected map", func(t *testing.T) {
expectedMap, err := json.Marshal(map[string]interface{}{
"bar": "World",
"foo": "Hello",
"value": 10,
"value_ptr": 20,
"embedded": map[string]interface{}{
"qux": "Big",
},
})
assert.NoError(t, err)
assert.JSONEq(t, string(expectedMap), string(actual))
})
}

type TestMarshal_EmbeddedEmpty struct {
Expand Down

0 comments on commit 51bf2c9

Please sign in to comment.