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

service/dynamodb/dynamodbattribute: Add marshaling nil map as empty map #2419

Closed
wants to merge 3 commits into from

Conversation

jasdel
Copy link
Contributor

@jasdel jasdel commented Jan 21, 2019

Adds support for optionally marshaling nil maps and slices as empty maps and lists instead of NULL DynamoDB AttributeValue.

  • Adds keepempty struct tag for marshaling and unmarshaling structs. Maps and Slices decorated with this struct tag if nil or empty will be marshaled as empty lists, maps, sets, etc instead of default NULL value.

  • Adds MarshalOptions KeepEmpty encoder and decoder marshaling options. Has same effect as the keepempty struct tag, but applying to all members marshaled.

Fix #682 #1890
Related: aws/aws-sdk-go-v2#195
Related PRs: #2105, #2032

TODO:

  • Documentation
  • Clarify interaction between keepempty tag, KeepEmpty option, and omitempty tag.

snithish and others added 2 commits January 21, 2019 14:48
…s empty map

Adds support for marshaling nil maps and slices as empty maps and
lists instead of NULL DynamoDB AttributeValue.

Fix aws#682
Related: aws/aws-sdk-go-v2#195
Original PR: aws#2105
@jasdel
Copy link
Contributor Author

jasdel commented Jan 22, 2019

While adding documentation for this feature, I came across a few areas that I think might be confusing for this feature. Specifically the difference between nil vs empty of a slice/map, along with the marshaler's behavior on when elements are set or not set on the user's side during marshal.

To help clarify this I think renaming NilAsEmpty to KeepEmpty to be more inline with the common, omitempty. Will review if a nil vs empty of a list/map needs additional separation, but a keepempty I think covers the nil and empty usecases. Need to see if or how keepempty applies to non map/slice members.

@gobadiah
Copy link

@jasdel do you use this code yourself ? we also have a need to save empty maps and slices correctly (not as nil) in dynamodb.

@jasdel
Copy link
Contributor Author

jasdel commented Jun 13, 2019

Thanks for asking @gobadiah. Could you share more information on your use case.

In making this change, it became evident that (un)marshaler's default behavior for handling of maps and slices insufficient. In building out this PR, I think the (un)marshaler's behavior should be that nil maps/slices are sent to DynamoDB as nil, and non-nil empty maps/slices should be stored as empty maps and lists in DynamoDB. With the same behavior for unmarshaling DynamoDB items to Go types. With the exception of members decorated as Set since they must not be empty.

I think the PR should be updated to include this behavior.

@gobadiah
Copy link

Our use case is pretty much exactly the same. Right now if we save empty map / slice we get back a nil value, which forces us to put all kind of custom logic to handle these cases.

I've read past issues but I don't really understand the reasoning behind the current behaviour, and why this change is not getting more support.

Do you have a workaround or are you using your own fork to get around this limitation ?

@diehlaws
Copy link
Contributor

Fixes #2746

@jasdel
Copy link
Contributor Author

jasdel commented Sep 16, 2019

Replaced by #2834

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/work-in-progress This PR is a draft and needs further work.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

service/dynamodb/dynamodbattribute: Marshal empty map inserts Null AttributeValue instead of empty Map
4 participants