| 1 | # Examples |
| 2 | |
| 3 | ## Simple example using builtin operators |
| 4 | |
| 5 | Evaluate expression `"Hello world! I'm " + name + "."` with `CEL` passed as |
| 6 | name. |
| 7 | |
| 8 | ```go |
| 9 | import "github.com/google/cel-go/cel" |
| 10 | |
| 11 | env, err := cel.NewEnv(cel.Variable("name", cel.StringType)) |
| 12 | // Check err for environment setup errors. |
| 13 | if err != nil { |
| 14 | log.Fatalln(err) |
| 15 | } |
| 16 | ast, iss := env.Compile(`"Hello world! I'm " + name + "."`) |
| 17 | // Check iss for compilation errors. |
| 18 | if iss.Err() != nil { |
| 19 | log.Fatalln(iss.Err()) |
| 20 | } |
| 21 | prg, err := env.Program(ast) |
| 22 | out, _, err := prg.Eval(map[string]interface{}{ |
| 23 | "name": "CEL", |
| 24 | }) |
| 25 | fmt.Println(out) |
| 26 | // Output:Hello world! I'm CEL. |
| 27 | ``` |
| 28 | |
| 29 | [Source code](simple_test.go) |
| 30 | |
| 31 | ## Custom function on string type |
| 32 | |
| 33 | Evaluate expression `i.greet(you)` with: |
| 34 | |
| 35 | ``` |
| 36 | i -> CEL |
| 37 | you -> world |
| 38 | greet -> "Hello %s! Nice to meet you, I'm %s." |
| 39 | ``` |
| 40 | |
| 41 | First we need to declare two string variables and `greet` function. |
| 42 | `Function` must be used if we want to declare an extension to CEL. The |
| 43 | `MemberOverload` declares an overload id, a list of arguments where the |
| 44 | first element the `argTypes` slice is the target type of the member |
| 45 | function. The remaining argument types are the signature of the member |
| 46 | method. |
| 47 | |
| 48 | ```go |
| 49 | env, _ := cel.NewEnv( |
| 50 | cel.Variable("i", cel.StringType), |
| 51 | cel.Variable("you", cel.StringType), |
| 52 | cel.Function("greet", |
| 53 | cel.MemberOverload("string_greet_string", |
| 54 | []*cel.Type{cel.StringType, cel.StringType}, |
| 55 | cel.StringType, |
| 56 | cel.BinaryBinding(func (lhs, rhs ref.Val) ref.Val { |
| 57 | return types.String( |
| 58 | fmt.Sprintf("Hello %s! Nice to meet you, I'm %s.\n", rhs, lhs)) |
| 59 | }, |
| 60 | ), |
| 61 | ), |
| 62 | ), |
| 63 | ) |
| 64 | prg, _ := env.Program(c) |
| 65 | out, _, _ := prg.Eval(map[string]interface{}{ |
| 66 | "i": "CEL", |
| 67 | "you": "world", |
| 68 | }) |
| 69 | fmt.Println(out) |
| 70 | // Output:Hello world! Nice to meet you, I'm CEL. |
| 71 | ``` |
| 72 | [Source code](custom_instance_function_test.go) |
| 73 | |
| 74 | ## Define custom global function |
| 75 | |
| 76 | Evaluate expression `shake_hands(i,you)` with: |
| 77 | |
| 78 | ``` |
| 79 | i -> CEL |
| 80 | you -> world |
| 81 | shake_hands -> "%s and %s are shaking hands." |
| 82 | ``` |
| 83 | |
| 84 | In order to declare global function we need to use `Overload` instead |
| 85 | of `MemberOverload` in the `Function` option: |
| 86 | |
| 87 | ```go |
| 88 | env, _ := cel.NewEnv( |
| 89 | cel.Variable("i", cel.StringType), |
| 90 | cel.Variable("you", cel.StringType), |
| 91 | cel.Function("shake_hands", |
| 92 | cel.Overload("shake_hands_string_string", |
| 93 | []*cel.Type{cel.StringType, cel.StringType}, |
| 94 | cel.StringType, |
| 95 | cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val { |
| 96 | return types.String( |
| 97 | fmt.Sprintf("%s and %s are shaking hands.\n", lhs, rhs)) |
| 98 | }, |
| 99 | ), |
| 100 | ), |
| 101 | ), |
| 102 | ) |
| 103 | prg, _ := env.Program(c) |
| 104 | out, _, _ := prg.Eval(map[string]interface{}{ |
| 105 | "i": "CEL", |
| 106 | "you": "world", |
| 107 | }) |
| 108 | fmt.Println(out) |
| 109 | // Output:CEL and world are shaking hands. |
| 110 | ``` |
| 111 | |
| 112 | [Source code](custom_global_function_test.go) |
| 113 | |
| 114 | For more examples of how to use CEL, see |
| 115 | [cel_test.go](https://github.com/google/cel-go/tree/master/cel/cel_test.go). |