8000 Don't skip anonymous (embedded) fields with named tags (#2022) · aws/aws-sdk-go-v2@9af2842 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9af2842

Browse files
authored
Don't skip anonymous (embedded) fields with named tags (#2022)
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
1 parent 50d82a9 commit 9af2842

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

feature/dynamodb/attributevalue/field.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func enumFields(t reflect.Type, opts structFieldOptions) []field {
128128
structField := buildField(f.Index, i, sf, fieldTag)
129129
structField.Type = ft
130130

131-
if !sf.Anonymous || ft.Kind() != reflect.Struct {
131+
if !sf.Anonymous || fieldTag.Name != "" || ft.Kind() != reflect.Struct {
132132
fields = append(fields, structField)
133133
if count[f.Type] > 1 {
134134
// If there were multiple instances, add a second,

feature/dynamodb/attributevalue/marshaler_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@ func BenchmarkUnmarshalTwoMembers(b *testing.B) {
636636
}
637637

638638
func Test_Encode_YAML_TagKey(t *testing.T) {
639+
type Embedded struct {
640+
String string `yaml:"string"`
641+
}
642+
639643
input := struct {
640644
String string `yaml:"string"`
641645
EmptyString string `yaml:"empty"`
@@ -649,6 +653,7 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
649653
Slice []string `yaml:"slice"`
650654
Map map[string]int `yaml:"map"`
651655
NoTag string
656+
Embedded `yaml:"embedded"`
652657
}{
653658
String: "String",
654659
Ignored: "Ignored",
@@ -658,6 +663,9 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
658663
"two": 2,
659664
},
660665
NoTag: "NoTag",
666+
Embedded: Embedded{
667+
String: "String",
668+
},
661669
}
662670

663671
expected := &types.AttributeValueMemberM{
@@ -682,6 +690,11 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
682690
},
683691
},
684692
"NoTag": &types.AttributeValueMemberS{Value: "NoTag"},
693+
"embedded": &types.AttributeValueMemberM{
694+
Value: map[string]types.AttributeValue{
695+
"string": &types.AttributeValueMemberS{Value: "String"},
696+
},
697+
},
685698
},
686699
}
687700

feature/dynamodbstreams/attributevalue/field.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func enumFields(t reflect.Type, opts structFieldOptions) []field {
128128
structField := buildField(f.Index, i, sf, fieldTag)
129129
structField.Type = ft
130130

131-
if !sf.Anonymous || ft.Kind() != reflect.Struct {
131+
if !sf.Anonymous || fieldTag.Name != "" || ft.Kind() != reflect.Struct {
132132
fields = append(fields, structField)
133133
if count[f.Type] > 1 {
134134
// If there were multiple instances, add a second,

feature/dynamodbstreams/attributevalue/marshaler_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@ func BenchmarkUnmarshalTwoMembers(b *testing.B) {
636636
}
637637

638638
func Test_Encode_YAML_TagKey(t *testing.T) {
639+
type Embedded struct {
640+
String string `yaml:"string"`
641+
}
642+
639643
input := struct {
640644
String string `yaml:"string"`
641645
EmptyString string `yaml:"empty"`
@@ -649,6 +653,7 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
649653
Slice []string `yaml:"slice"`
650654
Map map[string]int `yaml:"map"`
651655
NoTag string
656+
Embedded `yaml:"embedded"`
652657
}{
653658
String: "String",
654659
Ignored: "Ignored",
@@ -658,6 +663,9 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
658663
"two": 2,
659664
},
660665
NoTag: "NoTag",
666+
Embedded: Embedded{
667+
String: "String",
668+
},
661669
}
662670

663671
expected := &types.AttributeValueMemberM{
@@ -682,6 +690,11 @@ func Test_Encode_YAML_TagKey(t *testing.T) {
682690
},
683691
},
684692
"NoTag": &types.AttributeValueMemberS{Value: "NoTag"},
693+
"embedded": &types.AttributeValueMemberM{
694+
Value: map[string]types.AttributeValue{
695+
"string": &types.AttributeValueMemberS{Value: "String"},
696+
},
697+
},
685698
},
686699
}
687700

0 commit comments

Comments
 (0)
0