MCPcopy Create free account
hub / github.com/cel-expr/cel-go / CompileRule

Function CompileRule

policy/compiler.go:271–315  ·  view source on GitHub ↗

CompileRule creates a compiled rules from the policy which contains a set of compiled variables and match statements. The compiled rule defines an expression graph, which can be composed into a single expression via the RuleComposer.Compose method.

(env *cel.Env, p *Policy, opts ...CompilerOption)

Source from the content-addressed store, hash-verified

269// match statements. The compiled rule defines an expression graph, which can be composed into a single
270// expression via the RuleComposer.Compose method.
271func CompileRule(env *cel.Env, p *Policy, opts ...CompilerOption) (*CompiledRule, *cel.Issues) {
272 c := &compiler{
273 env: env,
274 info: p.SourceInfo(),
275 src: p.Source(),
276 compileMatchOutput: defaultCompileMatchOutput,
277 maxNestedExpressions: defaultMaxNestedExpressions,
278 }
279 var err error
280 errs := common.NewErrors(c.src)
281 iss := cel.NewIssuesWithSourceInfo(errs, c.info)
282 for _, o := range opts {
283 if err = o(c); err != nil {
284 iss.ReportErrorAtID(p.Name().ID, "error configuring compiler option: %s", err)
285 return nil, iss
286 }
287 }
288 c.env, err = c.env.Extend(cel.EagerlyValidateDeclarations(true))
289 if err != nil {
290 iss.ReportErrorAtID(p.Name().ID, "error configuring environment: %s", err)
291 return nil, iss
292 }
293
294 importCount := len(p.Imports())
295 if importCount > 0 {
296 importNames := make([]string, 0, importCount)
297 for _, imp := range p.Imports() {
298 typeName := imp.Name().Value
299 _, err := containers.NewContainer(containers.Abbrevs(typeName))
300 if err != nil {
301 iss.ReportErrorAtID(imp.Name().ID, "error configuring import: %s", err)
302 } else {
303 importNames = append(importNames, typeName)
304 }
305 }
306 env, err := c.env.Extend(cel.Abbrevs(importNames...))
307 if err != nil {
308 // validation happens earlier in the sequence, so this should be unreachable.
309 iss.ReportErrorAtID(p.Imports()[0].SourceID(), "error configuring imports: %s", err)
310 } else {
311 c.env = env
312 }
313 }
314 return c.compileRule(p.Rule(), p, c.env, iss, false)
315}
316
317type compiler struct {
318 env *cel.Env

Callers 4

parseAndComposeRuleFunction · 0.85
compileRuleMethod · 0.85
TestCompiledRuleSemanticFunction · 0.85
CompileFunction · 0.85

Calls 15

compileRuleMethod · 0.95
NewErrorsFunction · 0.92
NewIssuesWithSourceInfoFunction · 0.92
NewContainerFunction · 0.92
AbbrevsFunction · 0.92
AbbrevsFunction · 0.92
ImportsMethod · 0.80
ReportErrorAtIDMethod · 0.65
NameMethod · 0.65
SourceInfoMethod · 0.45
SourceMethod · 0.45

Tested by 3

parseAndComposeRuleFunction · 0.68
compileRuleMethod · 0.68
TestCompiledRuleSemanticFunction · 0.68