Add BsonToStruct
This commit is contained in:
18
struct.go
18
struct.go
@@ -128,6 +128,24 @@ func StructToMapRecursive(obj any) any {
|
||||
}
|
||||
}
|
||||
|
||||
func BsonToStruct(m bson.M, o any) error {
|
||||
b, err := bson.Marshal(m)
|
||||
if err != nil {
|
||||
log.Printf("Failed marshal %v", err)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
err = bson.Unmarshal(b, o)
|
||||
if err != nil {
|
||||
log.Printf("Failed to unmarshal %v", err)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MapToStruct will convert a map[string]any to a struct.
|
||||
func MapToStruct(m map[string]any, o any) error {
|
||||
b, err := bson.Marshal(m)
|
||||
|
||||
Reference in New Issue
Block a user