Skip to content

Commit

Permalink
path
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 17, 2024
1 parent 2cc0104 commit b51f39e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
17 changes: 7 additions & 10 deletions pkg/engine/assert/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ import (

"github.com/jmespath-community/go-jmespath/pkg/binding"
jpbinding "github.com/jmespath-community/go-jmespath/pkg/binding"
"github.com/jmespath-community/go-jmespath/pkg/interpreter"
"github.com/jmespath-community/go-jmespath/pkg/parsing"
"github.com/kyverno/kyverno-json/pkg/engine/match"
"github.com/kyverno/kyverno-json/pkg/engine/template"
reflectutils "github.com/kyverno/kyverno-json/pkg/utils/reflect"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func Parse(ctx context.Context, assertion any) (Assertion, error) {
func Parse(ctx context.Context, path *field.Path, assertion any) (Assertion, error) {

Check warning on line 17 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L17

Added line #L17 was not covered by tests
switch reflectutils.GetKind(assertion) {
case reflect.Slice:
node := sliceNode{}
valueOf := reflect.ValueOf(assertion)
for i := 0; i < valueOf.Len(); i++ {
sub, err := Parse(ctx, valueOf.Index(i).Interface())
sub, err := Parse(ctx, path.Index(i), valueOf.Index(i).Interface())
if err != nil {
return nil, err
}
Expand All @@ -32,16 +31,16 @@ func Parse(ctx context.Context, assertion any) (Assertion, error) {
node := mapNode{}
iter := reflect.ValueOf(assertion).MapRange()
for iter.Next() {
sub, err := Parse(ctx, iter.Value().Interface())
key := iter.Key().Interface()
sub, err := Parse(ctx, path.Child(fmt.Sprint(key)), iter.Value().Interface())
if err != nil {
return nil, err
}
node[iter.Key().Interface()] = sub
node[key] = sub

Check warning on line 39 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L34-L39

Added lines #L34 - L39 were not covered by tests
}
return node, nil

Check warning on line 41 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L41

Added line #L41 was not covered by tests
default:
// TODO: propagate path
return newScalarNode(ctx, nil, assertion)
return newScalarNode(ctx, path, assertion)

Check warning on line 43 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L43

Added line #L43 was not covered by tests
}
}

Expand Down Expand Up @@ -165,9 +164,7 @@ func newScalarNode(ctx context.Context, path *field.Path, rhs any) (Assertion, e
}
return &scalarNode{
project: func(value any, bindings binding.Bindings, opts ...template.Option) (any, error) {
o := template.BuildOptions(opts...)
vm := interpreter.NewInterpreter(nil, bindings)
return vm.Execute(compiled, value, interpreter.WithFunctionCaller(o.FunctionCaller))
return template.ExecuteAST(ctx, compiled, value, bindings, opts...)
},

Check warning on line 168 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L165-L168

Added lines #L165 - L168 were not covered by tests
}, nil
} else {
Expand Down
18 changes: 9 additions & 9 deletions pkg/engine/template/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ var (
defaultCaller = interpreter.NewFunctionCaller(funcs...)
)

type Option func(Options) Options
type Option func(options) options

type Options struct {
FunctionCaller interpreter.FunctionCaller
type options struct {
functionCaller interpreter.FunctionCaller
}

func WithFunctionCaller(functionCaller interpreter.FunctionCaller) Option {
return func(o Options) Options {
o.FunctionCaller = functionCaller
return func(o options) options {
o.functionCaller = functionCaller
return o
}
}

func BuildOptions(opts ...Option) Options {
var o Options
func buildOptions(opts ...Option) options {
var o options
for _, opt := range opts {
if opt != nil {
o = opt(o)
}
}
if o.FunctionCaller == nil {
o.FunctionCaller = defaultCaller
if o.functionCaller == nil {
o.functionCaller = defaultCaller
}
return o
}
10 changes: 7 additions & 3 deletions pkg/engine/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ func String(ctx context.Context, in string, value any, bindings binding.Bindings
}

func Execute(ctx context.Context, statement string, value any, bindings binding.Bindings, opts ...Option) (any, error) {
o := BuildOptions(opts...)
vm := interpreter.NewInterpreter(nil, bindings)
parser := parsing.NewParser()
compiled, err := parser.Parse(statement)
if err != nil {
return nil, err
}
return vm.Execute(compiled, value, interpreter.WithFunctionCaller(o.FunctionCaller))
return ExecuteAST(ctx, compiled, value, bindings, opts...)
}

func ExecuteAST(ctx context.Context, ast parsing.ASTNode, value any, bindings binding.Bindings, opts ...Option) (any, error) {
o := buildOptions(opts...)
vm := interpreter.NewInterpreter(nil, bindings)
return vm.Execute(ast, value, interpreter.WithFunctionCaller(o.functionCaller))
}
8 changes: 4 additions & 4 deletions pkg/matching/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func MatchAssert(ctx context.Context, path *field.Path, match *v1alpha1.Assert,
var fails []Result
path := path.Child("any")
for i, assertion := range match.Any {
parsed, err := assert.Parse(ctx, assertion.Check.Value)
parsed, err := assert.Parse(ctx, path.Index(i).Child("check"), assertion.Check.Value)
if err != nil {
return fails, err
}

Check warning on line 54 in pkg/matching/match.go

View check run for this annotation

Codecov / codecov/patch

pkg/matching/match.go#L53-L54

Added lines #L53 - L54 were not covered by tests
Expand Down Expand Up @@ -76,7 +76,7 @@ func MatchAssert(ctx context.Context, path *field.Path, match *v1alpha1.Assert,
var fails []Result
path := path.Child("all")
for i, assertion := range match.All {
parsed, err := assert.Parse(ctx, assertion.Check.Value)
parsed, err := assert.Parse(ctx, path.Index(i).Child("check"), assertion.Check.Value)
if err != nil {
return fails, err
}

Check warning on line 82 in pkg/matching/match.go

View check run for this annotation

Codecov / codecov/patch

pkg/matching/match.go#L81-L82

Added lines #L81 - L82 were not covered by tests
Expand Down Expand Up @@ -126,7 +126,7 @@ func Match(ctx context.Context, path *field.Path, match *v1alpha1.Match, actual
func MatchAny(ctx context.Context, path *field.Path, assertions []v1alpha1.Any, actual any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
var errs field.ErrorList
for i, assertion := range assertions {
parsed, err := assert.Parse(ctx, assertion.Value)
parsed, err := assert.Parse(ctx, path.Index(i), assertion.Value)
if err != nil {
return errs, err
}

Check warning on line 132 in pkg/matching/match.go

View check run for this annotation

Codecov / codecov/patch

pkg/matching/match.go#L131-L132

Added lines #L131 - L132 were not covered by tests
Expand All @@ -145,7 +145,7 @@ func MatchAny(ctx context.Context, path *field.Path, assertions []v1alpha1.Any,
func MatchAll(ctx context.Context, path *field.Path, assertions []v1alpha1.Any, actual any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
var errs field.ErrorList
for i, assertion := range assertions {
parsed, err := assert.Parse(ctx, assertion.Value)
parsed, err := assert.Parse(ctx, path.Index(i), assertion.Value)
if err != nil {
return errs, err
}

Check warning on line 151 in pkg/matching/match.go

View check run for this annotation

Codecov / codecov/patch

pkg/matching/match.go#L150-L151

Added lines #L150 - L151 were not covered by tests
Expand Down

0 comments on commit b51f39e

Please sign in to comment.