-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add core/compilers packages (#506)
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
183fe67
commit fc7ad29
Showing
22 changed files
with
146 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cel | ||
|
||
import ( | ||
"github.com/google/cel-go/cel" | ||
"github.com/jmespath-community/go-jmespath/pkg/binding" | ||
"github.com/kyverno/kyverno-json/pkg/core/compilers" | ||
) | ||
|
||
type Compiler interface { | ||
Compile(string) (compilers.Program, error) | ||
} | ||
|
||
type compiler struct{} | ||
|
||
func NewCompiler() *compiler { | ||
return &compiler{} | ||
} | ||
|
||
func (c *compiler) Compile(statement string) (compilers.Program, error) { | ||
env, err := DefaultEnv() | ||
if err != nil { | ||
return nil, err | ||
} | ||
ast, iss := env.Compile(statement) | ||
if iss.Err() != nil { | ||
return nil, iss.Err() | ||
} | ||
program, err := env.Program(ast) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return func(value any, bindings binding.Bindings) (any, error) { | ||
return Execute(program, value, bindings) | ||
}, nil | ||
} | ||
|
||
func Execute(program cel.Program, value any, bindings binding.Bindings) (any, error) { | ||
data := map[string]interface{}{ | ||
"object": value, | ||
"bindings": NewVal(bindings, BindingsType), | ||
} | ||
out, _, err := program.Eval(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return out.Value(), nil | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package compilers | ||
|
||
type Compiler interface { | ||
Compile(string) (Program, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package jp | ||
|
||
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" | ||
"github.com/kyverno/kyverno-json/pkg/core/compilers" | ||
) | ||
|
||
type Compiler interface { | ||
Compile(string) (compilers.Program, error) | ||
Options() []Option | ||
} | ||
|
||
type compiler struct { | ||
options []Option | ||
buildOptions func() options | ||
} | ||
|
||
func NewCompiler(opts ...Option) *compiler { | ||
return &compiler{ | ||
options: opts, | ||
buildOptions: sync.OnceValue(func() options { | ||
return buildOptions(opts...) | ||
}), | ||
} | ||
} | ||
|
||
func (c *compiler) Options() []Option { | ||
return c.options | ||
} | ||
|
||
func (c *compiler) Compile(statement string) (compilers.Program, error) { | ||
parser := parsing.NewParser() | ||
compiled, err := parser.Parse(statement) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return func(value any, bindings binding.Bindings) (any, error) { | ||
return execute(compiled, value, bindings, c.buildOptions()) | ||
}, nil | ||
} | ||
|
||
func Execute(ast parsing.ASTNode, value any, bindings binding.Bindings, opts ...Option) (any, error) { | ||
return execute(ast, value, bindings, buildOptions(opts...)) | ||
} | ||
|
||
func execute(ast parsing.ASTNode, value any, bindings binding.Bindings, options options) (any, error) { | ||
vm := interpreter.NewInterpreter(nil, bindings) | ||
return vm.Execute(ast, value, interpreter.WithFunctionCaller(options.functionCaller)) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package compilers | ||
|
||
import ( | ||
"github.com/jmespath-community/go-jmespath/pkg/binding" | ||
) | ||
|
||
type Program func(any, binding.Bindings) (any, error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.