syntax.proto 11 KB

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