mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #78539 from pmorie/unstructured-helpers-godoc
Unstructed helpers: document lack of slice syntax
This commit is contained in:
commit
fee0c4bd40
@ -32,6 +32,9 @@ import (
|
|||||||
// NestedFieldCopy returns a deep copy of the value of a nested field.
|
// NestedFieldCopy returns a deep copy of the value of a nested field.
|
||||||
// Returns false if the value is missing.
|
// Returns false if the value is missing.
|
||||||
// No error is returned for a nil field.
|
// No error is returned for a nil field.
|
||||||
|
//
|
||||||
|
// Note: fields passed to this function are treated as keys within the passed
|
||||||
|
// object; no array/slice syntax is supported.
|
||||||
func NestedFieldCopy(obj map[string]interface{}, fields ...string) (interface{}, bool, error) {
|
func NestedFieldCopy(obj map[string]interface{}, fields ...string) (interface{}, bool, error) {
|
||||||
val, found, err := NestedFieldNoCopy(obj, fields...)
|
val, found, err := NestedFieldNoCopy(obj, fields...)
|
||||||
if !found || err != nil {
|
if !found || err != nil {
|
||||||
@ -43,6 +46,9 @@ func NestedFieldCopy(obj map[string]interface{}, fields ...string) (interface{},
|
|||||||
// NestedFieldNoCopy returns a reference to a nested field.
|
// NestedFieldNoCopy returns a reference to a nested field.
|
||||||
// Returns false if value is not found and an error if unable
|
// Returns false if value is not found and an error if unable
|
||||||
// to traverse obj.
|
// to traverse obj.
|
||||||
|
//
|
||||||
|
// Note: fields passed to this function are treated as keys within the passed
|
||||||
|
// object; no array/slice syntax is supported.
|
||||||
func NestedFieldNoCopy(obj map[string]interface{}, fields ...string) (interface{}, bool, error) {
|
func NestedFieldNoCopy(obj map[string]interface{}, fields ...string) (interface{}, bool, error) {
|
||||||
var val interface{} = obj
|
var val interface{} = obj
|
||||||
|
|
||||||
|
@ -67,6 +67,11 @@ func TestNestedFieldNoCopy(t *testing.T) {
|
|||||||
"b": target,
|
"b": target,
|
||||||
"c": nil,
|
"c": nil,
|
||||||
"d": []interface{}{"foo"},
|
"d": []interface{}{"foo"},
|
||||||
|
"e": []interface{}{
|
||||||
|
map[string]interface{}{
|
||||||
|
"f": "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,13 +96,13 @@ func TestNestedFieldNoCopy(t *testing.T) {
|
|||||||
assert.Nil(t, res)
|
assert.Nil(t, res)
|
||||||
|
|
||||||
// case 4: field does not exist
|
// case 4: field does not exist
|
||||||
res, exists, err = NestedFieldNoCopy(obj, "a", "e")
|
res, exists, err = NestedFieldNoCopy(obj, "a", "g")
|
||||||
assert.False(t, exists)
|
assert.False(t, exists)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Nil(t, res)
|
assert.Nil(t, res)
|
||||||
|
|
||||||
// case 5: intermediate field does not exist
|
// case 5: intermediate field does not exist
|
||||||
res, exists, err = NestedFieldNoCopy(obj, "a", "e", "f")
|
res, exists, err = NestedFieldNoCopy(obj, "a", "g", "f")
|
||||||
assert.False(t, exists)
|
assert.False(t, exists)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Nil(t, res)
|
assert.Nil(t, res)
|
||||||
@ -108,6 +113,13 @@ func TestNestedFieldNoCopy(t *testing.T) {
|
|||||||
assert.False(t, exists)
|
assert.False(t, exists)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Nil(t, res)
|
assert.Nil(t, res)
|
||||||
|
|
||||||
|
// case 7: array/slice syntax is not supported
|
||||||
|
// (background: users may expect this to be supported)
|
||||||
|
res, exists, err = NestedFieldNoCopy(obj, "a", "e[0]")
|
||||||
|
assert.False(t, exists)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Nil(t, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNestedFieldCopy(t *testing.T) {
|
func TestNestedFieldCopy(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user