| 1 | # CEL REPL |
| 2 | |
| 3 | This is a simple tool for experimenting with CEL expressions and learning the |
| 4 | syntax. |
| 5 | |
| 6 | ## Usage |
| 7 | The REPL (Read Evaluate Print Loop) is implemented as a command line tool. |
| 8 | |
| 9 | By default, the the input will be interpreted as a cel expression to evaluate. |
| 10 | |
| 11 | Special commands (prefixed with '%') are used to update the evaluation |
| 12 | environment. |
| 13 | |
| 14 | An example session: |
| 15 | |
| 16 | ``` |
| 17 | # from a cel-go clone |
| 18 | $ cd ./repl/main |
| 19 | |
| 20 | $ go run . |
| 21 | CEL REPL |
| 22 | %exit or EOF to quit. |
| 23 | |
| 24 | cel-repl> %let x = 10 |
| 25 | cel-repl> %let y = {'abc': {'def': [1, 2, 3]}} |
| 26 | cel-repl> y.abc.def.filter(el, el < x) |
| 27 | [1 2 3] : list(int) |
| 28 | cel-repl> %delete x |
| 29 | cel-repl> y.abc.def.filter(el, el < x) |
| 30 | Expr failed: |
| 31 | ERROR: <input>:1:27: undeclared reference to 'x' (in container '') |
| 32 | | y.abc.def.filter(el, el < x) |
| 33 | | ..........................^ |
| 34 | cel-repl> %declare x : int |
| 35 | cel-repl> y.abc.def.filter(el, el < x) |
| 36 | Expr failed: |
| 37 | no such attribute: id: 8, names: [x] |
| 38 | no such attribute: id: 8, names: [x] : list(int) |
| 39 | cel-repl> y.abc.def.filter(el, el < x || el > 0) |
| 40 | [1 2 3] : list(int) |
| 41 | cel-repl> %exit |
| 42 | ``` |
| 43 | |
| 44 | ### Commands |
| 45 | |
| 46 | #### compile |
| 47 | |
| 48 | `%compile` compiles the input expression using the currently configured state |
| 49 | into a protocol buffer text format representing the type-checked AST. |
| 50 | |
| 51 | `%compile <expr>` |
| 52 | |
| 53 | Example: |
| 54 | |
| 55 | ``` |
| 56 | > %compile 3u |
| 57 | type_map: { |
| 58 | key: 1 |
| 59 | value: { |
| 60 | primitive: UINT64 |
| 61 | } |
| 62 | } |
| 63 | source_info: { |
| 64 | location: "<input>" |
| 65 | line_offsets: 3 |
| 66 | positions: { |
| 67 | key: 1 |
| 68 | value: 0 |
| 69 | } |
| 70 | } |
| 71 | expr: { |
| 72 | id: 1 |
| 73 | const_expr: { |
| 74 | uint64_value: 3 |
| 75 | } |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | #### let |
| 80 | `%let` introduces or update a variable or function declaration and provide a |
| 81 | definition (as another CEL expression). A type hint is optionally provided to |
| 82 | check that the provided expression has the expected type. |
| 83 | |
| 84 | `%let <identifier> (: <type>)? = <expr>` |
| 85 | |
| 86 | Example: |
| 87 | |
| 88 | `%let y = 42` |
| 89 | |
| 90 | For functions, result types are mandatory: |
| 91 | |
| 92 | `%let <identifier> (<identifier> : <type>, ...) : <type> -> <expr>` |
| 93 | |
| 94 | Example: |
| 95 | |
| 96 | `%let oracle(x : int) : bool -> x == 42` |
| 97 | |
| 98 | Instance functions are declared as `<type>.<identifier>(...): <type>` and may |
| 99 | reference `this` as the receiver instance. |
| 100 | |
| 101 | Example: |
| 102 | |
| 103 | ``` |
| 104 | > %let int.oracle() : bool -> this == 42 |
| 105 | > 42.oracle() |
| 106 | true : bool |
| 107 | > 41.oracle() |
| 108 | false : bool |
| 109 | ``` |
| 110 | |
| 111 | #### declare |
| 112 | |
| 113 | `%declare` introduces or updates a variable or function declaration with no |
| 114 | definition. |
| 115 | |
| 116 | `%declare <identifier> : <type>` |
| 117 | |
| 118 | `%declare <identifier> (<identifier> : <type>, ...) : <type>` |
| 119 | |
| 120 | #### delete |
| 121 | `%delete` deletes a variable declaration |
| 122 | |
| 123 | `%delete <identifier>` |
| 124 | |
| 125 | #### eval |
| 126 | `%eval` evaluate an expression: |
| 127 | |
| 128 | `%eval <expr>` or simply `<expr>` |
| 129 | |
| 130 | #### status |
| 131 | |
| 132 | `%status` prints a list of existing lets in the evaluation context. |
| 133 | |
| 134 | #### load_descriptors |
| 135 | |
| 136 | `%load_descriptors` loads a file descriptor set from file into the context (a |
| 137 | google.protobuf.FileDescriptorSet message). Message types from the file are |
| 138 | available for use in later expressions as CEL structs. |
| 139 | |
| 140 | `--textproto` expect the file format as protobuf text format. |
| 141 | |
| 142 | `--binarypb` expect the file format as serialized protobuf. |
| 143 | |
| 144 | `--pkg <string>` Alternatively, a few well known types are included in the |
| 145 | binary and can be added with the `--pkg` flag. Available packages: `google-rpc`, |
| 146 | `cel-spec-test-types`. |
| 147 | |
| 148 | example: |
| 149 | |
| 150 | `%load_descriptors --textproto "./testdata/attribute_context_fds.textproto"` |
| 151 | |
| 152 | `%load_descriptors --pkg "google-rpc"` |
| 153 | |
| 154 | #### option |
| 155 | |
| 156 | `%option` sets an environment option. Options are specified with flags that |
| 157 | may take string arguments. |
| 158 | |
| 159 | `--container <string>` sets the expression container for name resolution. |
| 160 | |
| 161 | `--extension <extensionType>` enables CEL extensions. Valid options are: |
| 162 | `strings`, `protos`, `math`, `encoders`, `optional`, `bindings`, |
| 163 | `two_var_comprehensions` and `all` (enables all extensions). |
| 164 | |
| 165 | `--enable_partial_eval` enables partial evaluations |
| 166 | |
| 167 | `--enable_escaped_fields` enables field escape syntax `` {"foo.bar": 1}.`foo.bar` `` |
| 168 | |
| 169 | example: |
| 170 | |
| 171 | `%option --container 'google.protobuf'` |
| 172 | |
| 173 | `%option --extension 'strings'` |
| 174 | |
| 175 | `%option --extension 'all'` (Loads all extensions) |
| 176 | |
| 177 | `%option --enable_partial_eval` |
| 178 | |
| 179 | #### reset |
| 180 | |
| 181 | `%reset` drops all options and let expressions, returning the evaluator to a |
| 182 | starting empty state. |
| 183 | |
| 184 | ### Evaluation Model |
| 185 | |
| 186 | The evaluator considers the let expressions and declarations in order, with |
| 187 | functions defined before variables. Let expressions may refer to earlier |
| 188 | expressions, but the reverse is not true. To prevent breaking dependant |
| 189 | expressions, updates will fail if removing or changing a let prevents a later |
| 190 | let expression from compiling. Let expressions are compiled when declared and |
| 191 | evaluated before the expression in an `%eval` command. |
| 192 | |
| 193 | Functions are implicitly defined before variables: let variables may refer to |
| 194 | functions, but functions cannot refer to let variables. |
| 195 | |
| 196 | Using curly-braces to indicate scopes, this looks like: |
| 197 | ``` |
| 198 | let sum (x : int, y : int) : int -> x + y |
| 199 | { |
| 200 | let x = sum(2, 4) |
| 201 | { |
| 202 | let y = sum(x, 30) |
| 203 | { |
| 204 | eval sum(x, y) == 42 |
| 205 | // (x) + (x + 30) |
| 206 | // (2 + 4) + ((2 + 4) + 30) |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | ``` |
| 211 | |
| 212 | ## Installing |
| 213 | |
| 214 | To build and install as a standalone binary: |
| 215 | |
| 216 | ``` |
| 217 | $ git clone [email protected]:google/cel-go.git ./cel-go |
| 218 | $ cd ./cel-go/repl/main |
| 219 | $ go build -o repl . |
| 220 | # e.g. to your $PATH |
| 221 | $ mv ./repl <install location> |
| 222 | ``` |