update json-patch to pick up bug fixes

This commit is contained in:
Chao Xu
2019-01-28 17:42:01 -08:00
parent 5468b8d5cb
commit 404c057b67
15 changed files with 90 additions and 23 deletions

View File

@@ -14,6 +14,10 @@ const (
eAry
)
var SupportNegativeIndices bool = true
var ArraySizeLimit int = 0
var ArraySizeAdditionLimit int = 0
type lazyNode struct {
raw *json.RawMessage
doc partialDoc
@@ -61,6 +65,19 @@ func (n *lazyNode) UnmarshalJSON(data []byte) error {
return nil
}
func deepCopy(src *lazyNode) (*lazyNode, error) {
if src == nil {
return nil, nil
}
a, err := src.MarshalJSON()
if err != nil {
return nil, err
}
ra := make(json.RawMessage, len(a))
copy(ra, a)
return newLazyNode(&ra), nil
}
func (n *lazyNode) intoDoc() (*partialDoc, error) {
if n.which == eDoc {
return &n.doc, nil
@@ -354,10 +371,19 @@ func (d *partialArray) set(key string, val *lazyNode) error {
}
sz := len(*d)
if diff := idx + 1 - sz; ArraySizeAdditionLimit > 0 && diff > ArraySizeAdditionLimit {
return fmt.Errorf("Unable to increase the array size by %d, the limit is %d", diff, ArraySizeAdditionLimit)
}
if idx+1 > sz {
sz = idx + 1
}
if ArraySizeLimit > 0 && sz > ArraySizeLimit {
return fmt.Errorf("Unable to create array of size %d, limit is %d", sz, ArraySizeLimit)
}
ary := make([]*lazyNode, sz)
cur := *d
@@ -385,17 +411,29 @@ func (d *partialArray) add(key string, val *lazyNode) error {
return err
}
ary := make([]*lazyNode, len(*d)+1)
sz := len(*d) + 1
if ArraySizeLimit > 0 && sz > ArraySizeLimit {
return fmt.Errorf("Unable to create array of size %d, limit is %d", sz, ArraySizeLimit)
}
ary := make([]*lazyNode, sz)
cur := *d
if idx < -len(ary) || idx >= len(ary) {
if idx >= len(ary) {
return fmt.Errorf("Unable to access invalid index: %d", idx)
}
if idx < 0 {
idx += len(ary)
if SupportNegativeIndices {
if idx < -len(ary) {
return fmt.Errorf("Unable to access invalid index: %d", idx)
}
if idx < 0 {
idx += len(ary)
}
}
copy(ary[0:idx], cur[0:idx])
ary[idx] = val
copy(ary[idx+1:], cur[idx:])
@@ -426,11 +464,18 @@ func (d *partialArray) remove(key string) error {
cur := *d
if idx < -len(cur) || idx >= len(cur) {
return fmt.Errorf("Unable to remove invalid index: %d", idx)
if idx >= len(cur) {
return fmt.Errorf("Unable to access invalid index: %d", idx)
}
if idx < 0 {
idx += len(cur)
if SupportNegativeIndices {
if idx < -len(cur) {
return fmt.Errorf("Unable to access invalid index: %d", idx)
}
if idx < 0 {
idx += len(cur)
}
}
ary := make([]*lazyNode, len(cur)-1)
@@ -567,7 +612,12 @@ func (p Patch) copy(doc *container, op operation) error {
return fmt.Errorf("jsonpatch copy operation does not apply: doc is missing destination path: %s", path)
}
return con.set(key, val)
valCopy, err := deepCopy(val)
if err != nil {
return err
}
return con.add(key, valCopy)
}
// Equal indicates if 2 JSON documents have the same structural equality.