Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jmespath reuse interpreter #24

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/jmoiron/jsonq v0.0.0-20150511023944-e874b168d07e
github.com/julienschmidt/httprouter v1.3.0
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23
github.com/kyverno/go-jmespath v0.4.1-0.20230705123211-d067dc3d6613
github.com/kyverno/go-jmespath v0.4.1-0.20230906134905-62fa64b71f91
github.com/lensesio/tableprinter v0.0.0-20201125135848-89e81fc956e7
github.com/notaryproject/notation-core-go v1.0.0
github.com/notaryproject/notation-go v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1022,8 +1022,8 @@ github.com/kunwardeep/paralleltest v1.0.2/go.mod h1:ZPqNm1fVHPllh5LPVujzbVz1JN2G
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/kyoh86/exportloopref v0.1.8/go.mod h1:1tUcJeiioIs7VWe5gcOObrux3lb66+sBqGZrRkMwPgg=
github.com/kyverno/go-jmespath v0.4.1-0.20230705123211-d067dc3d6613 h1:M0uOLuCAZydi/vZy7uvNhwaIge0HFMdfqQYOKw7kgnQ=
github.com/kyverno/go-jmespath v0.4.1-0.20230705123211-d067dc3d6613/go.mod h1:yzDHaKovQy16rjN4kFnjF+IdNoN4p1ndw+va6+B8zUU=
github.com/kyverno/go-jmespath v0.4.1-0.20230906134905-62fa64b71f91 h1:n63aowZk61f65e6OJxuql8BS/hCv8LZxz/eCO4+2NfM=
github.com/kyverno/go-jmespath v0.4.1-0.20230906134905-62fa64b71f91/go.mod h1:yzDHaKovQy16rjN4kFnjF+IdNoN4p1ndw+va6+B8zUU=
github.com/kyverno/go-jmespath/internal/testify v1.5.2-0.20230630133209-945021c749d9 h1:lL311dF3a2aeNibJj8v+uhFU3XkvRHZmCtAdSPOrQYY=
github.com/kyverno/go-jmespath/internal/testify v1.5.2-0.20230630133209-945021c749d9/go.mod h1:XRxUGHIiCy1WYma1CdfdO1WOhIe8dLPTENaZr5D1ex4=
github.com/ldez/gomoddirectives v0.2.1/go.mod h1:sGicqkRgBOg//JfpXwkB9Hj0X5RyJ7mlACM5B9f6Me4=
Expand Down
14 changes: 7 additions & 7 deletions pkg/engine/jmespath/arithmetic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func Test_Add(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.test)
jp, err := jmespathInterface.Query(tc.test)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -228,7 +228,7 @@ func Test_Sum(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.test)
jp, err := jmespathInterface.Query(tc.test)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -327,7 +327,7 @@ func Test_Subtract(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.test)
jp, err := jmespathInterface.Query(tc.test)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -426,7 +426,7 @@ func Test_Multiply(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.test)
jp, err := jmespathInterface.Query(tc.test)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -588,7 +588,7 @@ func Test_Divide(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.test)
jp, err := jmespathInterface.Query(tc.test)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -744,7 +744,7 @@ func Test_Modulo(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.test)
jp, err := jmespathInterface.Query(tc.test)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -813,7 +813,7 @@ func Test_Round(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.test)
jp, err := jmespathInterface.Query(tc.test)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down
66 changes: 33 additions & 33 deletions pkg/engine/jmespath/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"gotest.tools/assert"
)

var cfg = config.NewDefaultConfiguration(false)
var jmespathInterface = newImplementation(config.NewDefaultConfiguration(false))

func Test_Compare(t *testing.T) {
testCases := []struct {
Expand All @@ -33,7 +33,7 @@ func Test_Compare(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)

result, err := jp.Search("")
Expand All @@ -60,7 +60,7 @@ func Test_ParseJsonSerde(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc, func(t *testing.T) {
jp, err := newJMESPath(cfg, fmt.Sprintf(`to_string(parse_json('%s'))`, tc))
jp, err := jmespathInterface.Query(fmt.Sprintf(`to_string(parse_json('%s'))`, tc))
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -91,7 +91,7 @@ func Test_ParseJsonComplex(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.input)
jp, err := jmespathInterface.Query(tc.input)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -168,7 +168,7 @@ bar: null
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
jp, err := newJMESPath(cfg, fmt.Sprintf(`parse_yaml('%s')`, tc.input))
jp, err := jmespathInterface.Query(fmt.Sprintf(`parse_yaml('%s')`, tc.input))
assert.NilError(t, err)
result, err := jp.Search("")
assert.NilError(t, err)
Expand Down Expand Up @@ -197,7 +197,7 @@ func Test_EqualFold(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -234,7 +234,7 @@ func Test_Replace(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)

result, err := jp.Search("")
Expand All @@ -248,7 +248,7 @@ func Test_Replace(t *testing.T) {
}

func Test_ReplaceAll(t *testing.T) {
jp, err := newJMESPath(cfg, "replace_all('Lorem ipsum dolor sit amet', 'ipsum', 'muspi')")
jp, err := jmespathInterface.Query("replace_all('Lorem ipsum dolor sit amet', 'ipsum', 'muspi')")
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -279,7 +279,7 @@ func Test_ToUpper(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -312,7 +312,7 @@ func Test_ToLower(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)

result, err := jp.Search("")
Expand All @@ -326,7 +326,7 @@ func Test_ToLower(t *testing.T) {
}

func Test_Trim(t *testing.T) {
jp, err := newJMESPath(cfg, "trim('¡¡¡Hello, Gophers!!!', '!¡')")
jp, err := jmespathInterface.Query("trim('¡¡¡Hello, Gophers!!!', '!¡')")
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -397,7 +397,7 @@ func Test_TrimPrefix(t *testing.T) {
}

func Test_Split(t *testing.T) {
jp, err := newJMESPath(cfg, "split('Hello, Gophers', ', ')")
jp, err := jmespathInterface.Query("split('Hello, Gophers', ', ')")
assert.NilError(t, err)

result, err := jp.Search("")
Expand All @@ -410,7 +410,7 @@ func Test_Split(t *testing.T) {
}

func Test_HasPrefix(t *testing.T) {
jp, err := newJMESPath(cfg, "starts_with('Gophers', 'Go')")
jp, err := jmespathInterface.Query("starts_with('Gophers', 'Go')")
assert.NilError(t, err)

result, err := jp.Search("")
Expand All @@ -422,7 +422,7 @@ func Test_HasPrefix(t *testing.T) {
}

func Test_HasSuffix(t *testing.T) {
jp, err := newJMESPath(cfg, "ends_with('Amigo', 'go')")
jp, err := jmespathInterface.Query("ends_with('Amigo', 'go')")
assert.NilError(t, err)

result, err := jp.Search("")
Expand All @@ -437,7 +437,7 @@ func Test_RegexMatch(t *testing.T) {
data := make(map[string]interface{})
data["foo"] = "hgf'b1a2r'b12g"

query, err := newJMESPath(cfg, "regex_match('12.*', foo)")
query, err := jmespathInterface.Query("regex_match('12.*', foo)")
assert.NilError(t, err)

result, err := query.Search(data)
Expand All @@ -449,7 +449,7 @@ func Test_RegexMatchWithNumber(t *testing.T) {
data := make(map[string]interface{})
data["foo"] = -12.0

query, err := newJMESPath(cfg, "regex_match('12.*', abs(foo))")
query, err := jmespathInterface.Query("regex_match('12.*', abs(foo))")
assert.NilError(t, err)

result, err := query.Search(data)
Expand All @@ -461,7 +461,7 @@ func Test_PatternMatch(t *testing.T) {
data := make(map[string]interface{})
data["foo"] = "prefix-foo"

query, err := newJMESPath(cfg, "pattern_match('prefix-*', foo)")
query, err := jmespathInterface.Query("pattern_match('prefix-*', foo)")
assert.NilError(t, err)

result, err := query.Search(data)
Expand All @@ -473,7 +473,7 @@ func Test_PatternMatchWithNumber(t *testing.T) {
data := make(map[string]interface{})
data["foo"] = -12.0

query, err := newJMESPath(cfg, "pattern_match('12*', abs(foo))")
query, err := jmespathInterface.Query("pattern_match('12*', abs(foo))")
assert.NilError(t, err)

result, err := query.Search(data)
Expand All @@ -500,7 +500,7 @@ func Test_RegexReplaceAll(t *testing.T) {
var resource interface{}
err := json.Unmarshal(resourceRaw, &resource)
assert.NilError(t, err)
query, err := newJMESPath(cfg, `regex_replace_all('([Hh]e|G)l', spec.field, '${2}G')`)
query, err := jmespathInterface.Query(`regex_replace_all('([Hh]e|G)l', spec.field, '${2}G')`)
assert.NilError(t, err)

res, err := query.Search(resource)
Expand Down Expand Up @@ -531,7 +531,7 @@ func Test_RegexReplaceAllLiteral(t *testing.T) {
err := json.Unmarshal(resourceRaw, &resource)
assert.NilError(t, err)

query, err := newJMESPath(cfg, `regex_replace_all_literal('[Hh]el?', spec.field, 'G')`)
query, err := jmespathInterface.Query(`regex_replace_all_literal('[Hh]el?', spec.field, 'G')`)
assert.NilError(t, err)

res, err := query.Search(resource)
Expand Down Expand Up @@ -586,7 +586,7 @@ func Test_LabelMatch(t *testing.T) {
err := json.Unmarshal(tc.resource, &resource)
assert.NilError(t, err)

query, err := newJMESPath(cfg, "label_match(`"+tc.test+"`, metadata.labels)")
query, err := jmespathInterface.Query("label_match(`" + tc.test + "`, metadata.labels)")
assert.NilError(t, err)

res, err := query.Search(resource)
Expand Down Expand Up @@ -630,7 +630,7 @@ func Test_JpToBoolean(t *testing.T) {
}

func Test_Base64Decode(t *testing.T) {
jp, err := newJMESPath(cfg, "base64_decode('SGVsbG8sIHdvcmxkIQ==')")
jp, err := jmespathInterface.Query("base64_decode('SGVsbG8sIHdvcmxkIQ==')")
assert.NilError(t, err)

result, err := jp.Search("")
Expand All @@ -642,7 +642,7 @@ func Test_Base64Decode(t *testing.T) {
}

func Test_Base64Encode(t *testing.T) {
jp, err := newJMESPath(cfg, "base64_encode('Hello, world!')")
jp, err := jmespathInterface.Query("base64_encode('Hello, world!')")
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -672,7 +672,7 @@ func Test_Base64Decode_Secret(t *testing.T) {
err := json.Unmarshal(resourceRaw, &resource)
assert.NilError(t, err)

query, err := newJMESPath(cfg, `base64_decode(data.example1)`)
query, err := jmespathInterface.Query(`base64_decode(data.example1)`)
assert.NilError(t, err)

res, err := query.Search(resource)
Expand Down Expand Up @@ -747,7 +747,7 @@ func Test_PathCanonicalize(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -796,7 +796,7 @@ func Test_Truncate(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -833,7 +833,7 @@ func Test_SemverCompare(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -992,7 +992,7 @@ func Test_Lookup(t *testing.T) {
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
query, err := newJMESPath(cfg, "lookup(`"+tc.collection+"`,`"+tc.key+"`)")
query, err := jmespathInterface.Query("lookup(`" + tc.collection + "`,`" + tc.key + "`)")
assert.NilError(t, err)

result, err := query.Search("")
Expand Down Expand Up @@ -1034,7 +1034,7 @@ func Test_Lookup_InvalidArgs(t *testing.T) {
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
query, err := newJMESPath(cfg, "lookup(`"+tc.collection+"`,`"+tc.key+"`)")
query, err := jmespathInterface.Query("lookup(`" + tc.collection + "`,`" + tc.key + "`)")
assert.NilError(t, err)

_, err = query.Search("")
Expand Down Expand Up @@ -1077,7 +1077,7 @@ func Test_Items(t *testing.T) {
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
query, err := newJMESPath(cfg, "items(`"+tc.object+"`,`"+tc.keyName+"`,`"+tc.valName+"`)")
query, err := jmespathInterface.Query("items(`" + tc.object + "`,`" + tc.keyName + "`,`" + tc.valName + "`)")
assert.NilError(t, err)

res, err := query.Search("")
Expand Down Expand Up @@ -1128,7 +1128,7 @@ func Test_ObjectFromLists(t *testing.T) {
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
query, err := newJMESPath(cfg, "object_from_lists(`"+tc.keys+"`,`"+tc.values+"`)")
query, err := jmespathInterface.Query("object_from_lists(`" + tc.keys + "`,`" + tc.values + "`)")
assert.NilError(t, err)
res, err := query.Search("")
assert.NilError(t, err)
Expand Down Expand Up @@ -1245,7 +1245,7 @@ ZDGRs55xuoeLDJ/ZRFf9bI+IaCUd1YrfYcHIl3G87Av+r49YVwqRDT0VDV7uLgqn
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)

result, err := jp.Search("")
Expand Down Expand Up @@ -1661,7 +1661,7 @@ func Test_ImageNormalize(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.jmesPath, func(t *testing.T) {
jp, err := newJMESPath(cfg, tc.jmesPath)
jp, err := jmespathInterface.Query(tc.jmesPath)
assert.NilError(t, err)
result, err := jp.Search("")
if tc.wantErr {
Expand Down
Loading
Loading