expr.proto 8.7 KB

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