1// Copyright 2019 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 16syntax = "proto3"; 17 18package google.api.expr.v1beta1; 19 20import "google/api/expr/v1beta1/source.proto"; 21import "google/protobuf/struct.proto"; 22 23option cc_enable_arenas = true; 24option go_package = "google.golang.org/genproto/googleapis/api/expr/v1beta1;expr"; 25option java_multiple_files = true; 26option java_outer_classname = "ExprProto"; 27option java_package = "com.google.api.expr.v1beta1"; 28 29// An expression together with source information as returned by the parser. 30message ParsedExpr { 31 // The parsed expression. 32 Expr expr = 2; 33 34 // The source info derived from input that generated the parsed `expr`. 35 SourceInfo source_info = 3; 36 37 // The syntax version of the source, e.g. `cel1`. 38 string syntax_version = 4; 39} 40 41// An abstract representation of a common expression. 42// 43// Expressions are abstractly represented as a collection of identifiers, 44// select statements, function calls, literals, and comprehensions. All 45// operators with the exception of the '.' operator are modelled as function 46// calls. This makes it easy to represent new operators into the existing AST. 47// 48// All references within expressions must resolve to a [Decl][google.api.expr.v1beta1.Decl] provided at 49// type-check for an expression to be valid. A reference may either be a bare 50// identifier `name` or a qualified identifier `google.api.name`. References 51// may either refer to a value or a function declaration. 52// 53// For example, the expression `google.api.name.startsWith('expr')` references 54// the declaration `google.api.name` within a [Expr.Select][google.api.expr.v1beta1.Expr.Select] expression, and 55// the function declaration `startsWith`. 56message Expr { 57 // An identifier expression. e.g. `request`. 58 message Ident { 59 // Required. Holds a single, unqualified identifier, possibly preceded by a 60 // '.'. 61 // 62 // Qualified names are represented by the [Expr.Select][google.api.expr.v1beta1.Expr.Select] expression. 63 string name = 1; 64 } 65 66 // A field selection expression. e.g. `request.auth`. 67 message Select { 68 // Required. The target of the selection expression. 69 // 70 // For example, in the select expression `request.auth`, the `request` 71 // portion of the expression is the `operand`. 72 Expr operand = 1; 73 74 // Required. The name of the field to select. 75 // 76 // For example, in the select expression `request.auth`, the `auth` portion 77 // of the expression would be the `field`. 78 string field = 2; 79 80 // Whether the select is to be interpreted as a field presence test. 81 // 82 // This results from the macro `has(request.auth)`. 83 bool test_only = 3; 84 } 85 86 // A call expression, including calls to predefined functions and operators. 87 // 88 // For example, `value == 10`, `size(map_value)`. 89 message Call { 90 // The target of an method call-style expression. For example, `x` in 91 // `x.f()`. 92 Expr target = 1; 93 94 // Required. The name of the function or method being called. 95 string function = 2; 96 97 // The arguments. 98 repeated Expr args = 3; 99 } 100 101 // A list creation expression. 102 // 103 // Lists may either be homogenous, e.g. `[1, 2, 3]`, or heterogenous, e.g. 104 // `dyn([1, 'hello', 2.0])` 105 message CreateList { 106 // The elements part of the list. 107 repeated Expr elements = 1; 108 } 109 110 // A map or message creation expression. 111 // 112 // Maps are constructed as `{'key_name': 'value'}`. Message construction is 113 // similar, but prefixed with a type name and composed of field ids: 114 // `types.MyType{field_id: 'value'}`. 115 message CreateStruct { 116 // Represents an entry. 117 message Entry { 118 // Required. An id assigned to this node by the parser which is unique 119 // in a given expression tree. This is used to associate type 120 // information and other attributes to the node. 121 int32 id = 1; 122 123 // The `Entry` key kinds. 124 oneof key_kind { 125 // The field key for a message creator statement. 126 string field_key = 2; 127 128 // The key expression for a map creation statement. 129 Expr map_key = 3; 130 } 131 132 // Required. The value assigned to the key. 133 Expr value = 4; 134 } 135 136 // The type name of the message to be created, empty when creating map 137 // literals. 138 string type = 1; 139 140 // The entries in the creation expression. 141 repeated Entry entries = 2; 142 } 143 144 // A comprehension expression applied to a list or map. 145 // 146 // Comprehensions are not part of the core syntax, but enabled with macros. 147 // A macro matches a specific call signature within a parsed AST and replaces 148 // the call with an alternate AST block. Macro expansion happens at parse 149 // time. 150 // 151 // The following macros are supported within CEL: 152 // 153 // Aggregate type macros may be applied to all elements in a list or all keys 154 // in a map: 155 // 156 // * `all`, `exists`, `exists_one` - test a predicate expression against 157 // the inputs and return `true` if the predicate is satisfied for all, 158 // any, or only one value `list.all(x, x < 10)`. 159 // * `filter` - test a predicate expression against the inputs and return 160 // the subset of elements which satisfy the predicate: 161 // `payments.filter(p, p > 1000)`. 162 // * `map` - apply an expression to all elements in the input and return the 163 // output aggregate type: `[1, 2, 3].map(i, i * i)`. 164 // 165 // The `has(m.x)` macro tests whether the property `x` is present in struct 166 // `m`. The semantics of this macro depend on the type of `m`. For proto2 167 // messages `has(m.x)` is defined as 'defined, but not set`. For proto3, the 168 // macro tests whether the property is set to its default. For map and struct 169 // types, the macro tests whether the property `x` is defined on `m`. 170 message Comprehension { 171 // The name of the iteration variable. 172 string iter_var = 1; 173 174 // The range over which var iterates. 175 Expr iter_range = 2; 176 177 // The name of the variable used for accumulation of the result. 178 string accu_var = 3; 179 180 // The initial value of the accumulator. 181 Expr accu_init = 4; 182 183 // An expression which can contain iter_var and accu_var. 184 // 185 // Returns false when the result has been computed and may be used as 186 // a hint to short-circuit the remainder of the comprehension. 187 Expr loop_condition = 5; 188 189 // An expression which can contain iter_var and accu_var. 190 // 191 // Computes the next value of accu_var. 192 Expr loop_step = 6; 193 194 // An expression which can contain accu_var. 195 // 196 // Computes the result. 197 Expr result = 7; 198 } 199 200 // Required. An id assigned to this node by the parser which is unique in a 201 // given expression tree. This is used to associate type information and other 202 // attributes to a node in the parse tree. 203 int32 id = 2; 204 205 // Required. Variants of expressions. 206 oneof expr_kind { 207 // A literal expression. 208 Literal literal_expr = 3; 209 210 // An identifier expression. 211 Ident ident_expr = 4; 212 213 // A field selection expression, e.g. `request.auth`. 214 Select select_expr = 5; 215 216 // A call expression, including calls to predefined functions and operators. 217 Call call_expr = 6; 218 219 // A list creation expression. 220 CreateList list_expr = 7; 221 222 // A map or object creation expression. 223 CreateStruct struct_expr = 8; 224 225 // A comprehension expression. 226 Comprehension comprehension_expr = 9; 227 } 228} 229 230// Represents a primitive literal. 231// 232// This is similar to the primitives supported in the well-known type 233// `google.protobuf.Value`, but richer so it can represent CEL's full range of 234// primitives. 235// 236// Lists and structs are not included as constants as these aggregate types may 237// contain [Expr][google.api.expr.v1beta1.Expr] elements which require evaluation and are thus not constant. 238// 239// Examples of literals include: `"hello"`, `b'bytes'`, `1u`, `4.2`, `-2`, 240// `true`, `null`. 241message Literal { 242 // Required. The valid constant kinds. 243 oneof constant_kind { 244 // null value. 245 google.protobuf.NullValue null_value = 1; 246 247 // boolean value. 248 bool bool_value = 2; 249 250 // int64 value. 251 int64 int64_value = 3; 252 253 // uint64 value. 254 uint64 uint64_value = 4; 255 256 // double value. 257 double double_value = 5; 258 259 // string value. 260 string string_value = 6; 261 262 // bytes value. 263 bytes bytes_value = 7; 264 } 265} 266