File: README.md

package info (click to toggle)
golang-github-google-cel-go 0.18.2%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,888 kB
  • sloc: sh: 93; makefile: 12
file content (115 lines) | stat: -rw-r--r-- 3,328 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Examples

## Simple example using builtin operators

Evaluate expression `"Hello world! I'm " + name + "."` with `CEL` passed as
name.

```go
import "github.com/google/cel-go/cel"

    env, err := cel.NewEnv(cel.Variable("name", cel.StringType))
    // Check err for environment setup errors.
    if err != nil {
        log.Fatalln(err)
    }
    ast, iss := env.Compile(`"Hello world! I'm " + name + "."`)
    // Check iss for compilation errors.
    if iss.Err() != nil {
        log.Fatalln(iss.Err())
    }
    prg, err := env.Program(ast)
    out, _, err := prg.Eval(map[string]interface{}{
        "name":   "CEL",
    })
    fmt.Println(out)
    // Output:Hello world! I'm CEL.
```

[Source code](simple_test.go)

## Custom function on string type

Evaluate expression `i.greet(you)` with:

```
    i       -> CEL
    you     -> world
    greet   -> "Hello %s! Nice to meet you, I'm %s."
```

First we need to declare two string variables and `greet` function.
`Function` must be used if we want to declare an extension to CEL. The
`MemberOverload` declares an overload id, a list of arguments where the
first element the `argTypes` slice is the target type of the member
function. The remaining argument types are the signature of the member
method.

```go
    env, _ := cel.NewEnv(
        cel.Variable("i", cel.StringType),
        cel.Variable("you", cel.StringType),
        cel.Function("greet",
            cel.MemberOverload("string_greet_string",
                []*cel.Type{cel.StringType, cel.StringType},
                cel.StringType,
                cel.BinaryBinding(func (lhs, rhs ref.Val) ref.Val {
                    return types.String(
                        fmt.Sprintf("Hello %s! Nice to meet you, I'm %s.\n", rhs, lhs))
                    },
                ),
            ),
        ),
    )
    prg, _ := env.Program(c)
    out, _, _ := prg.Eval(map[string]interface{}{
        "i": "CEL",
        "you": "world",
    })
    fmt.Println(out)
    // Output:Hello world! Nice to meet you, I'm CEL.
```
[Source code](custom_instance_function_test.go)

## Define custom global function

Evaluate expression `shake_hands(i,you)` with:

```
    i           -> CEL
    you         -> world
    shake_hands -> "%s and %s are shaking hands."
```

In order to declare global function we need to use `Overload` instead
of `MemberOverload` in the `Function` option:

```go
    env, _ := cel.NewEnv(
        cel.Variable("i", cel.StringType),
        cel.Variable("you", cel.StringType),
        cel.Function("shake_hands",
            cel.Overload("shake_hands_string_string",
                []*cel.Type{cel.StringType, cel.StringType},
                cel.StringType,
                cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
                    return types.String(
                        fmt.Sprintf("%s and %s are shaking hands.\n", lhs, rhs))
                    },
                ),
            ),
        ),
    )
    prg, _ := env.Program(c)
    out, _, _ := prg.Eval(map[string]interface{}{
        "i": "CEL",
        "you": "world",
    })
    fmt.Println(out)
    // Output:CEL and world are shaking hands.
```

[Source code](custom_global_function_test.go)

For more examples of how to use CEL, see
[cel_test.go](https://github.com/google/cel-go/tree/master/cel/cel_test.go).