Add bson.A to slice map function
This commit is contained in:
29
struct.go
29
struct.go
@@ -72,6 +72,35 @@ func MapToStruct(m map[string]any, o any) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WrapA struct {
|
||||||
|
Items []map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
// BsonAToSlice will convert a bson.A to []map[string]any.
|
||||||
|
func BsonAToSlice(m any) ([]map[string]any, error) {
|
||||||
|
|
||||||
|
v := bson.M{
|
||||||
|
"items": m.(bson.A),
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := bson.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed marshal %v", err)
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var o WrapA
|
||||||
|
err = bson.Unmarshal(b, &o)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to unmarshal %v", err)
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return o.Items, nil
|
||||||
|
}
|
||||||
|
|
||||||
// MapIsSubset given two map[string]any m1 and m2 will determine if m1 is a subset of m2.
|
// MapIsSubset given two map[string]any m1 and m2 will determine if m1 is a subset of m2.
|
||||||
// Only fields' name is evaluated not their values.
|
// Only fields' name is evaluated not their values.
|
||||||
func MapIsSubset(subset, superset any) bool {
|
func MapIsSubset(subset, superset any) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user