Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support $rename field update operator #1753

Merged
merged 48 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
7a26bae
wip
noisersup Jan 4, 2023
8b9641d
wip
noisersup Jan 4, 2023
fa48763
wip
noisersup Jan 5, 2023
f9ee246
wip
noisersup Jan 5, 2023
7409e97
wip
noisersup Jan 5, 2023
d29931a
wip
noisersup Jan 5, 2023
f3d6fa8
wip
noisersup Jan 5, 2023
d1e3b3d
wip
noisersup Jan 5, 2023
3635d3a
wip
noisersup Jan 5, 2023
fc3e97e
wip
noisersup Jan 5, 2023
d94217f
wip
noisersup Jan 6, 2023
0b7bf27
wip
noisersup Jan 6, 2023
a942027
wip
noisersup Jan 6, 2023
baa4a5e
wip
noisersup Jan 6, 2023
c417def
Merge branch 'main' into rename-update-operator-626
noisersup Jan 6, 2023
f7af8c9
wip
noisersup Jan 6, 2023
882fbe7
wip
noisersup Jan 6, 2023
215b49c
Merge branch 'main' into rename-update-operator-626
mergify[bot] Jan 6, 2023
3d2e4d0
Merge branch 'main' into rename-update-operator-626
mergify[bot] Jan 6, 2023
1ce59e4
wip
noisersup Jan 9, 2023
d89d436
wip
noisersup Jan 9, 2023
addbc3e
Merge branch 'main' into rename-update-operator-626
mergify[bot] Jan 9, 2023
5a6b250
wip
noisersup Jan 10, 2023
e511305
wip
noisersup Jan 10, 2023
18ecb80
Merge branch 'main' into rename-update-operator-626
mergify[bot] Jan 10, 2023
9edd94e
wip
noisersup Jan 10, 2023
8a32051
wip
noisersup Jan 10, 2023
e69b24c
wip
noisersup Jan 10, 2023
58107c6
wip
noisersup Jan 10, 2023
04c8057
Update internal/types/path.go
noisersup Jan 11, 2023
6176b19
wip
noisersup Jan 11, 2023
d55a9f7
wip
noisersup Jan 11, 2023
38ec522
wip
noisersup Jan 11, 2023
a99608c
wip
noisersup Jan 11, 2023
55c72ba
wip
noisersup Jan 11, 2023
c944c71
wip
noisersup Jan 11, 2023
bf21a30
wip
noisersup Jan 11, 2023
0f1aa02
wip
noisersup Jan 11, 2023
d4b5d85
wip
noisersup Jan 11, 2023
0f25e59
wip
noisersup Jan 11, 2023
b970deb
wip
noisersup Jan 12, 2023
e2268bb
wip
noisersup Jan 12, 2023
fab6690
Add suggestions from reviews
noisersup Jan 12, 2023
d0c3a32
remove obsolete check
noisersup Jan 12, 2023
b18a5df
Merge branch 'main' into rename-update-operator-626
noisersup Jan 12, 2023
b35521c
Merge branch 'main' into rename-update-operator-626
mergify[bot] Jan 12, 2023
16e054e
Merge branch 'main' into rename-update-operator-626
Jan 12, 2023
168ce98
Merge branch 'main' into rename-update-operator-626
AlekSi Jan 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
noisersup committed Jan 5, 2023
commit f3d6fa88d912695b98f2f5f3ad85b403a20e2795
71 changes: 55 additions & 16 deletions integration/update_field_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,23 +508,39 @@ func TestUpdateFieldCompatRename(t *testing.T) {
//"Nested": {
// update: bson.D{{"$rename", bson.D{{"v", bson.D{{"array", ""}}}}}},
//},
"DotDocument": {
update: bson.D{{"$rename", bson.D{{"v.foo", "v.boo"}}}},
},
"DotDocumentMove": {
update: bson.D{{"$rename", bson.D{{"v.foo", "boo"}}}},
},
"DotDocumentDuplicate": {
update: bson.D{{"$rename", bson.D{{"v.foo", "v.array"}}}},
},
//"DotDocumentNonExisting": {
// update: bson.D{{"$rename", bson.D{{"foo.bar", ""}}}},
// resultType: emptyResult,
//},
//"DotArrayField": {
// update: bson.D{{"$rename", bson.D{{"v.array.0", ""}}}},
// skip: "https://github.com/FerretDB/FerretDB/issues/1242",
//},
"DotDocumentNonExisting": {
update: bson.D{{"$rename", bson.D{{"foo.bar", ""}}}},
resultType: emptyResult,
},
"DotArrayField": {
update: bson.D{{"$rename", bson.D{{"v.array.0", ""}}}},
resultType: emptyResult,
//skip: "https://github.com/FerretDB/FerretDB/issues/1242",
},

"Same": {
update: bson.D{{"$rename", bson.D{{"v", "v"}}}},
},

"MultipleDiff": {
update: bson.D{{"$rename", bson.D{{"v.foo", "v.bar"}, {"v.42", "v.43"}}}},
},

"Multiple": {
update: bson.D{{"$rename", bson.D{{"v", "foo"}, {"foo", "bar"}}}},
},
"MultipleSame": {
update: bson.D{{"$rename", bson.D{{"v", "foo"}, {"foo", "v"}}}},
},
"MultipleSame2": {
update: bson.D{{"$rename", bson.D{{"v", "foo"}, {"v", "boo"}}}},
},
//"DotArrayNonExisting": {
// update: bson.D{{"$rename", bson.D{{"foo.0.baz", int32(1)}}}},
// resultType: emptyResult,
Expand All @@ -533,14 +549,37 @@ func TestUpdateFieldCompatRename(t *testing.T) {
// update: bson.D{{"$rename", bson.D{{"v", ""}, {"v", ""}}}},
// resultType: emptyResult,
//},
//"Empty": {
// update: bson.D{{"$rename", bson.D{}}},
// resultType: emptyResult,
//},
"Empty": {
update: bson.D{{"$rename", bson.D{}}},
resultType: emptyResult,
},
//"DocumentField": {
// update: bson.D{{"$rename", bson.D{{"foo", ""}}}},
// resultType: emptyResult,
// resulttype: emptyresult,
//},

"FieldDoc": {
update: bson.D{{"$rename", bson.D{{"name", primitive.D{}}}}},
},

"FieldArray": {
update: bson.D{{"$rename", bson.D{{"name", primitive.A{}}}}},
},
"FieldNaN": {
update: bson.D{{"$rename", bson.D{{"name", math.NaN()}}}},
},
"FieldNil": {
update: bson.D{{"$rename", bson.D{{"name", nil}}}},
},
"RenameString": {
update: bson.D{{"$rename", "string"}},
},
"RenameNil": {
update: bson.D{{"$rename", nil}},
},
"RenameDoc": {
update: bson.D{{"$rename", primitive.D{}}},
},
}

testUpdateCompat(t, testCases)
Expand Down
28 changes: 3 additions & 25 deletions internal/handlers/common/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,49 +257,27 @@ func processRenameFieldExpression(doc *types.Document, updateV any) (bool, error
continue
w84thesun marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO: test multiple keys, one invalid
if key == "" || renameRawValue == "" {
return changed, NewWriteErrorMsg(ErrEmptyName, "An empty update path is not valid.")
}

path := types.NewPathFromString(key)
//if !doc.HasByPath(keyPath) {
// return changed,
//}

//if path.Len() > 1 && !doc.HasByPath(path) {
// return changed, NewWriteErrorMsg(1, fmt.Sprintf(
// "cannot use the part (%s of %s) to traverse the element (%v)",
// path.Slice()[:1],
// path.Slice(),
// ),
// )
//}

renameValue, ok := renameRawValue.(string)
if !ok {
// TODO: test for invalid _id values
panic(1)
return changed, NewWriteErrorMsg(2, fmt.Sprintf("the 'to' field for $rename must be a string: %s: %v", key, renameRawValue))
}

renamePath := types.NewPathFromString(renameValue)

//if _, err := doc.Get(key); err != nil {
// return changed, nil
//}

// TODO: test for non existing key
// TODO: check if key exists
// [{"foo":"aaa"},{"boo":"aaa"}]
// eg. { $rename: {"foo","boo"}}

var val any

if path.Len() > 1 {
pathVal, err := doc.GetByPath(path)
if err != nil {
if strings.Contains(err.Error(), "key not found") {
return changed, nil
continue
//return changed, nil
}
//TODO: differentiate between key not found and can't access <type> by path
return changed, NewWriteErrorMsg(28, err.Error())
Expand Down