Created
July 4, 2023 11:02
-
-
Save richardcase/d615eb9452c5e5e9af3f4f05e8d9e99b to your computer and use it in GitHub Desktop.
CEL & CAPI WatchFilter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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