Make UnstructuredContent return contents without mutating the source

This commit is contained in:
Mikhail Mazurskiy 2018-04-03 22:13:29 +10:00
parent 22440e1576
commit e82e8b4e89
No known key found for this signature in database
GPG Key ID: 93551ECC96E2F568
8 changed files with 49 additions and 17 deletions

View File

@ -2133,14 +2133,14 @@ URL: http://localhost
"name": "MyName",
"namespace": "MyNamespace",
"creationTimestamp": "2017-04-01T00:00:00Z",
"resourceVersion": 123,
"resourceVersion": int64(123),
"uid": "00000000-0000-0000-0000-000000000001",
"dummy3": "present",
},
"items": []interface{}{
map[string]interface{}{
"itemBool": true,
"itemInt": 42,
"itemInt": int64(42),
},
},
"url": "http://localhost",

View File

@ -11,6 +11,7 @@ go_test(
srcs = [
"helpers_test.go",
"unstructured_list_test.go",
"unstructured_test.go",
],
embed = [":go_default_library"],
deps = [

View File

@ -82,7 +82,7 @@ func (obj *Unstructured) EachListItem(fn func(runtime.Object) error) error {
func (obj *Unstructured) UnstructuredContent() map[string]interface{} {
if obj.Object == nil {
obj.Object = make(map[string]interface{})
return make(map[string]interface{})
}
return obj.Object
}

View File

@ -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
// the Object field. Items always overwrites overlay. Changing "items" in the
// 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.
// the Object field. Items always overwrites overlay.
func (u *UnstructuredList) UnstructuredContent() map[string]interface{} {
out := u.Object
if out == nil {
out = make(map[string]interface{})
out := make(map[string]interface{}, len(u.Object)+1)
// 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))
for i, item := range u.Items {
items[i] = item.Object
items[i] = item.UnstructuredContent()
}
out["items"] = items
return out

View File

@ -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)
}

View File

@ -411,8 +411,7 @@ func (c *unstructuredConverter) ToUnstructured(obj interface{}) (map[string]inte
var u map[string]interface{}
var err error
if unstr, ok := obj.(Unstructured); ok {
// UnstructuredContent() mutates the object so we need to make a copy first
u = unstr.DeepCopyObject().(Unstructured).UnstructuredContent()
u = unstr.UnstructuredContent()
} else {
t := reflect.TypeOf(obj)
value := reflect.ValueOf(obj)

View File

@ -234,9 +234,9 @@ type Object interface {
// to JSON allowed.
type Unstructured interface {
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
// and from JSON.
// and from JSON. SetUnstructuredContent should be used to mutate the contents.
UnstructuredContent() map[string]interface{}
// SetUnstructuredContent updates the object content to match the provided map.
SetUnstructuredContent(map[string]interface{})

View File

@ -263,7 +263,7 @@ func (obj *Unstructured) EachListItem(fn func(runtime.Object) error) error {
func (obj *Unstructured) UnstructuredContent() map[string]interface{} {
if obj.Object == nil {
obj.Object = make(map[string]interface{})
return make(map[string]interface{})
}
return obj.Object
}