From 8e12eac25c5b59f99d09e095e1d62427a4030289 Mon Sep 17 00:00:00 2001 From: Greg Date: Sun, 19 May 2024 03:37:19 +0900 Subject: [PATCH] encode: avoid panic for nil embedded struct (#233) --- encode_test.go | 12 ++++++++++++ reflect.go | 3 +++ 2 files changed, 15 insertions(+) diff --git a/encode_test.go b/encode_test.go index ab8361c..c96426b 100644 --- a/encode_test.go +++ b/encode_test.go @@ -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) { diff --git a/reflect.go b/reflect.go index 1c70bd1..4188d0a 100644 --- a/reflect.go +++ b/reflect.go @@ -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 {