New API tested

This commit is contained in:
Bel LaPointe
2020-07-23 21:11:36 -06:00
parent d04ced1e33
commit d572cb5c87
6 changed files with 388 additions and 16 deletions

View File

@@ -316,7 +316,29 @@ func applySet(doc, operator bson.M) (bson.M, error) {
if k == entity.Name {
return nil, errModifiedReserved
}
doc[k] = v
nesting := strings.Split(k, ".")
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)
}
m, ok := mInterface.(map[string]interface{})
if !ok {
pm, pmok := mInterface.(primitive.M)
m = map[string]interface{}(pm)
ok = pmok
}
if !ok {
return nil, fmt.Errorf("subpath cannot be followed for non object: %s (%s): %+v (%T)", k, nesting[0], mInterface, mInterface)
}
subdoc, err := applySet(bson.M(m), bson.M{strings.Join(nesting[1:], "."): v})
if err != nil {
return nil, err
}
doc[nesting[0]] = subdoc
} else {
doc[k] = v
}
}
return doc, nil
}