Skip to content

Commit

Permalink
feat: update jmespath lib
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Sep 20, 2024
1 parent 3493e99 commit fb1e8f1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 45 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/gin-gonic/gin v1.10.0
github.com/google/cel-go v0.20.1
github.com/google/go-cmp v0.6.0
github.com/jmespath-community/go-jmespath v1.1.2-0.20240117150817-e430401a2172
github.com/jmespath-community/go-jmespath v1.1.2-0.20240919193755-5e4e8ae73c8a
github.com/kyverno/pkg/ext v0.0.0-20240418121121-df8add26c55c
github.com/loopfz/gadgeto v0.11.4
github.com/nlepage/go-wasm-http-server v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jmespath-community/go-jmespath v1.1.2-0.20240117150817-e430401a2172 h1:XQYEhx+bEiWn6eiHFivu4wEHm91FoZ/gCvoLZK6Ze5Y=
github.com/jmespath-community/go-jmespath v1.1.2-0.20240117150817-e430401a2172/go.mod h1:j4OeykGPBbhX3rw4AOPGXSmX2/zuWXktm704A4MtHFs=
github.com/jmespath-community/go-jmespath v1.1.2-0.20240919193755-5e4e8ae73c8a h1:baQnVszUF+wENqBnqJDlgmSIBpJLMqQE0i+hICMZ9sI=
github.com/jmespath-community/go-jmespath v1.1.2-0.20240919193755-5e4e8ae73c8a/go.mod h1:VL6C6nwf/wRivvXAjziX9yFRVmvOC1qzERc8RTQ0tv4=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down
58 changes: 31 additions & 27 deletions pkg/core/templating/compiler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package templating

import (
"sync"

"github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/jmespath-community/go-jmespath/pkg/interpreter"
"github.com/jmespath-community/go-jmespath/pkg/parsing"
Expand Down Expand Up @@ -65,36 +67,38 @@ func (c Compiler) CompileJP(statement string) (Program, error) {
}

func (c Compiler) NewBinding(path *field.Path, value any, bindings binding.Bindings, template any) binding.Binding {
return jp.NewLazyBinding(
func() (any, error) {
switch typed := template.(type) {
case string:
expr := expression.Parse(typed)
if expr.Foreach {
return nil, field.Invalid(path.Child("variable"), typed, "foreach is not supported in context")
}
if expr.Binding != "" {
return nil, field.Invalid(path.Child("variable"), typed, "binding is not supported in context")
}
switch expr.Engine {
case expression.EngineJP:
projected, err := ExecuteJP(expr.Statement, value, bindings, c)
if err != nil {
return nil, field.InternalError(path.Child("variable"), err)
return binding.NewDelegate(
sync.OnceValues(
func() (any, error) {
switch typed := template.(type) {
case string:
expr := expression.Parse(typed)
if expr.Foreach {
return nil, field.Invalid(path.Child("variable"), typed, "foreach is not supported in context")
}

Check warning on line 78 in pkg/core/templating/compiler.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/templating/compiler.go#L77-L78

Added lines #L77 - L78 were not covered by tests
if expr.Binding != "" {
return nil, field.Invalid(path.Child("variable"), typed, "binding is not supported in context")

Check warning on line 80 in pkg/core/templating/compiler.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/templating/compiler.go#L80

Added line #L80 was not covered by tests
}
return projected, nil
case expression.EngineCEL:
projected, err := ExecuteCEL(expr.Statement, value, bindings, c)
if err != nil {
return nil, field.InternalError(path.Child("variable"), err)
switch expr.Engine {
case expression.EngineJP:
projected, err := ExecuteJP(expr.Statement, value, bindings, c)
if err != nil {
return nil, field.InternalError(path.Child("variable"), err)
}

Check warning on line 87 in pkg/core/templating/compiler.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/templating/compiler.go#L86-L87

Added lines #L86 - L87 were not covered by tests
return projected, nil
case expression.EngineCEL:
projected, err := ExecuteCEL(expr.Statement, value, bindings, c)
if err != nil {
return nil, field.InternalError(path.Child("variable"), err)
}

Check warning on line 93 in pkg/core/templating/compiler.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/templating/compiler.go#L92-L93

Added lines #L92 - L93 were not covered by tests
return projected, nil
default:
return expr.Statement, nil
}
return projected, nil
default:
return expr.Statement, nil
return typed, nil
}
default:
return typed, nil
}
},
},
),
)
}
15 changes: 0 additions & 15 deletions pkg/core/templating/jp/binding.go

This file was deleted.

0 comments on commit fb1e8f1

Please sign in to comment.