Matt_Dale
(Matt Dale)
5
Hey @Meinrad_Hermanek thanks for the follow-up question! To unmarshal the BSON value, create a bson.RawValue and use that to unmarshal the bytes into a Go time.Time value:
func (v *MyDate) UnmarshalBSONValue(t bsontype.Type, b []byte) error {
rv := bson.RawValue{
Type: t,
Value: b,
}
var res time.Time
if err := rv.Unmarshal(&res); err != nil {
return err
}
*v = MyDate(res)
return nil
}
See an example on the Go Playground here.
P.S. I’m surprised that there isn’t a corresponding UnmarshalValue function in the bson package. That seems like an oversight and definitely makes satisfying the ValueUnmarshaler interface a lot less intuitive. There is an open Jira ticket for adding an UnmarshalValue function (see GODRIVER-1892) that I will suggest the Go driver team prioritize (I work on the Go driver team).
1 Like