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)
| 269 | // match statements. The compiled rule defines an expression graph, which can be composed into a single |
| 270 | // expression via the RuleComposer.Compose method. |
| 271 | func 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 | |
| 317 | type compiler struct { |
| 318 | env *cel.Env |