mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-14 14:23:37 +00:00
Make UnstructuredContent return contents without mutating the source
This commit is contained in:
parent
22440e1576
commit
e82e8b4e89
@ -2133,14 +2133,14 @@ URL: http://localhost
|
|||||||
"name": "MyName",
|
"name": "MyName",
|
||||||
"namespace": "MyNamespace",
|
"namespace": "MyNamespace",
|
||||||
"creationTimestamp": "2017-04-01T00:00:00Z",
|
"creationTimestamp": "2017-04-01T00:00:00Z",
|
||||||
"resourceVersion": 123,
|
"resourceVersion": int64(123),
|
||||||
"uid": "00000000-0000-0000-0000-000000000001",
|
"uid": "00000000-0000-0000-0000-000000000001",
|
||||||
"dummy3": "present",
|
"dummy3": "present",
|
||||||
},
|
},
|
||||||
"items": []interface{}{
|
"items": []interface{}{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"itemBool": true,
|
"itemBool": true,
|
||||||
"itemInt": 42,
|
"itemInt": int64(42),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"url": "http://localhost",
|
"url": "http://localhost",
|
||||||
|
@ -11,6 +11,7 @@ go_test(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"helpers_test.go",
|
"helpers_test.go",
|
||||||
"unstructured_list_test.go",
|
"unstructured_list_test.go",
|
||||||
|
"unstructured_test.go",
|
||||||
],
|
],
|
||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
deps = [
|
deps = [
|
||||||
|
@ -82,7 +82,7 @@ func (obj *Unstructured) EachListItem(fn func(runtime.Object) error) error {
|
|||||||
|
|
||||||
func (obj *Unstructured) UnstructuredContent() map[string]interface{} {
|
func (obj *Unstructured) UnstructuredContent() map[string]interface{} {
|
||||||
if obj.Object == nil {
|
if obj.Object == nil {
|
||||||
obj.Object = make(map[string]interface{})
|
return make(map[string]interface{})
|
||||||
}
|
}
|
||||||
return obj.Object
|
return obj.Object
|
||||||
}
|
}
|
||||||
|
@ -53,19 +53,21 @@ func (u *UnstructuredList) EachListItem(fn func(runtime.Object) error) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UnstructuredContent returns a map contain an overlay of the Items field onto
|
// UnstructuredContent returns a map contain an overlay of the Items field onto
|
||||||
// the Object field. Items always overwrites overlay. Changing "items" in the
|
// the Object field. Items always overwrites overlay.
|
||||||
// returned object will affect items in the underlying Items field, but changing
|
|
||||||
// the "items" slice itself will have no effect.
|
|
||||||
// TODO: expose SetUnstructuredContent on runtime.Unstructured that allows
|
|
||||||
// items to be changed.
|
|
||||||
func (u *UnstructuredList) UnstructuredContent() map[string]interface{} {
|
func (u *UnstructuredList) UnstructuredContent() map[string]interface{} {
|
||||||
out := u.Object
|
out := make(map[string]interface{}, len(u.Object)+1)
|
||||||
if out == nil {
|
|
||||||
out = make(map[string]interface{})
|
// shallow copy every property but items
|
||||||
|
for k, v := range u.Object {
|
||||||
|
if k == "items" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
items := make([]interface{}, len(u.Items))
|
items := make([]interface{}, len(u.Items))
|
||||||
for i, item := range u.Items {
|
for i, item := range u.Items {
|
||||||
items[i] = item.Object
|
items[i] = item.UnstructuredContent()
|
||||||
}
|
}
|
||||||
out["items"] = items
|
out["items"] = items
|
||||||
return out
|
return out
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 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.
|
||||||
|
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 unstructured
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNilUnstructuredContent(t *testing.T) {
|
||||||
|
var u Unstructured
|
||||||
|
content := u.UnstructuredContent()
|
||||||
|
expContent := make(map[string]interface{})
|
||||||
|
assert.EqualValues(t, expContent, content)
|
||||||
|
}
|
@ -411,8 +411,7 @@ func (c *unstructuredConverter) ToUnstructured(obj interface{}) (map[string]inte
|
|||||||
var u map[string]interface{}
|
var u map[string]interface{}
|
||||||
var err error
|
var err error
|
||||||
if unstr, ok := obj.(Unstructured); ok {
|
if unstr, ok := obj.(Unstructured); ok {
|
||||||
// UnstructuredContent() mutates the object so we need to make a copy first
|
u = unstr.UnstructuredContent()
|
||||||
u = unstr.DeepCopyObject().(Unstructured).UnstructuredContent()
|
|
||||||
} else {
|
} else {
|
||||||
t := reflect.TypeOf(obj)
|
t := reflect.TypeOf(obj)
|
||||||
value := reflect.ValueOf(obj)
|
value := reflect.ValueOf(obj)
|
||||||
|
@ -234,9 +234,9 @@ type Object interface {
|
|||||||
// to JSON allowed.
|
// to JSON allowed.
|
||||||
type Unstructured interface {
|
type Unstructured interface {
|
||||||
Object
|
Object
|
||||||
// UnstructuredContent returns a non-nil, mutable map of the contents of this object. Values may be
|
// UnstructuredContent returns a non-nil map with this object's contents. Values may be
|
||||||
// []interface{}, map[string]interface{}, or any primitive type. Contents are typically serialized to
|
// []interface{}, map[string]interface{}, or any primitive type. Contents are typically serialized to
|
||||||
// and from JSON.
|
// and from JSON. SetUnstructuredContent should be used to mutate the contents.
|
||||||
UnstructuredContent() map[string]interface{}
|
UnstructuredContent() map[string]interface{}
|
||||||
// SetUnstructuredContent updates the object content to match the provided map.
|
// SetUnstructuredContent updates the object content to match the provided map.
|
||||||
SetUnstructuredContent(map[string]interface{})
|
SetUnstructuredContent(map[string]interface{})
|
||||||
|
@ -263,7 +263,7 @@ func (obj *Unstructured) EachListItem(fn func(runtime.Object) error) error {
|
|||||||
|
|
||||||
func (obj *Unstructured) UnstructuredContent() map[string]interface{} {
|
func (obj *Unstructured) UnstructuredContent() map[string]interface{} {
|
||||||
if obj.Object == nil {
|
if obj.Object == nil {
|
||||||
obj.Object = make(map[string]interface{})
|
return make(map[string]interface{})
|
||||||
}
|
}
|
||||||
return obj.Object
|
return obj.Object
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user