From 52b0c4b73c0ecda0a25edd0a193c29923dfb338b Mon Sep 17 00:00:00 2001 From: Emil Nikolov Date: Fri, 17 May 2019 16:42:52 +0200 Subject: [PATCH 1/2] added support for arrays at the root --- jsonpatch.go | 6 +++--- jsonpatch_test.go | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/jsonpatch.go b/jsonpatch.go index 3e69894..99a9d7d 100644 --- a/jsonpatch.go +++ b/jsonpatch.go @@ -58,8 +58,8 @@ func NewPatch(operation, path string, value interface{}) Operation { // // An error will be returned if any of the two documents are invalid. func CreatePatch(a, b []byte) ([]Operation, error) { - aI := map[string]interface{}{} - bI := map[string]interface{}{} + var aI interface{} + var bI interface{} err := json.Unmarshal(a, &aI) if err != nil { return nil, errBadJSONDoc @@ -68,7 +68,7 @@ func CreatePatch(a, b []byte) ([]Operation, error) { if err != nil { return nil, errBadJSONDoc } - return diff(aI, bI, "", []Operation{}) + return handleValues(aI, bI, "", []JsonPatchOperation{}) } // Returns true if the values matches (must be json types) diff --git a/jsonpatch_test.go b/jsonpatch_test.go index 18c58be..5cc9bd3 100644 --- a/jsonpatch_test.go +++ b/jsonpatch_test.go @@ -704,7 +704,6 @@ var ( }` ) - var ( oldNestedObj = `{ "apiVersion": "kubedb.com/v1alpha1", @@ -738,7 +737,6 @@ var ( }` ) - func TestCreatePatch(t *testing.T) { cases := []struct { name string @@ -779,7 +777,10 @@ func TestCreatePatch(t *testing.T) { {"Kubernetes:Annotations", oldDeployment, newDeployment}, // crd with nested object {"Nested Member Object", oldNestedObj, newNestedObj}, + {"Array at root", `[{"asdf":"qwerty"}]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`}, + {"Empty array at root", `[]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`}, } + for _, c := range cases { t.Run(c.name+"[src->dst]", func(t *testing.T) { check(t, c.src, c.dst) From c9dd2856c83f9e5bf276e5f99b1686dbe4aeed3b Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Tue, 25 Jun 2019 03:18:24 -0700 Subject: [PATCH 2/2] Update jsonpatch.go --- jsonpatch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonpatch.go b/jsonpatch.go index 99a9d7d..7f84a07 100644 --- a/jsonpatch.go +++ b/jsonpatch.go @@ -68,7 +68,7 @@ func CreatePatch(a, b []byte) ([]Operation, error) { if err != nil { return nil, errBadJSONDoc } - return handleValues(aI, bI, "", []JsonPatchOperation{}) + return handleValues(aI, bI, "", []Operation{}) } // Returns true if the values matches (must be json types)