Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions events/attributevalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ func (av *DynamoDBAttributeValue) ensureType(expectedType DynamoDBDataType) {

// MarshalJSON implements custom marshaling to be used by the standard json/encoding package
func (av DynamoDBAttributeValue) MarshalJSON() ([]byte, error) {
// A zero DynamoDBAttributeValue holds no value; IsNull reports it as
// null, so encode it as NULL instead of asserting on the nil value.
if av.value == nil {
return []byte(`{ "NULL": true }`), nil
}

var buff bytes.Buffer
var err error
Expand Down
15 changes: 15 additions & 0 deletions events/attributevalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,18 @@ func Test_DynamoDBAttributeValue_NewAttribute(t *testing.T) {
assert.Equal(t, []string{"test", "test"}, av.StringSet())
}
}

func TestMarshalZeroValue(t *testing.T) {
var zero DynamoDBAttributeValue
assert.True(t, zero.IsNull())

b, err := json.Marshal(zero)
assert.NoError(t, err)
assert.JSONEq(t, `{"NULL": true}`, string(b))

// A missing key in an image map yields the zero value.
image := map[string]DynamoDBAttributeValue{"id": NewStringAttribute("1"), "missing": {}}
b, err = json.Marshal(image)
assert.NoError(t, err)
assert.JSONEq(t, `{"id": {"S": "1"}, "missing": {"NULL": true}}`, string(b))
}
Loading