Skip to content

Instantly share code, notes, and snippets.

@richardcase
Created July 4, 2023 11:02
Show Gist options
  • Select an option

  • Save richardcase/d615eb9452c5e5e9af3f4f05e8d9e99b to your computer and use it in GitHub Desktop.

Select an option

Save richardcase/d615eb9452c5e5e9af3f4f05e8d9e99b to your computer and use it in GitHub Desktop.
CEL & CAPI WatchFilter
package main
import (
"log"
"github.com/google/cel-go/cel"
)
func main() {
env, err := cel.NewEnv(
cel.Variable("watchFilter", cel.StringType),
cel.Variable("label", cel.StringType),
)
if err != nil {
log.Fatalf("creating cel env: %s", err)
}
ast, issues := env.Compile(`watchFilter != label`)
if issues != nil && issues.Err() != nil {
log.Fatalf("type-check error: %s", issues.Err())
}
prg, err := env.Program(ast)
if err != nil {
log.Fatalf("program construction error: %s", err)
}
watchFilter := "test1"
out, details, err := prg.Eval(map[string]interface{}{
"watchFilter": watchFilter,
"label": "abcdef",
})
if err != nil {
log.Fatalf("evaluating program error: %s", err)
}
println(out)
println(details)
out, details, err = prg.Eval(map[string]interface{}{
"watchFilter": watchFilter,
"label": "test1",
})
if err != nil {
log.Fatalf("evaluating program error: %s", err)
}
println(out)
println(details)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment