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)
|
||||
}
|
||||
|
||||
@@ -30,6 +30,9 @@ func TestOneMarshalBSON(t *testing.T) {
|
||||
"modified changes": {
|
||||
one: (One{Name: "hello", Type: "world", Modified: 1}),
|
||||
},
|
||||
"w/ connections": {
|
||||
one: (One{Name: "hello", Type: "world", Modified: 1, Connections: []One{One{Name: "hi", Relationship: "mom"}}}),
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
@@ -53,6 +56,9 @@ func TestOneMarshalBSON(t *testing.T) {
|
||||
}
|
||||
c.one.Modified = 0
|
||||
one.Modified = 0
|
||||
for i := range one.Connections {
|
||||
one.Connections[i].Modified = 0
|
||||
}
|
||||
if fmt.Sprint(c.one) != fmt.Sprint(one) {
|
||||
t.Error(c.one, one)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user