Skip to content

Commit 7fc2a4f

Browse files
committed
Fix omitempty panic for double-embedded struct (#247)
1 parent f0ac63c commit 7fc2a4f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

encode_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,40 @@ 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+
155189
type isValue_Kind interface {
156190
isValue_Kind()
157191
}

reflect.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,12 @@ 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() {
254-
// TODO: encode NULL?
253+
if !fv.IsValid() /* field doesn't exist */ {
254+
continue
255+
}
256+
if field.isZero == nil {
257+
// TODO: https://github.com/guregu/dynamo/issues/247
258+
// need to give child structs an isZero
255259
continue
256260
}
257261
if !field.isZero(fv) {

0 commit comments

Comments
 (0)