Skip to content

Commit

Permalink
Don't skip anonymous (embedded) fields with named tags (#2022)
Browse files Browse the repository at this point in the history
When collecting the fields in a struct, we skip over embedded fields
because by default they are just that: embedded.

However, if the embedded field itself carries a named tag, it is
not embedded during serialization, so consider this case.

fixes #2021
  • Loading branch information
Airblader authored Feb 20, 2023
1 parent 50d82a9 commit 9af2842
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion feature/dynamodb/attributevalue/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func enumFields(t reflect.Type, opts structFieldOptions) []field {
structField := buildField(f.Index, i, sf, fieldTag)
structField.Type = ft

if !sf.Anonymous || ft.Kind() != reflect.Struct {
if !sf.Anonymous || fieldTag.Name != "" || ft.Kind() != reflect.Struct {
fields = append(fields, structField)
if count[f.Type] > 1 {
// If there were multiple instances, add a second,
Expand Down
13 changes: 13 additions & 0 deletions feature/dynamodb/attributevalue/marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ func BenchmarkUnmarshalTwoMembers(b *testing.B) {
}

func Test_Encode_YAML_TagKey(t *testing.T) {
type Embedded struct {
String string `yaml:"string"`
}

input := struct {
String string `yaml:"string"`
EmptyString string `yaml:"empty"`
Expand All @@ -649,6 +653,7 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
Slice []string `yaml:"slice"`
Map map[string]int `yaml:"map"`
NoTag string
Embedded `yaml:"embedded"`
}{
String: "String",
Ignored: "Ignored",
Expand All @@ -658,6 +663,9 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
"two": 2,
},
NoTag: "NoTag",
Embedded: Embedded{
String: "String",
},
}

expected := &types.AttributeValueMemberM{
Expand All @@ -682,6 +690,11 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
},
},
"NoTag": &types.AttributeValueMemberS{Value: "NoTag"},
"embedded": &types.AttributeValueMemberM{
Value: map[string]types.AttributeValue{
"string": &types.AttributeValueMemberS{Value: "String"},
},
},
},
}

Expand Down
2 changes: 1 addition & 1 deletion feature/dynamodbstreams/attributevalue/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func enumFields(t reflect.Type, opts structFieldOptions) []field {
structField := buildField(f.Index, i, sf, fieldTag)
structField.Type = ft

if !sf.Anonymous || ft.Kind() != reflect.Struct {
if !sf.Anonymous || fieldTag.Name != "" || ft.Kind() != reflect.Struct {
fields = append(fields, structField)
if count[f.Type] > 1 {
// If there were multiple instances, add a second,
Expand Down
13 changes: 13 additions & 0 deletions feature/dynamodbstreams/attributevalue/marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ func BenchmarkUnmarshalTwoMembers(b *testing.B) {
}

func Test_Encode_YAML_TagKey(t *testing.T) {
type Embedded struct {
String string `yaml:"string"`
}

input := struct {
String string `yaml:"string"`
EmptyString string `yaml:"empty"`
Expand All @@ -649,6 +653,7 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
Slice []string `yaml:"slice"`
Map map[string]int `yaml:"map"`
NoTag string
Embedded `yaml:"embedded"`
}{
String: "String",
Ignored: "Ignored",
Expand All @@ -658,6 +663,9 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
"two": 2,
},
NoTag: "NoTag",
Embedded: Embedded{
String: "String",
},
}

expected := &types.AttributeValueMemberM{
Expand All @@ -682,6 +690,11 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
},
},
"NoTag": &types.AttributeValueMemberS{Value: "NoTag"},
"embedded": &types.AttributeValueMemberM{
Value: map[string]types.AttributeValue{
"string": &types.AttributeValueMemberS{Value: "String"},
},
},
},
}

Expand Down

0 comments on commit 9af2842

Please sign in to comment.