Skip to content

Commit

Permalink
Fix omitempty panic for double-embedded struct (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Aug 29, 2024
1 parent f0ac63c commit 7fc2a4f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
34 changes: 34 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,40 @@ func TestMarshalItemAsymmetric(t *testing.T) {
}
}

func TestIssue247(t *testing.T) {
// https: //github.com/guregu/dynamo/issues/247
type ServerResponse struct {
EmbeddedID int
}
type TestAddition struct {
ServerResponse
}
type TestItem struct {
ID int `dynamo:"id,hash" json:"id"`
Name string `dynamo:"name,range" json:"name"`
Addition TestAddition `dynamo:"addition,omitempty"`
}
x := TestItem{ID: 1, Name: "test"}
item, err := MarshalItem(x)
if err != nil {
t.Fatal(err)
}
_, ok := item["addition"]
if ok {
t.Error("should be omitted")
}

x.Addition.EmbeddedID = 123
item, err = MarshalItem(x)
if err != nil {
t.Fatal(err)
}
_, ok = item["addition"]
if !ok {
t.Error("should be present")
}
}

type isValue_Kind interface {
isValue_Kind()
}
Expand Down
8 changes: 6 additions & 2 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,12 @@ func (info *structInfo) isZero(rv reflect.Value) bool {
}
for _, field := range info.fields {
fv := dig(rv, field.index)
if !fv.IsValid() {
// TODO: encode NULL?
if !fv.IsValid() /* field doesn't exist */ {
continue
}
if field.isZero == nil {
// TODO: https://github.com/guregu/dynamo/issues/247
// need to give child structs an isZero
continue
}
if !field.isZero(fv) {
Expand Down

0 comments on commit 7fc2a4f

Please sign in to comment.