Merge pull request #43096 from deads2k/update-patch

Automatic merge from submit-queue

vendor: Update github.com/evanphx/json-patch

Regenerated files for https://github.com/kubernetes/kubernetes/pull/40387


Fixes #40218
pull/6/head
Kubernetes Submit Queue 2017-03-14 19:44:05 -07:00 committed by GitHub
commit a19210f418
7 changed files with 56 additions and 29 deletions

2
Godeps/Godeps.json generated
View File

@ -960,7 +960,7 @@
},
{
"ImportPath": "github.com/evanphx/json-patch",
"Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de"
"Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379"
},
{
"ImportPath": "github.com/exponent-io/jsonpath",

View File

@ -40,7 +40,7 @@
},
{
"ImportPath": "github.com/evanphx/json-patch",
"Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de"
"Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379"
},
{
"ImportPath": "github.com/ghodss/yaml",

View File

@ -312,7 +312,7 @@
},
{
"ImportPath": "github.com/evanphx/json-patch",
"Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de"
"Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379"
},
{
"ImportPath": "github.com/ghodss/yaml",

View File

@ -116,7 +116,7 @@
},
{
"ImportPath": "github.com/evanphx/json-patch",
"Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de"
"Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379"
},
{
"ImportPath": "github.com/ghodss/yaml",

View File

@ -108,7 +108,7 @@
},
{
"ImportPath": "github.com/evanphx/json-patch",
"Rev": "465937c80b3c07a7c7ad20cc934898646a91c1de"
"Rev": "ba18e35c5c1b36ef6334cad706eb681153d2d379"
},
{
"ImportPath": "github.com/ghodss/yaml",

View File

@ -7,7 +7,7 @@ import (
"strings"
)
func merge(cur, patch *lazyNode) *lazyNode {
func merge(cur, patch *lazyNode, mergeMerge bool) *lazyNode {
curDoc, err := cur.intoDoc()
if err != nil {
@ -21,16 +21,20 @@ func merge(cur, patch *lazyNode) *lazyNode {
return patch
}
mergeDocs(curDoc, patchDoc)
mergeDocs(curDoc, patchDoc, mergeMerge)
return cur
}
func mergeDocs(doc, patch *partialDoc) {
func mergeDocs(doc, patch *partialDoc, mergeMerge bool) {
for k, v := range *patch {
k := decodePatchKey(k)
if v == nil {
delete(*doc, k)
if mergeMerge {
(*doc)[k] = nil
} else {
delete(*doc, k)
}
} else {
cur, ok := (*doc)[k]
@ -38,7 +42,7 @@ func mergeDocs(doc, patch *partialDoc) {
pruneNulls(v)
(*doc)[k] = v
} else {
(*doc)[k] = merge(cur, v)
(*doc)[k] = merge(cur, v, mergeMerge)
}
}
}
@ -88,8 +92,19 @@ func pruneAryNulls(ary *partialArray) *partialArray {
var errBadJSONDoc = fmt.Errorf("Invalid JSON Document")
var errBadJSONPatch = fmt.Errorf("Invalid JSON Patch")
// MergeMergePatches merges two merge patches together, such that
// applying this resulting merged merge patch to a document yields the same
// as merging each merge patch to the document in succession.
func MergeMergePatches(patch1Data, patch2Data []byte) ([]byte, error) {
return doMergePatch(patch1Data, patch2Data, true)
}
// MergePatch merges the patchData into the docData.
func MergePatch(docData, patchData []byte) ([]byte, error) {
return doMergePatch(docData, patchData, false)
}
func doMergePatch(docData, patchData []byte, mergeMerge bool) ([]byte, error) {
doc := &partialDoc{}
docErr := json.Unmarshal(docData, doc)
@ -117,7 +132,11 @@ func MergePatch(docData, patchData []byte) ([]byte, error) {
if docErr != nil || patchErr != nil {
// Not an error, just not a doc, so we turn straight into the patch
if patchErr == nil {
doc = pruneDocNulls(patch)
if mergeMerge {
doc = patch
} else {
doc = pruneDocNulls(patch)
}
} else {
patchAry := &partialArray{}
patchErr = json.Unmarshal(patchData, patchAry)
@ -137,7 +156,7 @@ func MergePatch(docData, patchData []byte) ([]byte, error) {
return out, nil
}
} else {
mergeDocs(doc, patch)
mergeDocs(doc, patch, mergeMerge)
}
return json.Marshal(doc)

View File

@ -257,11 +257,15 @@ Loop:
return false
}
func findObject(pd *partialDoc, path string) (container, string) {
doc := container(pd)
func findObject(pd *container, path string) (container, string) {
doc := *pd
split := strings.Split(path, "/")
if len(split) < 2 {
return nil, ""
}
parts := split[1 : len(split)-1]
key := split[len(split)-1]
@ -311,7 +315,7 @@ func (d *partialDoc) get(key string) (*lazyNode, error) {
func (d *partialDoc) remove(key string) error {
_, ok := (*d)[key]
if !ok {
return fmt.Errorf("Unable to remove nonexistant key: %s", key)
return fmt.Errorf("Unable to remove nonexistent key: %s", key)
}
delete(*d, key)
@ -341,7 +345,7 @@ func (d *partialArray) set(key string, val *lazyNode) error {
copy(ary, cur)
if idx >= len(ary) {
fmt.Printf("huh?: %#v[%d] %s, %s\n", ary, idx)
return fmt.Errorf("Unable to access invalid index: %d", idx)
}
ary[idx] = val
@ -409,7 +413,7 @@ func (d *partialArray) remove(key string) error {
}
func (p Patch) add(doc *partialDoc, op operation) error {
func (p Patch) add(doc *container, op operation) error {
path := op.path()
con, key := findObject(doc, path)
@ -421,7 +425,7 @@ func (p Patch) add(doc *partialDoc, op operation) error {
return con.add(key, op.value())
}
func (p Patch) remove(doc *partialDoc, op operation) error {
func (p Patch) remove(doc *container, op operation) error {
path := op.path()
con, key := findObject(doc, path)
@ -433,7 +437,7 @@ func (p Patch) remove(doc *partialDoc, op operation) error {
return con.remove(key)
}
func (p Patch) replace(doc *partialDoc, op operation) error {
func (p Patch) replace(doc *container, op operation) error {
path := op.path()
con, key := findObject(doc, path)
@ -445,7 +449,7 @@ func (p Patch) replace(doc *partialDoc, op operation) error {
return con.set(key, op.value())
}
func (p Patch) move(doc *partialDoc, op operation) error {
func (p Patch) move(doc *container, op operation) error {
from := op.from()
con, key := findObject(doc, from)
@ -475,7 +479,7 @@ func (p Patch) move(doc *partialDoc, op operation) error {
return con.set(key, val)
}
func (p Patch) test(doc *partialDoc, op operation) error {
func (p Patch) test(doc *container, op operation) error {
path := op.path()
con, key := findObject(doc, path)
@ -493,9 +497,8 @@ func (p Patch) test(doc *partialDoc, op operation) error {
if val == nil {
if op.value().raw == nil {
return nil
} else {
return fmt.Errorf("Testing value %s failed", path)
}
return fmt.Errorf("Testing value %s failed", path)
}
if val.equal(op.value()) {
@ -540,7 +543,12 @@ func (p Patch) Apply(doc []byte) ([]byte, error) {
// ApplyIndent mutates a JSON document according to the patch, and returns the new
// document indented.
func (p Patch) ApplyIndent(doc []byte, indent string) ([]byte, error) {
pd := &partialDoc{}
var pd container
if doc[0] == '[' {
pd = &partialArray{}
} else {
pd = &partialDoc{}
}
err := json.Unmarshal(doc, pd)
@ -553,15 +561,15 @@ func (p Patch) ApplyIndent(doc []byte, indent string) ([]byte, error) {
for _, op := range p {
switch op.kind() {
case "add":
err = p.add(pd, op)
err = p.add(&pd, op)
case "remove":
err = p.remove(pd, op)
err = p.remove(&pd, op)
case "replace":
err = p.replace(pd, op)
err = p.replace(&pd, op)
case "move":
err = p.move(pd, op)
err = p.move(&pd, op)
case "test":
err = p.test(pd, op)
err = p.test(&pd, op)
default:
err = fmt.Errorf("Unexpected kind: %s", op.kind())
}