Allow setting fields previously unset, like adding a connection when it was nil in db
This commit is contained in:
@@ -288,7 +288,8 @@ func applyUnset(doc, operator bson.M) (bson.M, error) {
|
||||
if len(nesting) > 1 {
|
||||
mInterface, ok := doc[nesting[0]]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("path does not exist: %s (%s): %+v", k, nesting[0], doc)
|
||||
continue
|
||||
//return nil, fmt.Errorf("path does not exist: %s (%s): %+v", k, nesting[0], doc)
|
||||
}
|
||||
m, ok := mInterface.(map[string]interface{})
|
||||
if !ok {
|
||||
@@ -320,7 +321,8 @@ func applySet(doc, operator bson.M) (bson.M, error) {
|
||||
if len(nesting) > 1 {
|
||||
mInterface, ok := doc[nesting[0]]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("path does not exist: %s (%s): %+v", k, nesting[0], doc)
|
||||
mInterface = map[string]interface{}{}
|
||||
//return nil, fmt.Errorf("path does not exist: %s (%s): %+v", k, nesting[0], doc)
|
||||
}
|
||||
m, ok := mInterface.(map[string]interface{})
|
||||
if !ok {
|
||||
|
||||
@@ -444,6 +444,11 @@ func TestApplySet(t *testing.T) {
|
||||
doc: bson.M{"hello": bson.M{"world": "not hi"}},
|
||||
want: bson.M{"hello": bson.M{"world": "not hi", "notworld": "hi"}},
|
||||
},
|
||||
"add to nonexisting, nested field": {
|
||||
operator: bson.M{"hello.notworld.notnotworld": "hi"},
|
||||
doc: bson.M{"hello": bson.M{"world": "not hi"}},
|
||||
want: bson.M{"hello": bson.M{"world": "not hi", "notworld": bson.M{"notnotworld": "hi"}}},
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
@@ -459,3 +464,55 @@ func TestApplySet(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyUnset(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
doc bson.M
|
||||
operator bson.M
|
||||
want bson.M
|
||||
}{
|
||||
"noop on empty": {},
|
||||
"noop on full": {
|
||||
doc: bson.M{"hello": "world"},
|
||||
want: bson.M{"hello": "world"},
|
||||
},
|
||||
"del one field on full": {
|
||||
operator: bson.M{"hi": "mom"},
|
||||
doc: bson.M{"hello": "world", "hi": "mom"},
|
||||
want: bson.M{"hello": "world"},
|
||||
},
|
||||
"del only field on full": {
|
||||
operator: bson.M{"hello": ""},
|
||||
doc: bson.M{"hello": "world"},
|
||||
want: bson.M{},
|
||||
},
|
||||
"del existing, nested field": {
|
||||
operator: bson.M{"hello.world": ""},
|
||||
doc: bson.M{"hello": bson.M{"world": "not hi"}},
|
||||
want: bson.M{"hello": bson.M{}},
|
||||
},
|
||||
"del to existing, nested field": {
|
||||
operator: bson.M{"hello.notworld": ""},
|
||||
doc: bson.M{"hello": bson.M{"world": "not hi", "notworld": "hi"}},
|
||||
want: bson.M{"hello": bson.M{"world": "not hi"}},
|
||||
},
|
||||
"del to nonexisting, nested field": {
|
||||
operator: bson.M{"hello.notworld.notnotworld": "hi"},
|
||||
doc: bson.M{"hello": bson.M{"world": "not hi"}},
|
||||
want: bson.M{"hello": bson.M{"world": "not hi"}},
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
c := d
|
||||
t.Run(name, func(t *testing.T) {
|
||||
out, err := applyUnset(c.doc, c.operator)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fmt.Sprint(out) != fmt.Sprint(c.want) {
|
||||
t.Fatalf("(%+v, %+v) => want \n%+v\n, got \n%+v", c.doc, c.operator, c.want, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user