Skip to content

Commit

Permalink
Merge pull request #671 from crossplane/backport-666-to-release-1.15
Browse files Browse the repository at this point in the history
  • Loading branch information
phisco authored Feb 21, 2024
2 parents fe82488 + 94b26aa commit 8c2a42a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
27 changes: 16 additions & 11 deletions pkg/fieldpath/paved.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func (p *Paved) getValue(s Segments) (any, error) {
return getValueFromInterface(p.object, s)
}

func getValueFromInterface(it any, s Segments) (any, error) {
func getValueFromInterface(it any, s Segments) (any, error) { //nolint:gocyclo // See note below.
// Although the complexity of the function may seem high, in fact the same
// operation is performed in different cases.
for i, current := range s {
final := i == len(s)-1
switch current.Type {
Expand All @@ -129,18 +131,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("%s: expected map, got nil", 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[]\""),
},
},
"NilParent": {
reason: "Request for a path with a nil parent value",
path: "spec.containers[*].name",
data: []byte(`{"spec":{"containers": null}}`),
want: want{
err: errNotFound{errors.Errorf("%s: expected map, got nil", "spec.containers")},
},
},
}

for name, tc := range cases {
Expand Down

0 comments on commit 8c2a42a

Please sign in to comment.