MCPcopy Create free account
hub / github.com/cloudquery/cloudquery / bsonDocToMap

Function bsonDocToMap

plugins/destination/mongodb/client/read.go:100–116  ·  view source on GitHub ↗

bsonDocToMap converts a bson.D or bson.M value to map[string]any for JSON marshaling. In MongoDB Go driver v2, nested documents decode as bson.D instead of bson.M, so we recursively convert all nested bson.D values to maps.

(val any)

Source from the content-addressed store, hash-verified

98// In MongoDB Go driver v2, nested documents decode as bson.D instead of bson.M,
99// so we recursively convert all nested bson.D values to maps.
100func bsonDocToMap(val any) map[string]any {
101 switch v := val.(type) {
102 case bson.M:
103 for key, elem := range v {
104 v[key] = convertBSONValue(elem)
105 }
106 return v
107 case bson.D:
108 m := make(map[string]any, len(v))
109 for _, e := range v {
110 m[e.Key] = convertBSONValue(e.Value)
111 }
112 return m
113 default:
114 return nil
115 }
116}
117
118// convertBSONValue recursively converts bson.D and bson.A values to Go maps and slices.
119func convertBSONValue(val any) any {

Calls 1

convertBSONValueFunction · 0.85