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

Added support for arrays at the root #15

Merged
merged 2 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions jsonpatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, "", []Operation{})
}

// Returns true if the values matches (must be json types)
Expand Down
5 changes: 3 additions & 2 deletions jsonpatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,6 @@ var (
}`
)


var (
oldNestedObj = `{
"apiVersion": "kubedb.com/v1alpha1",
Expand Down Expand Up @@ -738,7 +737,6 @@ var (
}`
)


func TestCreatePatch(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -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)
Expand Down