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

refactor: compiler #523

Merged
merged 1 commit into from
Sep 27, 2024
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
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 @@
"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 @@
}, 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 @@
}, nil
}

func (c *compiler) compileMatch(
func CompileMatch(
path *field.Path,
compilers compilers.Compilers,
in *v1alpha1.Match,
Expand All @@ -76,11 +74,11 @@
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 @@
}, nil
}

func (c *compiler) compileAssert(
func CompileAssert(
path *field.Path,
compilers compilers.Compilers,
in v1alpha1.Assert,
Expand All @@ -118,11 +116,11 @@
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 @@
}, 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 @@
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 @@
}, 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 @@
return out, nil
}

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

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

func (c *compiler) compileFeedbacks(
func CompileFeedbacks(
path *field.Path,
compilers compilers.Compilers,
in ...v1alpha1.Feedback,
Expand All @@ -270,7 +268,7 @@
}
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)

Check warning on line 271 in pkg/json-engine/compiler.go

View check run for this annotation

Codecov / codecov/patch

pkg/json-engine/compiler.go#L271

Added line #L271 was not covered by tests
if err != nil {
return nil, err
}
Expand All @@ -285,7 +283,7 @@
}, nil
}

func (c *compiler) compileFeedback(
func CompileFeedback(
path *field.Path,
compilers compilers.Compilers,
in v1alpha1.Feedback,
Expand All @@ -308,35 +306,35 @@
}, 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