Skip to content

Commit

Permalink
DocumentFromStruct: if struct implements bson.Marshaler, use MarshalBSON
Browse files Browse the repository at this point in the history
Changelog: none

Signed-off-by: Fabio Tranchitella <fabio.tranchitella@northern.tech>
  • Loading branch information
tranchitella committed May 25, 2021
1 parent 9fb0bf7 commit 0cdd40b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mongo/doc/bson.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Northern.tech AS
// Copyright 2021 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,13 @@ func DocumentFromStruct(
sct interface{},
appendElements ...bson.E,
) (doc bson.D) {
if marshaller, ok := sct.(bson.Marshaler); ok {
var doc bson.D
if data, err := marshaller.MarshalBSON(); err == nil {
_ = bson.Unmarshal(data, &doc)
}
return doc
}
s := reflect.ValueOf(sct)
defer func() {
if r := recover(); r != nil {
Expand Down

0 comments on commit 0cdd40b

Please sign in to comment.