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