Unverified Commit 1fd45cfb authored by k8s-ci-robot's avatar k8s-ci-robot Committed by GitHub

Merge pull request #71272 from sttts/sttts-unstructured-accessor-intermediate-not-found

unstructured: return not-found if intermediate path is not found
parents a08d89c0 52b1718a
......@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment