Initialize the CEL program in a util function

This commit is contained in:
PiMaDaum 2023-02-05 22:19:54 +01:00
parent f30590588e
commit 0a542bfdba
5 changed files with 35 additions and 48 deletions

23
util/cel.go Normal file
View file

@ -0,0 +1,23 @@
package util
import "github.com/google/cel-go/cel"
// InitCELProgram - Initialize a CEL program with given CEL command and a set of environments
func InitCELProgram(celCmd string, options ...cel.EnvOption) (*cel.Program, error) {
env, err := cel.NewEnv(options...)
if err != nil {
return nil, err
}
ast, issue := env.Compile(celCmd)
if issue.Err() != nil {
return nil, issue.Err()
}
program, err := env.Program(ast)
if err != nil {
return nil, err
}
return &program, nil
}