Allow setting fields previously unset, like adding a connection when it was nil in db

master
breel 2020-07-26 02:37:02 -06:00
parent 962a0b0585
commit 363d3143e8
5 changed files with 91 additions and 3 deletions

View File

@ -13,6 +13,7 @@ info:
servers:
- url: http://api.dndex.lan:8080/
- url: http://authapi.dndex.lan:8080/
paths:
/who:

@ -1 +1 @@
Subproject commit ef4bfae193a599c4fe7016a64e56663caf06f470
Subproject commit ae4e0a319c0c9a0d22c03df66985c802053f8f05

View File

@ -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 {

View File

@ -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)
}
})
}
}

View File

@ -342,6 +342,34 @@ func TestIntegration(t *testing.T) {
}
})
t.Run("graph.Update connections when none previously", func(t *testing.T) {
cleanFill()
one := randomOne()
one.Connections = nil
err := graph.Insert(ctx, "col", one)
if err != nil {
t.Fatal(err)
}
err = graph.Update(ctx, "col", one.Query(), operator.Set{Key: entity.Connections + ".newfriend", Value: randomOne()})
if err != nil {
t.Fatal(err)
}
ones, err = graph.ListCaseInsensitive(ctx, "col", one.Name)
if err != nil {
t.Fatal(err)
}
if len(ones) != 1 {
t.Fatal(ones)
}
if len(ones[0].Connections) != 1 {
t.Fatal(ones[0].Connections)
}
if _, ok := ones[0].Connections["newfriend"]; !ok {
t.Fatal(ones[0].Connections)
}
})
}
func randomOne() entity.One {