Skip to content

Commit cb20568

Browse files
committed
tidy: move issue #247 test to the big table
1 parent 7fc2a4f commit cb20568

File tree

3 files changed

+70
-37
lines changed

3 files changed

+70
-37
lines changed

encode_test.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -152,40 +152,6 @@ func TestMarshalItemAsymmetric(t *testing.T) {
152152
}
153153
}
154154

155-
func TestIssue247(t *testing.T) {
156-
// https: //github.com/guregu/dynamo/issues/247
157-
type ServerResponse struct {
158-
EmbeddedID int
159-
}
160-
type TestAddition struct {
161-
ServerResponse
162-
}
163-
type TestItem struct {
164-
ID int `dynamo:"id,hash" json:"id"`
165-
Name string `dynamo:"name,range" json:"name"`
166-
Addition TestAddition `dynamo:"addition,omitempty"`
167-
}
168-
x := TestItem{ID: 1, Name: "test"}
169-
item, err := MarshalItem(x)
170-
if err != nil {
171-
t.Fatal(err)
172-
}
173-
_, ok := item["addition"]
174-
if ok {
175-
t.Error("should be omitted")
176-
}
177-
178-
x.Addition.EmbeddedID = 123
179-
item, err = MarshalItem(x)
180-
if err != nil {
181-
t.Fatal(err)
182-
}
183-
_, ok = item["addition"]
184-
if !ok {
185-
t.Error("should be present")
186-
}
187-
}
188-
189155
type isValue_Kind interface {
190156
isValue_Kind()
191157
}

encoding_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,50 @@ var itemEncodingTests = []struct {
541541
"Embedded": &types.AttributeValueMemberS{Value: "OK"},
542542
},
543543
},
544+
{
545+
name: "field with embedded struct + omitempty (empty)",
546+
in: Issue247{ID: 1, Name: "test"},
547+
out: Item{
548+
"id": &types.AttributeValueMemberN{Value: "1"},
549+
"name": &types.AttributeValueMemberS{Value: "test"},
550+
},
551+
},
552+
{
553+
name: "field with embedded struct + omitempty (not empty)",
554+
in: Issue247{
555+
ID: 1,
556+
Name: "test",
557+
Addition: Issue247Field{Issue247Embedded: Issue247Embedded{EmbeddedID: 123}},
558+
},
559+
out: Item{
560+
"id": &types.AttributeValueMemberN{Value: "1"},
561+
"name": &types.AttributeValueMemberS{Value: "test"},
562+
"addition": &types.AttributeValueMemberM{Value: Item{"EmbeddedID": &types.AttributeValueMemberN{Value: "123"}}},
563+
},
564+
},
565+
{
566+
name: "field with embedded struct subfield + omitempty (empty)",
567+
in: Issue247Alt{ID: 1, Name: "test", Addition: Issue247FieldAlt{}},
568+
out: Item{
569+
"id": &types.AttributeValueMemberN{Value: "1"},
570+
"name": &types.AttributeValueMemberS{Value: "test"},
571+
},
572+
},
573+
{
574+
name: "field with embedded struct subfield + omitempty (not empty)",
575+
in: Issue247Alt{ID: 1, Name: "test", Addition: Issue247FieldAlt{
576+
Field: Issue247Embedded{EmbeddedID: 123},
577+
}},
578+
out: Item{
579+
"id": &types.AttributeValueMemberN{Value: "1"},
580+
"name": &types.AttributeValueMemberS{Value: "test"},
581+
"addition": &types.AttributeValueMemberM{Value: Item{
582+
"Field": &types.AttributeValueMemberM{Value: Item{
583+
"EmbeddedID": &types.AttributeValueMemberN{Value: "123"},
584+
}},
585+
}},
586+
},
587+
},
544588
{
545589
name: "sets",
546590
in: struct {
@@ -969,6 +1013,27 @@ func byteSlicePtr(a []byte) *[]byte {
9691013
return &a
9701014
}
9711015

1016+
type Issue247 struct {
1017+
ID int `dynamo:"id,hash" json:"id"`
1018+
Name string `dynamo:"name,range" json:"name"`
1019+
Addition Issue247Field `dynamo:"addition,omitempty"`
1020+
}
1021+
type Issue247Field struct {
1022+
Issue247Embedded
1023+
}
1024+
type Issue247Embedded struct {
1025+
EmbeddedID int
1026+
}
1027+
1028+
type Issue247Alt struct {
1029+
ID int `dynamo:"id,hash" json:"id"`
1030+
Name string `dynamo:"name,range" json:"name"`
1031+
Addition Issue247FieldAlt `dynamo:"addition,omitempty"`
1032+
}
1033+
type Issue247FieldAlt struct {
1034+
Field Issue247Embedded `dynamo:",omitempty"`
1035+
}
1036+
9721037
var (
9731038
_ Marshaler = new(customMarshaler)
9741039
_ Unmarshaler = new(customMarshaler)

reflect.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,14 @@ func (info *structInfo) isZero(rv reflect.Value) bool {
250250
}
251251
for _, field := range info.fields {
252252
fv := dig(rv, field.index)
253-
if !fv.IsValid() /* field doesn't exist */ {
253+
if !fv.IsValid() {
254+
// field doesn't exist
254255
continue
255256
}
256257
if field.isZero == nil {
257-
// TODO: https://github.com/guregu/dynamo/issues/247
258-
// need to give child structs an isZero
258+
// see: https://github.com/guregu/dynamo/issues/247
259+
// this happens when there's no substance in the field
260+
// (e.g. when it's embedded and isZero is handled by the parent type)
259261
continue
260262
}
261263
if !field.isZero(fv) {

0 commit comments

Comments
 (0)