| 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package cel_test |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "fmt" |
| 20 | "log" |
| 21 | |
| 22 | "github.com/google/cel-go/cel" |
| 23 | "github.com/google/cel-go/common/types" |
| 24 | "github.com/google/cel-go/common/types/ref" |
| 25 | ) |
| 26 | |
| 27 | func Example() { |
| 28 | // Create the CEL environment with declarations for the input attributes and the extension functions. |
| 29 | // In many cases the desired functionality will be present in a built-in function. |
| 30 | e, err := cel.NewEnv( |
| 31 | // Variable identifiers used within this expression. |
| 32 | cel.Variable("i", cel.StringType), |
| 33 | cel.Variable("you", cel.StringType), |
| 34 | // Function to generate a greeting from one person to another: i.greet(you) |
| 35 | cel.Function("greet", |
| 36 | cel.MemberOverload("string_greet_string", []*cel.Type{cel.StringType, cel.StringType}, cel.StringType, |
| 37 | cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val { |
| 38 | return types.String(fmt.Sprintf("Hello %s! Nice to meet you, I'm %s.\n", rhs, lhs)) |
| 39 | }), |
| 40 | ), |
| 41 | ), |
| 42 | ) |
| 43 | if err != nil { |
| 44 | log.Fatalf("environment creation error: %s\n", err) |
| 45 | } |
| 46 | |
| 47 | // Compile the expression. |
| 48 | ast, iss := e.Compile("i.greet(you)") |
| 49 | if iss.Err() != nil { |
| 50 | log.Fatalln(iss.Err()) |
| 51 | } |
| 52 | |
| 53 | // Create the program. |
| 54 | prg, err := e.Program(ast) |
| 55 | if err != nil { |
| 56 | log.Fatalf("program creation error: %s\n", err) |
| 57 | } |
| 58 | |
| 59 | // Evaluate the program against some inputs. Note: the details return is not used. |
| 60 | out, _, err := prg.Eval(map[string]any{ |
| 61 | // Native values are converted to CEL values under the covers. |
| 62 | "i": "CEL", |
| 63 | // Values may also be lazily supplied. |
| 64 | "you": func() ref.Val { return types.String("world") }, |
| 65 | }) |
| 66 | if err != nil { |
| 67 | log.Fatalf("runtime error: %s\n", err) |
| 68 | } |
| 69 | |
| 70 | fmt.Println(out) |
| 71 | // Output:Hello world! Nice to meet you, I'm CEL. |
| 72 | } |
| 73 | |
| 74 | func Example_globalOverload() { |
| 75 | // The GlobalOverload example demonstrates how to define global overload function. |
| 76 | // Create the CEL environment with declarations for the input attributes and |
| 77 | // the desired extension functions. In many cases the desired functionality will |
| 78 | // be present in a built-in function. |
| 79 | e, err := cel.NewEnv( |
| 80 | // Identifiers used within this expression. |
| 81 | cel.Variable("i", cel.StringType), |
| 82 | cel.Variable("you", cel.StringType), |
| 83 | // Function to generate shake_hands between two people. |
| 84 | // shake_hands(i,you) |
| 85 | cel.Function("shake_hands", |
| 86 | cel.Overload("shake_hands_string_string", []*cel.Type{cel.StringType, cel.StringType}, cel.StringType, |
| 87 | cel.BinaryBinding(func(arg1, arg2 ref.Val) ref.Val { |
| 88 | return types.String(fmt.Sprintf("%v and %v are shaking hands.\n", arg1, arg2)) |
| 89 | }), |
| 90 | ), |
| 91 | ), |
| 92 | ) |
| 93 | if err != nil { |
| 94 | log.Fatalf("environment creation error: %s\n", err) |
| 95 | } |
| 96 | |
| 97 | // Compile the expression. |
| 98 | ast, iss := e.Compile(`shake_hands(i,you)`) |
| 99 | if iss.Err() != nil { |
| 100 | log.Fatalln(iss.Err()) |
| 101 | } |
| 102 | |
| 103 | // Create the program. |
| 104 | prg, err := e.Program(ast) |
| 105 | if err != nil { |
| 106 | log.Fatalf("program creation error: %s\n", err) |
| 107 | } |
| 108 | |
| 109 | // Evaluate the program against some inputs. Note: the details return is not used. |
| 110 | out, _, err := prg.Eval(map[string]any{ |
| 111 | "i": "CEL", |
| 112 | "you": func() ref.Val { return types.String("world") }, |
| 113 | }) |
| 114 | if err != nil { |
| 115 | log.Fatalf("runtime error: %s\n", err) |
| 116 | } |
| 117 | |
| 118 | fmt.Println(out) |
| 119 | // Output:CEL and world are shaking hands. |
| 120 | } |
| 121 | |
| 122 | func Example_statefulOverload() { |
| 123 | // makeFetch produces a consistent function signature with a different function |
| 124 | // implementation depending on the provided context. |
| 125 | makeFetch := func(ctx any) cel.EnvOption { |
| 126 | fn := func(arg ref.Val) ref.Val { |
| 127 | return types.NewErr("stateful context not bound") |
| 128 | } |
| 129 | if ctx != nil { |
| 130 | fn = func(resource ref.Val) ref.Val { |
| 131 | return types.DefaultTypeAdapter.NativeToValue( |
| 132 | ctx.(context.Context).Value(contextString(string(resource.(types.String)))), |
| 133 | ) |
| 134 | } |
| 135 | } |
| 136 | return cel.Function("fetch", |
| 137 | cel.Overload("fetch_string", |
| 138 | []*cel.Type{cel.StringType}, cel.StringType, |
| 139 | cel.UnaryBinding(fn), |
| 140 | ), |
| 141 | ) |
| 142 | } |
| 143 | |
| 144 | // The base environment declares the fetch function with a dummy binding that errors |
| 145 | // if it is invoked without being replaced by a subsequent call to `baseEnv.Extend` |
| 146 | baseEnv, err := cel.NewEnv( |
| 147 | // Identifiers used within this expression. |
| 148 | cel.Variable("resource", cel.StringType), |
| 149 | // Function to fetch a resource. |
| 150 | // fetch(resource) |
| 151 | makeFetch(nil), |
| 152 | ) |
| 153 | if err != nil { |
| 154 | log.Fatalf("environment creation error: %s\n", err) |
| 155 | } |
| 156 | ast, iss := baseEnv.Compile("fetch('my-resource') == 'my-value'") |
| 157 | if iss.Err() != nil { |
| 158 | log.Fatalf("Compile() failed: %v", iss.Err()) |
| 159 | } |
| 160 | |
| 161 | // The runtime environment extends the base environment with a contextual binding for |
| 162 | // the 'fetch' function. |
| 163 | ctx := context.WithValue(context.TODO(), contextString("my-resource"), "my-value") |
| 164 | runtimeEnv, err := baseEnv.Extend(makeFetch(ctx)) |
| 165 | if err != nil { |
| 166 | log.Fatalf("baseEnv.Extend() failed with error: %s\n", err) |
| 167 | } |
| 168 | prg, err := runtimeEnv.Program(ast) |
| 169 | if err != nil { |
| 170 | log.Fatalf("runtimeEnv.Program() error: %s\n", err) |
| 171 | } |
| 172 | out, _, err := prg.Eval(cel.NoVars()) |
| 173 | if err != nil { |
| 174 | log.Fatalf("runtime error: %s\n", err) |
| 175 | } |
| 176 | |
| 177 | fmt.Println(out) |
| 178 | // Output:true |
| 179 | } |
| 180 | |
| 181 | type contextString string |