mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #71272 from sttts/sttts-unstructured-accessor-intermediate-not-found
unstructured: return not-found if intermediate path is not found
This commit is contained in:
commit
1fd45cfb24
@ -47,6 +47,9 @@ func NestedFieldNoCopy(obj map[string]interface{}, fields ...string) (interface{
|
||||
var val interface{} = obj
|
||||
|
||||
for i, field := range fields {
|
||||
if val == nil {
|
||||
return nil, false, nil
|
||||
}
|
||||
if m, ok := val.(map[string]interface{}); ok {
|
||||
val, ok = m[field]
|
||||
if !ok {
|
||||
|
@ -95,6 +95,19 @@ func TestNestedFieldNoCopy(t *testing.T) {
|
||||
assert.False(t, exists)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, res)
|
||||
|
||||
// case 5: intermediate field does not exist
|
||||
res, exists, err = NestedFieldNoCopy(obj, "a", "e", "f")
|
||||
assert.False(t, exists)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, res)
|
||||
|
||||
// case 6: intermediate field is null
|
||||
// (background: happens easily in YAML)
|
||||
res, exists, err = NestedFieldNoCopy(obj, "a", "c", "f")
|
||||
assert.False(t, exists)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
func TestNestedFieldCopy(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user