Open kmacmcfarlane opened 1 year ago
Hi @kmacmcfarlane ,
The Dynamodb marshallar is built to account for Dynamodb native types (BOOL,SS,N,S,L, etc..) time.Time is not one of them. Thats why there is no out of the box functionality to support this. I have converted this to a feature request and added it to our backlog until it gets prioritized.
In the meantime, as a workaround, you can handle your time.Time data by converting it into a string or an epoch number, both of which are supported by the encoder. For example, you can format the time.Time as an ISO 8601 string, or you can convert it to a UNIX timestamp (epoch number). Something like
epochNumber := yourTime.Unix()
Thanks, Ran~
@RanVaknin This is surprising, since the godocs indicate time.RFC3339Nano
is the default encoding for time.Time
in Marshal()
and the EncoderOptions
docs indicate if you don't supply a EncodeTime
func it will use time.RFC3339Nano
as well. If I call MarshalMapWithOptions()
with default options, I would expect it to encode time.Time
fields as time.RFC3339Nano
strings.
Further the docs state
MarshalMapWithOptions is an alias for MarshalWithOptions func which marshals Go value type to a map of AttributeValues. If the in parameter does not serialize to a map, an empty AttributeValue map will be returned.
It feels like it shouldn't behave any different than a regular Marshal()
which is why I think it's a bug.
Describe the bug
I am seeing that it's not possible to use
omitempty
ontime.Time
struct fields withattributevalue.MarshalMap()
fromgithub.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue
.This behavior was observed on the latest version:
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue
v1.12.3Expected Behavior
When
MarshalMap()
is supplied with a struct containing atime.Time
field with the tagomitempty
whos value is the zero value, the output map should not contain a key for that struct field.Current Behavior
Output attribute value map includes a key and value for the field that should be omitted.
Reproduction Steps
repro_test.go
service_test_suite.go
Possible Solution
As far as I've been able to reason by stepping through the repro above in a debugger, there should probably be a check for
fieldTag.OmitEmpty
in this code block inencode.go
(just like thefieldTag.AsUnixTime
tag is checked).https://github.com/aws/aws-sdk-go-v2/blob/3bd97c063d962a34ca496720a3ce00ef4affe5fd/feature/dynamodb/attributevalue/encode.go#L478
Additional Information/Context
No response
AWS Go SDK V2 Module Versions Used
Compiler and Version used
go version go1.20.6 linux/amd64
Operating System and version
Fedora Linux 37