mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 15:37:24 +00:00
move HasConflicts to shareable location
This commit is contained in:
@@ -827,3 +827,44 @@ func sliceElementType(slices ...[]interface{}) (reflect.Type, error) {
|
||||
|
||||
return prevType, nil
|
||||
}
|
||||
|
||||
// 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.
|
||||
// Since patches of the same Type have congruent keys, this is valid for multiple patch
|
||||
// types.
|
||||
func HasConflicts(left, right interface{}) (bool, error) {
|
||||
switch typedLeft := left.(type) {
|
||||
case map[string]interface{}:
|
||||
switch typedRight := right.(type) {
|
||||
case map[string]interface{}:
|
||||
for key, leftValue := range typedLeft {
|
||||
rightValue, ok := typedRight[key]
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
return HasConflicts(leftValue, rightValue)
|
||||
}
|
||||
return false, nil
|
||||
default:
|
||||
return true, nil
|
||||
}
|
||||
case []interface{}:
|
||||
switch typedRight := right.(type) {
|
||||
case []interface{}:
|
||||
if len(typedLeft) != len(typedRight) {
|
||||
return true, nil
|
||||
}
|
||||
for i := range typedLeft {
|
||||
return HasConflicts(typedLeft[i], typedRight[i])
|
||||
}
|
||||
return false, nil
|
||||
default:
|
||||
return true, nil
|
||||
}
|
||||
case string, float64, bool, int, int64, nil:
|
||||
return !reflect.DeepEqual(left, right), nil
|
||||
default:
|
||||
return true, fmt.Errorf("unknown type: %v", reflect.TypeOf(left))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user