This is a BUG I've read up on, wondering if anyone has a particular suggestion for my use-case, since I'm not sure when it's going to get fixed (last I read 1.4).
I'm decoding JSON into the following struct:
User struct {
ID_ bson.ObjectId `bson:"_id,omitempty" json:"_id,omitempty"`
UTC *time.Time `bson:"utc,omitempty" json:"utc,omitempty"`
USR string `bson:"usr,omitempty" json:"usr,omitempty"`
PWD string `bson:"pwd,omitempty" json:"pwd,omitempty"`
}
And then updating the USR and PWD fields using MGO:
func save(w http.ResponseWriter, r *http.Request) {
m := s.Copy()
defer m.Close()
user := m.DB("0").C("user")
var a User
json.NewDecoder(r.Body).Decode(&a)
b := &User {USR:a.USR, PWD:a.PWD}
user.UpdateId(a.ID_, b)
}
When I have time.Time in struct definition, I get a zero date value, this resets my date in my Mongo Doc back to UNIX start. When I use *time.Time, I get a nil date value, which completely erases my date field from my Mongo Doc. Here's the code:
Any ideas how to make this work? I'd rather not create a new struct for each call -- it's nice to essentially reuse my User struct (which is essentially my DB collection schema).
CURRENT STATE:
Some great comments, and work-around solutions, thanks all. However, I'm still confused about the inconsistent behavior of "omitempty". When it's a string && empty it's truly omitted form the struct post Decode. However, when it's a time.Time/bson.ObjectId it's zeroed when empty OR *time.Time/*bson.ObjectId it's nil when empty. It'd be really nice if they were truly omitted. Then we could reuse the struct over and over again (without needing to be ultra explicit -- which is basically additional work to get around those nil/zeroed values).