jsonpath filter: allow intermediate missing keys

In jsonpath, when filtering a list, if allowMissingKeys is true, skip
over any items that are missing an intermediate key in the filter,
instead of returning a confusing error.

For example, if the filter is

{.items[?(@.metadata.annotations.foo=="bar")].metadata.name}

we should return all items where metadata.annotations.foo == bar, but if
an item in the list does not have metadata, metadata.annotations, or
metadata.annotations.foo, skip it instead of erroring.

Kubernetes-commit: e6f97d514d83fc2614d1ad4e18de0b318cc81653
This commit is contained in:
Andy Goldstein
2017-06-21 11:23:25 -04:00
committed by Kubernetes Publisher
parent 812b4cbd4b
commit 75943a8927
2 changed files with 132 additions and 45 deletions

View File

@@ -455,7 +455,10 @@ func (j *JSONPath) evalFilter(input []reflect.Value, node *FilterNode) ([]reflec
}
var left, right interface{}
if len(lefts) != 1 {
switch {
case len(lefts) == 0:
continue
case len(lefts) > 1:
return input, fmt.Errorf("can only compare one element at a time")
}
left = lefts[0].Interface()
@@ -464,7 +467,10 @@ func (j *JSONPath) evalFilter(input []reflect.Value, node *FilterNode) ([]reflec
if err != nil {
return input, err
}
if len(rights) != 1 {
switch {
case len(rights) == 0:
continue
case len(rights) > 1:
return input, fmt.Errorf("can only compare one element at a time")
}
right = rights[0].Interface()