mirror of https://github.com/k3s-io/k3s
Support typed nils; test empty Unstructured is not mutated
parent
d5fdac399c
commit
53e8fd04ec
|
@ -2133,14 +2133,14 @@ URL: http://localhost
|
|||
"name": "MyName",
|
||||
"namespace": "MyNamespace",
|
||||
"creationTimestamp": "2017-04-01T00:00:00Z",
|
||||
"resourceVersion": int64(123),
|
||||
"resourceVersion": 123,
|
||||
"uid": "00000000-0000-0000-0000-000000000001",
|
||||
"dummy3": "present",
|
||||
},
|
||||
"items": []interface{}{
|
||||
map[string]interface{}{
|
||||
"itemBool": true,
|
||||
"itemInt": int64(42),
|
||||
"itemInt": 42,
|
||||
},
|
||||
},
|
||||
"url": "http://localhost",
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
@ -24,7 +24,9 @@ import (
|
|||
|
||||
func TestNilUnstructuredContent(t *testing.T) {
|
||||
var u Unstructured
|
||||
uCopy := u.DeepCopy()
|
||||
content := u.UnstructuredContent()
|
||||
expContent := make(map[string]interface{})
|
||||
assert.EqualValues(t, expContent, content)
|
||||
assert.Equal(t, uCopy, &u)
|
||||
}
|
||||
|
|
|
@ -446,20 +446,31 @@ func DeepCopyJSON(x map[string]interface{}) map[string]interface{} {
|
|||
// DeepCopyJSONValue deep copies the passed value, assuming it is a valid JSON representation i.e. only contains
|
||||
// types produced by json.Unmarshal().
|
||||
func DeepCopyJSONValue(x interface{}) interface{} {
|
||||
if x == nil {
|
||||
return nil
|
||||
}
|
||||
switch x := x.(type) {
|
||||
case map[string]interface{}:
|
||||
if x == nil {
|
||||
// Typed nil - an interface{} that contains a type map[string]interface{} with a value of nil
|
||||
return x
|
||||
}
|
||||
clone := make(map[string]interface{}, len(x))
|
||||
for k, v := range x {
|
||||
clone[k] = DeepCopyJSONValue(v)
|
||||
}
|
||||
return clone
|
||||
case []interface{}:
|
||||
if x == nil {
|
||||
// Typed nil - an interface{} that contains a type []interface{} with a value of nil
|
||||
return x
|
||||
}
|
||||
clone := make([]interface{}, len(x))
|
||||
for i, v := range x {
|
||||
clone[i] = DeepCopyJSONValue(v)
|
||||
}
|
||||
return clone
|
||||
case string, int64, bool, float64, nil, encodingjson.Number:
|
||||
case string, int64, bool, float64, encodingjson.Number:
|
||||
return x
|
||||
default:
|
||||
panic(fmt.Errorf("cannot deep copy %T", x))
|
||||
|
|
Loading…
Reference in New Issue