// MarshalBSON controls the marshal behavior of the CFResponse struct
func (resp CFResponse) MarshalBSON() ([]byte, error) {
if len(strings.TrimSpace(resp.KTUUID)) == 0 {
ruuid, _ := uuid.NewV4()
resp.KTUUID = ruuid.String()
}
if resp.Created.IsZero() {
// Set the created time if we weren't given one
resp.Created = time.Now()
}
if resp.ID == nil {
resp.ID = utils.NewBSONObjectIDPointer()
}
if resp.Events == nil {
resp.Events = []CFResponseEventDetail{}
}
if resp.Jobs == nil {
resp.Jobs = []CFJobLabel{}
}
return bson.Marshal(resp)
}
// UnmarshalBSON controls the behavior of the CFResponse as it's
// unmarshalled from the DB
func (resp *CFResponse) UnmarshalBSON(bytes []byte) error {
if err := bson.Unmarshal(bytes, resp); err != nil {
return err
}
if len(strings.TrimSpace(resp.KTUUID)) == 0 {
ruuid, _ := uuid.NewV4()
resp.KTUUID = ruuid.String()
}
if resp.Created.IsZero() {
// Set the created time if we weren't given one
resp.Created = time.Now()
}
if resp.ID == nil {
resp.ID = utils.NewBSONObjectIDPointer()
}
if resp.Scores == nil {
resp.Scores = []CFResponseScore{}
}
if resp.Events == nil {
resp.Events = []CFResponseEventDetail{}
}
if resp.Jobs == nil {
resp.Jobs = []CFJobLabel{}
}
return nil
}
Keep in mind that ALL of these values were properly initialized by the old mgo library, so we either have to modify hundreds of functions or add a central decoder.