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

Function CompileRule

policy/compiler.go:257–301  ·  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

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

Callers 4

TestCompose_SourceInfoFunction · 0.85
TestCompose_UnnestFunction · 0.85
compileRuleMethod · 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

TestCompose_SourceInfoFunction · 0.68
TestCompose_UnnestFunction · 0.68
compileRuleMethod · 0.68