Skip to content

Commit

Permalink
refactor: compiler (#523)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly authored Sep 27, 2024
1 parent 0500f6b commit 2389f3a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
56 changes: 27 additions & 29 deletions pkg/json-engine/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
)

type compiler struct{}

func (c *compiler) compileContextEntry(
func CompileContextEntry(
path *field.Path,
compilers compilers.Compilers,
in v1alpha1.ContextEntry,
Expand Down Expand Up @@ -41,14 +39,14 @@ func (c *compiler) compileContextEntry(
}, nil
}

func (c *compiler) compileContextEntries(
func CompileContextEntries(
path *field.Path,
compilers compilers.Compilers,
in ...v1alpha1.ContextEntry,
) (func(any, binding.Bindings) binding.Bindings, *field.Error) {
var out []func(any, binding.Bindings) binding.Bindings
for i, entry := range in {
entry, err := c.compileContextEntry(path.Index(i), compilers, entry)
entry, err := CompileContextEntry(path.Index(i), compilers, entry)
if err != nil {
return nil, err
}
Expand All @@ -62,7 +60,7 @@ func (c *compiler) compileContextEntries(
}, nil
}

func (c *compiler) compileMatch(
func CompileMatch(
path *field.Path,
compilers compilers.Compilers,
in *v1alpha1.Match,
Expand All @@ -76,11 +74,11 @@ func (c *compiler) compileMatch(
if in.Compiler != nil {
compilers = compilers.WithDefaultCompiler(string(*in.Compiler))
}
_any, err := c.compileAssertionTrees(path.Child("any"), compilers, in.Any...)
_any, err := CompileAssertionTrees(path.Child("any"), compilers, in.Any...)
if err != nil {
return nil, err
}
_all, err := c.compileAssertionTrees(path.Child("all"), compilers, in.All...)
_all, err := CompileAssertionTrees(path.Child("all"), compilers, in.All...)
if err != nil {
return nil, err
}
Expand All @@ -107,7 +105,7 @@ func (c *compiler) compileMatch(
}, nil
}

func (c *compiler) compileAssert(
func CompileAssert(
path *field.Path,
compilers compilers.Compilers,
in v1alpha1.Assert,
Expand All @@ -118,11 +116,11 @@ func (c *compiler) compileAssert(
if len(in.Any) == 0 && len(in.All) == 0 {
return nil, field.Invalid(path, in, "an empty assert is not valid")
}
_any, err := c.compileAssertions(path.Child("any"), compilers, in.Any...)
_any, err := CompileAssertions(path.Child("any"), compilers, in.Any...)
if err != nil {
return nil, err
}
_all, err := c.compileAssertions(path.Child("all"), compilers, in.All...)
_all, err := CompileAssertions(path.Child("all"), compilers, in.All...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -161,14 +159,14 @@ func (c *compiler) compileAssert(
}, nil
}

func (c *compiler) compileAssertions(
func CompileAssertions(
path *field.Path,
compilers compilers.Compilers,
in ...v1alpha1.Assertion,
) ([]func(any, binding.Bindings) (Result, error), *field.Error) {
var out []func(any, binding.Bindings) (Result, error)
for i, in := range in {
if in, err := c.compileAssertion(path.Index(i), compilers, in); err != nil {
if in, err := CompileAssertion(path.Index(i), compilers, in); err != nil {
return nil, err
} else {
out = append(out, in)
Expand All @@ -177,15 +175,15 @@ func (c *compiler) compileAssertions(
return out, nil
}

func (c *compiler) compileAssertion(
func CompileAssertion(
path *field.Path,
compilers compilers.Compilers,
in v1alpha1.Assertion,
) (func(any, binding.Bindings) (Result, error), *field.Error) {
if in.Compiler != nil {
compilers = compilers.WithDefaultCompiler(string(*in.Compiler))
}
check, err := c.compileAssertionTree(path.Child("check"), compilers, in.Check)
check, err := CompileAssertionTree(path.Child("check"), compilers, in.Check)
if err != nil {
return nil, err
}
Expand All @@ -204,14 +202,14 @@ func (c *compiler) compileAssertion(
}, nil
}

func (c *compiler) compileAssertionTrees(
func CompileAssertionTrees(
path *field.Path,
compilers compilers.Compilers,
in ...v1alpha1.AssertionTree,
) ([]func(any, binding.Bindings) (field.ErrorList, error), *field.Error) {
var out []func(any, binding.Bindings) (field.ErrorList, error)
for i, in := range in {
if in, err := c.compileAssertionTree(path.Index(i), compilers, in); err != nil {
if in, err := CompileAssertionTree(path.Index(i), compilers, in); err != nil {
return nil, err
} else {
out = append(out, in)
Expand All @@ -220,7 +218,7 @@ func (c *compiler) compileAssertionTrees(
return out, nil
}

func (c *compiler) compileAssertionTree(
func CompileAssertionTree(
path *field.Path,
compilers compilers.Compilers,
in v1alpha1.AssertionTree,
Expand All @@ -234,7 +232,7 @@ func (c *compiler) compileAssertionTree(
}, nil
}

func (c *compiler) compileIdentifier(
func CompileIdentifier(
path *field.Path,
compilers compilers.Compilers,
in string,
Expand All @@ -258,7 +256,7 @@ func (c *compiler) compileIdentifier(
}, nil
}

func (c *compiler) compileFeedbacks(
func CompileFeedbacks(
path *field.Path,
compilers compilers.Compilers,
in ...v1alpha1.Feedback,
Expand All @@ -270,7 +268,7 @@ func (c *compiler) compileFeedbacks(
}
feedback := map[string]func(any, binding.Bindings) Feedback{}
for i, in := range in {
f, err := c.compileFeedback(path.Index(i), compilers, in)
f, err := CompileFeedback(path.Index(i), compilers, in)
if err != nil {
return nil, err
}
Expand All @@ -285,7 +283,7 @@ func (c *compiler) compileFeedbacks(
}, nil
}

func (c *compiler) compileFeedback(
func CompileFeedback(
path *field.Path,
compilers compilers.Compilers,
in v1alpha1.Feedback,
Expand All @@ -308,35 +306,35 @@ func (c *compiler) compileFeedback(
}, nil
}

func (c *compiler) compileRule(
func CompileRule(
path *field.Path,
compilers compilers.Compilers,
in v1alpha1.ValidatingRule,
) (func(any, binding.Bindings) *RuleResponse, *field.Error) {
if in.Compiler != nil {
compilers = compilers.WithDefaultCompiler(string(*in.Compiler))
}
context, err := c.compileContextEntries(path.Child("context"), compilers, in.Context...)
context, err := CompileContextEntries(path.Child("context"), compilers, in.Context...)
if err != nil {
return nil, err
}
identifier, err := c.compileIdentifier(path.Child("identifier"), compilers, in.Identifier)
identifier, err := CompileIdentifier(path.Child("identifier"), compilers, in.Identifier)
if err != nil {
return nil, err
}
match, err := c.compileMatch(path.Child("match"), compilers, in.Match)
match, err := CompileMatch(path.Child("match"), compilers, in.Match)
if err != nil {
return nil, err
}
exclude, err := c.compileMatch(path.Child("exclude"), compilers, in.Exclude)
exclude, err := CompileMatch(path.Child("exclude"), compilers, in.Exclude)
if err != nil {
return nil, err
}
feedback, err := c.compileFeedbacks(path.Child("feedback"), compilers, in.Feedback...)
feedback, err := CompileFeedbacks(path.Child("feedback"), compilers, in.Feedback...)
if err != nil {
return nil, err
}
assert, err := c.compileAssert(path.Child("assert"), compilers, in.Assert)
assert, err := CompileAssert(path.Child("assert"), compilers, in.Assert)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/json-engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func New() engine.Engine[Request, Response] {
}
ruleEngine := builder.
Function(func(ctx context.Context, r ruleRequest) *RuleResponse {
ruleCompiler := compiler{}
compiled, err := ruleCompiler.compileRule(r.path, r.compilers, r.rule)
compiled, err := CompileRule(r.path, r.compilers, r.rule)
if err != nil {
return &RuleResponse{
Rule: r.rule,
Expand Down

0 comments on commit 2389f3a

Please sign in to comment.