Debian Codegraph @

examples/example_cel_overload_test.go

raw symbols SCIP
1 // Copyright 2020 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 examples
16
17 import (
18 "fmt"
19 "log"
20
21 "github.com/google/cel-go/cel"
22 "github.com/google/cel-go/common/types"
23 "github.com/google/cel-go/common/types/ref"
24 )
25
26 func 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 }