()
| 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. |
nothing calls this directly
no test coverage detected