Skip to content

Commit

Permalink
encode: avoid panic for nil embedded struct (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed May 18, 2024
1 parent 17ebbaf commit 8e12eac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ var itemEncodeOnlyTests = []struct {
"Public": {N: aws.String("555")},
},
},
{
name: "nil exported pointer embedded struct",
in: struct {
ID string
*ExportedEmbedded
}{
ID: "abc",
},
out: map[string]*dynamodb.AttributeValue{
"ID": {S: aws.String("abc")},
},
},
}

func TestMarshal(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func indirectPtrNoAlloc(rv reflect.Value) reflect.Value {
func dig(rv reflect.Value, index []int) reflect.Value {
rv = indirectNoAlloc(rv)
for i, idx := range index {
if !rv.IsValid() {
break
}
if i == len(index)-1 {
rv = indirectPtrNoAlloc(rv.Field(idx))
} else {
Expand Down

0 comments on commit 8e12eac

Please sign in to comment.