dynamo icon indicating copy to clipboard operation
dynamo copied to clipboard

unixtime format time.Time is always omitempty

Open lparkes opened this issue 3 years ago • 2 comments

I have noticed that when storing time.Time values with the unixtime option, that zero value are always omitted. With the following struct, the MyFIeld is never stored if its value is 0.

type MyStruct struct {
	MyField     time.Time `dynamo:",unixtime,allowempty"`
}

The top level README is also slightly misleading. It talks about the TextMarshaller defaulting to omitempty and time.Time triggering this behaviour, but it doesn't mention that when using the unixtime enconding, the TextMarshaller is ignored and there is special code in the library to always omitempty.

Without this field present my objects don't appear in my global secondary index which does make it a touch difficult for me to query them. There may be smarter things I can do with AWS, but I'd prefer to just store 0 in that field.

lparkes avatar Oct 03 '21 00:10 lparkes

Sounds like a bug, thanks. We need to document that behavior and fix allowempty for it. Shouldn't be too hard to fix it. I'll take a look.

I believe the unixtime feature was originally an outside contribution intended for TTL expiration in which 0 values are meaningless. However, I agree the current behavior is confusing.

I'll also tag #113 because I want to go over all the automatic behavior for v2 and have it make more sense. This probably shouldn't be automatically omitted at all in a future version.

guregu avatar Oct 03 '21 01:10 guregu

I wrote a fix for this, but it turned out to be less useful than I thought (for me anyway).

I also had the bright idea of switching my code to use time.Unix(0, 0) as my zero value. Since it's not a time.Time zero value it gets stored just fine and I'm only creating these zero values in one place so it was an easy change.

I'm pretty sure I don't need to use the unixtime but I've seen date parsing in general consume a startling amount of CPU in the past.

lparkes avatar Oct 03 '21 01:10 lparkes