Skip to content

Commit 84ca95e

Browse files
authored
omitempty for NULL attribute values from custom marshalers (#2739)
1 parent d7a7f5a commit 84ca95e

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": "281f2e28-209e-4406-8c04-c9d74bab8970",
3+
"type": "feature",
4+
"description": "Add Encoder option to obey omitempty tag for NULL attribute values.",
5+
"modules": [
6+
"feature/dynamodb/attributevalue",
7+
"feature/dynamodbstreams/attributevalue"
8+
]
9+
}

feature/dynamodb/attributevalue/encode.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,13 @@ type EncoderOptions struct {
392392
// The results of a MarshalText call will convert to string (S), results
393393
// from a MarshalBinary call will convert to binary (B).
394394
UseEncodingMarshalers bool
395+
396+
// When enabled, the encoder will omit null (NULL) attribute values
397+
// returned from custom marshalers tagged with `omitempty`.
398+
//
399+
// NULL attribute values returned from the standard marshaling routine will
400+
// always respect omitempty regardless of this setting.
401+
OmitNullAttributeValues bool
395402
}
396403

397404
// An Encoder provides marshaling Go value types to AttributeValues.
@@ -452,6 +459,8 @@ func (e *Encoder) encode(v reflect.Value, fieldTag tag) (types.AttributeValue, e
452459
if v.Kind() != reflect.Invalid {
453460
if av, err := e.tryMarshaler(v); err != nil {
454461
return nil, err
462+
} else if e.options.OmitNullAttributeValues && fieldTag.OmitEmpty && isNullAttributeValue(av) {
463+
return nil, nil
455464
} else if av != nil {
456465
return av, nil
457466
}
@@ -893,3 +902,8 @@ func defaultEncodeTime(t time.Time) (types.AttributeValue, error) {
893902
Value: t.Format(time.RFC3339Nano),
894903
}, nil
895904
}
905+
906+
func isNullAttributeValue(av types.AttributeValue) bool {
907+
n, ok := av.(*types.AttributeValueMemberNULL)
908+
return ok && n.Value
909+
}

feature/dynamodb/attributevalue/encode_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,41 @@ func TestMarshalOmitEmpty(t *testing.T) {
420420
}
421421
}
422422

423+
type customNullMarshaler struct{}
424+
425+
func (m customNullMarshaler) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
426+
return &types.AttributeValueMemberNULL{Value: true}, nil
427+
}
428+
429+
type testOmitEmptyCustom struct {
430+
CustomNullOmit customNullMarshaler `dynamodbav:",omitempty"`
431+
CustomNullOmitTagKey customNullMarshaler `tagkey:",omitempty"`
432+
CustomNullPresent customNullMarshaler
433+
EmptySetOmit []string `dynamodbav:",omitempty"`
434+
}
435+
436+
func TestMarshalOmitEmptyCustom(t *testing.T) {
437+
expect := &types.AttributeValueMemberM{
438+
Value: map[string]types.AttributeValue{
439+
"CustomNullPresent": &types.AttributeValueMemberNULL{Value: true},
440+
},
441+
}
442+
443+
m := testOmitEmptyCustom{}
444+
445+
actual, err := MarshalWithOptions(m, func(eo *EncoderOptions) {
446+
eo.TagKey = "tagkey"
447+
eo.OmitNullAttributeValues = true
448+
eo.NullEmptySets = true
449+
})
450+
if err != nil {
451+
t.Errorf("expect nil, got %v", err)
452+
}
453+
if e, a := expect, actual; !reflect.DeepEqual(e, a) {
454+
t.Errorf("expect %v, got %v", e, a)
455+
}
456+
}
457+
423458
func TestEncodeEmbeddedPointerStruct(t *testing.T) {
424459
type B struct {
425460
Bint int

feature/dynamodbstreams/attributevalue/encode.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,13 @@ type EncoderOptions struct {
392392
// The results of a MarshalText call will convert to string (S), results
393393
// from a MarshalBinary call will convert to binary (B).
394394
UseEncodingMarshalers bool
395+
396+
// When enabled, the encoder will omit null (NULL) attribute values
397+
// returned from custom marshalers tagged with `omitempty`.
398+
//
399+
// NULL attribute values returned from the standard marshaling routine will
400+
// always respect omitempty regardless of this setting.
401+
OmitNullAttributeValues bool
395402
}
396403

397404
// An Encoder provides marshaling Go value types to AttributeValues.
@@ -452,6 +459,8 @@ func (e *Encoder) encode(v reflect.Value, fieldTag tag) (types.AttributeValue, e
452459
if v.Kind() != reflect.Invalid {
453460
if av, err := e.tryMarshaler(v); err != nil {
454461
return nil, err
462+
} else if e.options.OmitNullAttributeValues && fieldTag.OmitEmpty && isNullAttributeValue(av) {
463+
return nil, nil
455464
} else if av != nil {
456465
return av, nil
457466
}
@@ -893,3 +902,8 @@ func defaultEncodeTime(t time.Time) (types.AttributeValue, error) {
893902
Value: t.Format(time.RFC3339Nano),
894903
}, nil
895904
}
905+
906+
func isNullAttributeValue(av types.AttributeValue) bool {
907+
n, ok := av.(*types.AttributeValueMemberNULL)
908+
return ok && n.Value
909+
}

feature/dynamodbstreams/attributevalue/encode_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,41 @@ func TestMarshalOmitEmpty(t *testing.T) {
420420
}
421421
}
422422

423+
type customNullMarshaler struct{}
424+
425+
func (m customNullMarshaler) MarshalDynamoDBStreamsAttributeValue() (types.AttributeValue, error) {
426+
return &types.AttributeValueMemberNULL{Value: true}, nil
427+
}
428+
429+
type testOmitEmptyCustom struct {
430+
CustomNullOmit customNullMarshaler `dynamodbav:",omitempty"`
431+
CustomNullOmitTagKey customNullMarshaler `tagkey:",omitempty"`
432+
CustomNullPresent customNullMarshaler
433+
EmptySetOmit []string `dynamodbav:",omitempty"`
434+
}
435+
436+
func TestMarshalOmitEmptyCustom(t *testing.T) {
437+
expect := &types.AttributeValueMemberM{
438+
Value: map[string]types.AttributeValue{
439+
"CustomNullPresent": &types.AttributeValueMemberNULL{Value: true},
440+
},
441+
}
442+
443+
m := testOmitEmptyCustom{}
444+
445+
actual, err := MarshalWithOptions(m, func(eo *EncoderOptions) {
446+
eo.TagKey = "tagkey"
447+
eo.OmitNullAttributeValues = true
448+
eo.NullEmptySets = true
449+
})
450+
if err != nil {
451+
t.Errorf("expect nil, got %v", err)
452+
}
453+
if e, a := expect, actual; !reflect.DeepEqual(e, a) {
454+
t.Errorf("expect %v, got %v", e, a)
455+
}
456+
}
457+
423458
func TestEncodeEmbeddedPointerStruct(t *testing.T) {
424459
type B struct {
425460
Bint int

0 commit comments

Comments
 (0)