Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

omitempty for NULL attribute values from custom marshalers #2739

Merged
merged 4 commits into from
Aug 24, 2024
Merged
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
9 changes: 9 additions & 0 deletions .changelog/281f2e28209e44068c04c9d74bab8970.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "281f2e28-209e-4406-8c04-c9d74bab8970",
"type": "feature",
"description": "Add Encoder option to obey omitempty tag for NULL attribute values.",
"modules": [
"feature/dynamodb/attributevalue",
"feature/dynamodbstreams/attributevalue"
]
}
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
// returned from custom marshalers tagged with `omitempty`.
//
// NULL attribute values returned from the standard marshaling routine will
// always respect omitempty regardless of this setting.
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
14 changes: 14 additions & 0 deletions feature/dynamodbstreams/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
// returned from custom marshalers tagged with `omitempty`.
//
// NULL attribute values returned from the standard marshaling routine will
// always respect omitempty regardless of this setting.
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/dynamodbstreams/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) MarshalDynamoDBStreamsAttributeValue() (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
Loading