Working but i shouldve made connections a map
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -59,5 +60,32 @@ func (o One) MarshalBSON() ([]byte, error) {
|
||||
m[Name] = name
|
||||
delete(m, JSONName)
|
||||
}
|
||||
var connections primitive.A
|
||||
switch m[Connections].(type) {
|
||||
case nil:
|
||||
case []interface{}:
|
||||
connections = primitive.A(m[Connections].([]interface{}))
|
||||
case primitive.A:
|
||||
connections = m[Connections].(primitive.A)
|
||||
default:
|
||||
return nil, fmt.Errorf("bad type for %q: %T", Connections, m[Connections])
|
||||
}
|
||||
curL := len(connections)
|
||||
wantL := len(o.Connections)
|
||||
for i := curL; i < wantL; i++ {
|
||||
connections = append(connections, nil)
|
||||
}
|
||||
for i, connection := range o.Connections {
|
||||
b, err := bson.Marshal(connection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := bson.M{}
|
||||
if err := bson.Unmarshal(b, &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
connections[i] = m
|
||||
}
|
||||
m[Connections] = connections
|
||||
return bson.Marshal(m)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user