Skip to content

Commit

Permalink
port over fix for testing/replacing a whole document to v4
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Oct 21, 2021
1 parent 5561fe0 commit 50fdc0b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
42 changes: 42 additions & 0 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,29 @@ func (p Patch) replace(doc *container, op Operation) error {
return errors.Wrapf(err, "replace operation failed to decode path")
}

if path == "" {
val := op.value()

if val.which == eRaw {
if !val.tryDoc() {
if !val.tryAry() {
return errors.Wrapf(err, "replace operation value must be object or array")
}
}
}

switch val.which {
case eAry:
*doc = &val.ary
case eDoc:
*doc = &val.doc
case eRaw:
return errors.Wrapf(err, "replace operation hit impossible case")
}

return nil
}

con, key := findObject(doc, path)

if con == nil {
Expand Down Expand Up @@ -634,6 +657,25 @@ func (p Patch) test(doc *container, op Operation) error {
return errors.Wrapf(err, "test operation failed to decode path")
}

if path == "" {
var self lazyNode

switch sv := (*doc).(type) {
case *partialDoc:
self.doc = *sv
self.which = eDoc
case *partialArray:
self.ary = *sv
self.which = eAry
}

if self.equal(op.value()) {
return nil
}

return errors.Wrapf(ErrTestFailed, "testing value %s failed", path)
}

con, key := findObject(doc, path)

if con == nil {
Expand Down
30 changes: 30 additions & 0 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,36 @@ var Cases = []Case{
{ "op": "copy", "path": "/foo/-", "from": "/foo/1" }]`,
fmt.Sprintf(`{ "foo": ["A", %q, %q, %q] }`, repeatedA(48), repeatedA(48), repeatedA(48)),
},
{
`{
"id": "00000000-0000-0000-0000-000000000000",
"parentID": "00000000-0000-0000-0000-000000000000"
}`,
`[
{
"op": "test",
"path": "",
"value": {
"id": "00000000-0000-0000-0000-000000000000",
"parentID": "00000000-0000-0000-0000-000000000000"
}
},
{
"op": "replace",
"path": "",
"value": {
"id": "759981e8-ec68-4639-a83e-513225914ecb",
"originalID": "bar",
"parentID": "00000000-0000-0000-0000-000000000000"
}
}
]`,
`{
"id" : "759981e8-ec68-4639-a83e-513225914ecb",
"originalID" : "bar",
"parentID" : "00000000-0000-0000-0000-000000000000"
}`,
},
}

type BadCase struct {
Expand Down

0 comments on commit 50fdc0b

Please sign in to comment.