move HasConflicts to shareable location

This commit is contained in:
deads2k
2015-09-30 10:39:08 -04:00
parent 41e2a4c40f
commit 05e84fafbe
5 changed files with 116 additions and 157 deletions

View File

@@ -19,11 +19,11 @@ package jsonmerge
import (
"encoding/json"
"fmt"
"reflect"
"github.com/evanphx/json-patch"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/util/strategicpatch"
"k8s.io/kubernetes/pkg/util/yaml"
)
@@ -161,9 +161,14 @@ func (d *Delta) Apply(latest []byte) ([]byte, error) {
}
glog.V(6).Infof("Testing for conflict between:\n%s\n%s", string(d.edit), string(changes))
if hasConflicts(diff1, diff2) {
hasConflicts, err := strategicpatch.HasConflicts(diff1, diff2)
if err != nil {
return nil, err
}
if hasConflicts {
return nil, ErrConflict
}
return jsonpatch.MergePatch(base, d.edit)
}
@@ -183,45 +188,6 @@ func IsPreconditionFailed(err error) bool {
var ErrPreconditionFailed = fmt.Errorf("a precondition failed")
var ErrConflict = fmt.Errorf("changes are in conflict")
// hasConflicts returns true if the left and right JSON interface objects overlap with
// different values in any key. The code will panic if an unrecognized type is passed
// (anything not returned by a JSON decode). All keys are required to be strings.
func hasConflicts(left, right interface{}) bool {
switch typedLeft := left.(type) {
case map[string]interface{}:
switch typedRight := right.(type) {
case map[string]interface{}:
for key, leftValue := range typedLeft {
if rightValue, ok := typedRight[key]; ok && hasConflicts(leftValue, rightValue) {
return true
}
}
return false
default:
return true
}
case []interface{}:
switch typedRight := right.(type) {
case []interface{}:
if len(typedLeft) != len(typedRight) {
return true
}
for i := range typedLeft {
if hasConflicts(typedLeft[i], typedRight[i]) {
return true
}
}
return false
default:
return true
}
case string, float64, bool, int, int64, nil:
return !reflect.DeepEqual(left, right)
default:
panic(fmt.Sprintf("unknown type: %v", reflect.TypeOf(left)))
}
}
func (d *Delta) Edit() []byte {
return d.edit
}

View File

@@ -1,75 +0,0 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package jsonmerge
import (
"testing"
)
func TestHasConflicts(t *testing.T) {
testCases := []struct {
A interface{}
B interface{}
Ret bool
}{
{A: "hello", B: "hello", Ret: false}, // 0
{A: "hello", B: "hell", Ret: true},
{A: "hello", B: nil, Ret: true},
{A: "hello", B: 1, Ret: true},
{A: "hello", B: float64(1.0), Ret: true},
{A: "hello", B: false, Ret: true},
{A: "hello", B: []interface{}{}, Ret: true}, // 6
{A: []interface{}{1}, B: []interface{}{}, Ret: true},
{A: []interface{}{}, B: []interface{}{}, Ret: false},
{A: []interface{}{1}, B: []interface{}{1}, Ret: false},
{A: map[string]interface{}{}, B: []interface{}{1}, Ret: true},
{A: map[string]interface{}{}, B: map[string]interface{}{"a": 1}, Ret: false}, // 11
{A: map[string]interface{}{"a": 1}, B: map[string]interface{}{"a": 1}, Ret: false},
{A: map[string]interface{}{"a": 1}, B: map[string]interface{}{"a": 2}, Ret: true},
{A: map[string]interface{}{"a": 1}, B: map[string]interface{}{"b": 2}, Ret: false},
{ // 15
A: map[string]interface{}{"a": []interface{}{1}},
B: map[string]interface{}{"a": []interface{}{1}},
Ret: false,
},
{
A: map[string]interface{}{"a": []interface{}{1}},
B: map[string]interface{}{"a": []interface{}{}},
Ret: true,
},
{
A: map[string]interface{}{"a": []interface{}{1}},
B: map[string]interface{}{"a": 1},
Ret: true,
},
}
for i, testCase := range testCases {
out := hasConflicts(testCase.A, testCase.B)
if out != testCase.Ret {
t.Errorf("%d: expected %t got %t", i, testCase.Ret, out)
continue
}
out = hasConflicts(testCase.B, testCase.A)
if out != testCase.Ret {
t.Errorf("%d: expected reversed %t got %t", i, testCase.Ret, out)
}
}
}