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:
k8s-ci-robot 2018-11-29 19:04:55 -08:00 committed by GitHub
commit 1fd45cfb24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -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 {

View File

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