diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index f5cdcbc1..9aba8058 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -448,11 +448,11 @@ }, { "ImportPath": "k8s.io/api", - "Rev": "7a95b569f5ff" + "Rev": "18d4659ab40b" }, { "ImportPath": "k8s.io/apimachinery", - "Rev": "8046191f3410" + "Rev": "1aed267868af" }, { "ImportPath": "k8s.io/gengo", diff --git a/go.mod b/go.mod index fb214c58..f4a10b02 100644 --- a/go.mod +++ b/go.mod @@ -26,14 +26,14 @@ require ( golang.org/x/net v0.0.0-20200707034311-ab3426394381 golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 golang.org/x/time v0.0.0-20191024005414-555d28b269f0 - k8s.io/api v0.0.0-20201102202211-7a95b569f5ff - k8s.io/apimachinery v0.0.0-20201102202032-8046191f3410 + k8s.io/api v0.0.0-20201103002210-18d4659ab40b + k8s.io/apimachinery v0.0.0-20201103082028-1aed267868af k8s.io/klog/v2 v2.2.0 k8s.io/utils v0.0.0-20200729134348-d5654de09c73 sigs.k8s.io/yaml v1.2.0 ) replace ( - k8s.io/api => k8s.io/api v0.0.0-20201102202211-7a95b569f5ff - k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20201102202032-8046191f3410 + k8s.io/api => k8s.io/api v0.0.0-20201103002210-18d4659ab40b + k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20201103082028-1aed267868af ) diff --git a/go.sum b/go.sum index d0ff1c2d..b2aade4e 100644 --- a/go.sum +++ b/go.sum @@ -335,8 +335,8 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= -k8s.io/api v0.0.0-20201102202211-7a95b569f5ff/go.mod h1:DvHvdeQUPLTohsRVYB2Fd49FYz7l0WWEnJBD4BrP9Pc= -k8s.io/apimachinery v0.0.0-20201102202032-8046191f3410/go.mod h1:6s3VNb000AUbBIxR7q3WHlbBwfpEGqIJsCG5gIX+0LI= +k8s.io/api v0.0.0-20201103002210-18d4659ab40b/go.mod h1:b+K6RbdbECmbVaw11+iMzmt6th7YpfdynGdh/HKUa54= +k8s.io/apimachinery v0.0.0-20201103082028-1aed267868af/go.mod h1:6s3VNb000AUbBIxR7q3WHlbBwfpEGqIJsCG5gIX+0LI= k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE= k8s.io/klog/v2 v2.2.0 h1:XRvcwJozkgZ1UQJmfMGpvRthQHOvihEhYtDfAaxMz/A= diff --git a/util/jsonpath/jsonpath.go b/util/jsonpath/jsonpath.go index 9740fe69..49ecd146 100644 --- a/util/jsonpath/jsonpath.go +++ b/util/jsonpath/jsonpath.go @@ -103,13 +103,23 @@ func (j *JSONPath) FindResults(data interface{}) ([][]reflect.Value, error) { if j.beginRange > 0 { j.beginRange-- j.inRange++ - for _, value := range results { + if len(results) > 0 { + for _, value := range results { + j.parser.Root.Nodes = nodes[i+1:] + nextResults, err := j.FindResults(value.Interface()) + if err != nil { + return nil, err + } + fullResult = append(fullResult, nextResults...) + } + } else { + // If the range has no results, we still need to process the nodes within the range + // so the position will advance to the end node j.parser.Root.Nodes = nodes[i+1:] - nextResults, err := j.FindResults(value.Interface()) + _, err := j.FindResults(nil) if err != nil { return nil, err } - fullResult = append(fullResult, nextResults...) } j.inRange-- diff --git a/util/jsonpath/jsonpath_test.go b/util/jsonpath/jsonpath_test.go index e15c4097..34a0f9e7 100644 --- a/util/jsonpath/jsonpath_test.go +++ b/util/jsonpath/jsonpath_test.go @@ -393,6 +393,21 @@ func TestKubernetes(t *testing.T) { testJSONPathSortOutput(randomPrintOrderTests, t) } +func TestEmptyRange(t *testing.T) { + var input = []byte(`{"items":[]}`) + var emptyList interface{} + err := json.Unmarshal(input, &emptyList) + if err != nil { + t.Error(err) + } + + tests := []jsonpathTest{ + {"empty range", `{range .items[*]}{.metadata.name}{end}`, &emptyList, "", false}, + {"empty nested range", `{range .items[*]}{.metadata.name}{":"}{range @.spec.containers[*]}{.name}{","}{end}{"+"}{end}`, &emptyList, "", false}, + } + testJSONPath(tests, true, t) +} + func TestNestedRanges(t *testing.T) { var input = []byte(`{ "items": [