Transcode single fields in a Go struct


#1

When I insert a user document using the go struct

Bucket.Insert("key", &user, 0)

and the struct has fields of type time.Time in it

type User struct {
ID            string     `json:"id"`
UpdatedAt     time.Time   `json:"updatedAt"`
CreatedAt     time.Time   `json:"createdAt"`

}

the json of it is like

{
“id”: “01bfc164-6485-46db-a709-0e4c2581ec31”,
“updatedAt”: “2018-12-02T17:35:34.6110459+01:00”,
“createdAt”: “2018-12-02T17:35:34.6110459+01:00”
}

but I want to save the fields as unix milliseconds like

{
“id”: “01bfc164-6485-46db-a709-0e4c2581ec31”,
“updatedAt”: “1543769760061”,
“createdAt”: “1543769760061”
}

.

How can I insert objects but change some field’s type?


#2

Hi @robinbraemer, whilst we do expose custom transcoding we don’t expose any specific way to do this on a per field basis. The best approach might be to do something like https://stackoverflow.com/questions/23695479/format-timestamp-in-outgoing-json-in-golang where you only have to write your own MarshalJSON function for your type.