Skip to content

Commit

Permalink
omitempty for NULL attribute values from custom marshalers
Browse files Browse the repository at this point in the history
  • Loading branch information
babattles committed Aug 23, 2024
1 parent 097b04b commit 976f982
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
14 changes: 14 additions & 0 deletions feature/dynamodb/attributevalue/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,13 @@ type EncoderOptions struct {
// The results of a MarshalText call will convert to string (S), results
// from a MarshalBinary call will convert to binary (B).
UseEncodingMarshalers bool

// When enabled, the encoder will omit null (NULL) attribute values when
// marshaling a field tagged with omitempty.
//
// The value field of the marshaled null (NULL) attribute value must be set
// to true in order for the attribute value to be omitted.
OmitNullAttributeValues bool
}

// An Encoder provides marshaling Go value types to AttributeValues.
Expand Down Expand Up @@ -452,6 +459,8 @@ func (e *Encoder) encode(v reflect.Value, fieldTag tag) (types.AttributeValue, e
if v.Kind() != reflect.Invalid {
if av, err := e.tryMarshaler(v); err != nil {
return nil, err
} else if e.options.OmitNullAttributeValues && fieldTag.OmitEmpty && isNullAttributeValue(av) {
return nil, nil
} else if av != nil {
return av, nil
}
Expand Down Expand Up @@ -893,3 +902,8 @@ func defaultEncodeTime(t time.Time) (types.AttributeValue, error) {
Value: t.Format(time.RFC3339Nano),
}, nil
}

func isNullAttributeValue(av types.AttributeValue) bool {
n, ok := av.(*types.AttributeValueMemberNULL)
return ok && n.Value
}
35 changes: 35 additions & 0 deletions feature/dynamodb/attributevalue/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,41 @@ func TestMarshalOmitEmpty(t *testing.T) {
}
}

type customNullMarshaler struct {}

func (m customNullMarshaler) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
return &types.AttributeValueMemberNULL{Value: true}, nil
}

type testOmitEmptyCustom struct {
CustomNullOmit customNullMarshaler `dynamodbav:",omitempty"`
CustomNullOmitTagKey customNullMarshaler `tagkey:",omitempty"`
CustomNullPresent customNullMarshaler
EmptySetOmit []string `dynamodbav:",omitempty"`
}

func TestMarshalOmitEmptyCustom(t *testing.T) {
expect := &types.AttributeValueMemberM{
Value: map[string]types.AttributeValue{
"CustomNullPresent": &types.AttributeValueMemberNULL{Value: true},
},
}

m := testOmitEmptyCustom{}

actual, err := MarshalWithOptions(m, func(eo *EncoderOptions){
eo.TagKey = "tagkey"
eo.OmitNullAttributeValues = true
eo.NullEmptySets = true
})
if err != nil {
t.Errorf("expect nil, got %v", err)
}
if e, a := expect, actual; !reflect.DeepEqual(e, a) {
t.Errorf("expect %v, got %v", e, a)
}
}

func TestEncodeEmbeddedPointerStruct(t *testing.T) {
type B struct {
Bint int
Expand Down

0 comments on commit 976f982

Please sign in to comment.