Skip to content

Commit

Permalink
Add a nil case to the getValueFromInterface function
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <[email protected]>
  • Loading branch information
sergenyalcin committed Feb 6, 2024
1 parent fa4ffff commit a739e1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
23 changes: 13 additions & 10 deletions pkg/fieldpath/paved.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,21 @@ func getValueFromInterface(it any, s Segments) (any, error) {
}
it = array[current.Index]
case SegmentField:
object, ok := it.(map[string]any)
if !ok {
switch object := it.(type) {
case map[string]any:
v, ok := object[current.Field]
if !ok {
return nil, errNotFound{errors.Errorf("%s: no such field", s[:i+1])}
}
if final {
return v, nil
}
it = object[current.Field]
case nil:
return nil, errNotFound{errors.Errorf("field %q is not found in the path", s[:i])}
default:
return nil, errors.Errorf("%s: not an object", s[:i])
}
v, ok := object[current.Field]
if !ok {
return nil, errNotFound{errors.Errorf("%s: no such field", s[:i+1])}
}
if final {
return v, nil
}
it = object[current.Field]
}
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/fieldpath/paved_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ func TestGetValue(t *testing.T) {
err: errors.Wrap(errors.New("unexpected ']' at position 5"), "cannot parse path \"spec[]\""),
},
},
"NilValue": {
reason: "Requesting for an object that has nil value",
path: "spec.containers[*].name",
data: []byte(`{"spec":{"containers": null}}`),
want: want{
err: errNotFound{errors.Errorf("field %q is not found in the path", "spec.containers")},
},
},
}

for name, tc := range cases {
Expand Down

0 comments on commit a739e1e

Please sign in to comment.