MCPcopy Create free account
hub / github.com/cel-expr/cel-go / Example_cel_Overload

Function Example_cel_Overload

examples/example_cel_overload_test.go:26–65  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

24)
25
26func Example_cel_Overload() {
27 env, err := cel.NewEnv(
28 cel.Variable("i", cel.StringType),
29 cel.Variable("you", cel.StringType),
30 cel.Function("shake_hands",
31 cel.Overload("shake_hands_string_string",
32 []*cel.Type{cel.StringType, cel.StringType},
33 cel.StringType,
34 cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
35 return types.String(
36 fmt.Sprintf("%s and %s are shaking hands.\n", lhs, rhs))
37 },
38 ),
39 ),
40 ),
41 )
42 if err != nil {
43 log.Fatalf("environment creation error: %v\n", err)
44 }
45 // Check iss for error in both Parse and Check.
46 ast, iss := env.Compile(`shake_hands(i,you)`)
47 if iss.Err() != nil {
48 log.Fatalln(iss.Err())
49 }
50 prg, err := env.Program(ast)
51 if err != nil {
52 log.Fatalf("Program creation error: %v\n", err)
53 }
54
55 out, _, err := prg.Eval(map[string]any{
56 "i": "CEL",
57 "you": "world",
58 })
59 if err != nil {
60 log.Fatalf("Evaluation error: %v\n", err)
61 }
62
63 fmt.Println(out)
64 // Output:CEL and world are shaking hands.
65}

Callers

nothing calls this directly

Calls 10

CompileMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
VariableFunction · 0.92
FunctionFunction · 0.92
OverloadFunction · 0.92
BinaryBindingFunction · 0.92
StringTypeAlias · 0.92
ErrMethod · 0.80
EvalMethod · 0.65

Tested by

no test coverage detected