NewCompiler creates a new compiler with a set of functional options.
(opts ...any)
| 118 | |
| 119 | // NewCompiler creates a new compiler with a set of functional options. |
| 120 | func NewCompiler(opts ...any) (Compiler, error) { |
| 121 | c := &compiler{ |
| 122 | envOptions: []cel.EnvOption{}, |
| 123 | policyParserOptions: []policy.ParserOption{}, |
| 124 | policyCompilerOptions: []policy.CompilerOption{}, |
| 125 | policyMetadataEnvOptions: []PolicyMetadataEnvOption{}, |
| 126 | } |
| 127 | for _, opt := range opts { |
| 128 | switch opt := opt.(type) { |
| 129 | case cel.EnvOption: |
| 130 | c.envOptions = append(c.envOptions, opt) |
| 131 | case policy.ParserOption: |
| 132 | c.policyParserOptions = append(c.policyParserOptions, opt) |
| 133 | case policy.CompilerOption: |
| 134 | c.policyCompilerOptions = append(c.policyCompilerOptions, opt) |
| 135 | case PolicyMetadataEnvOption: |
| 136 | c.policyMetadataEnvOptions = append(c.policyMetadataEnvOptions, opt) |
| 137 | default: |
| 138 | return nil, fmt.Errorf("unsupported compiler option: %v", opt) |
| 139 | } |
| 140 | } |
| 141 | c.envOptions = append(c.envOptions, extensionOpt()) |
| 142 | return c, nil |
| 143 | } |
| 144 | |
| 145 | func extensionOpt() cel.EnvOption { |
| 146 | return func(e *cel.Env) (*cel.Env, error) { |