xref: /aosp_15_r20/external/googleapis/google/api/expr/v1alpha1/checked.proto (revision d5c09012810ac0c9f33fe448fb6da8260d444cc9)
1// Copyright 2023 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
15syntax = "proto3";
16
17package google.api.expr.v1alpha1;
18
19import "google/api/expr/v1alpha1/syntax.proto";
20import "google/protobuf/empty.proto";
21import "google/protobuf/struct.proto";
22
23option cc_enable_arenas = true;
24option go_package = "google.golang.org/genproto/googleapis/api/expr/v1alpha1;expr";
25option java_multiple_files = true;
26option java_outer_classname = "DeclProto";
27option java_package = "com.google.api.expr.v1alpha1";
28
29// Protos for representing CEL declarations and typed checked expressions.
30
31// A CEL expression which has been successfully type checked.
32message CheckedExpr {
33  // A map from expression ids to resolved references.
34  //
35  // The following entries are in this table:
36  //
37  // - An Ident or Select expression is represented here if it resolves to a
38  //   declaration. For instance, if `a.b.c` is represented by
39  //   `select(select(id(a), b), c)`, and `a.b` resolves to a declaration,
40  //   while `c` is a field selection, then the reference is attached to the
41  //   nested select expression (but not to the id or or the outer select).
42  //   In turn, if `a` resolves to a declaration and `b.c` are field selections,
43  //   the reference is attached to the ident expression.
44  // - Every Call expression has an entry here, identifying the function being
45  //   called.
46  // - Every CreateStruct expression for a message has an entry, identifying
47  //   the message.
48  map<int64, Reference> reference_map = 2;
49
50  // A map from expression ids to types.
51  //
52  // Every expression node which has a type different than DYN has a mapping
53  // here. If an expression has type DYN, it is omitted from this map to save
54  // space.
55  map<int64, Type> type_map = 3;
56
57  // The source info derived from input that generated the parsed `expr` and
58  // any optimizations made during the type-checking pass.
59  SourceInfo source_info = 5;
60
61  // The expr version indicates the major / minor version number of the `expr`
62  // representation.
63  //
64  // The most common reason for a version change will be to indicate to the CEL
65  // runtimes that transformations have been performed on the expr during static
66  // analysis. In some cases, this will save the runtime the work of applying
67  // the same or similar transformations prior to evaluation.
68  string expr_version = 6;
69
70  // The checked expression. Semantically equivalent to the parsed `expr`, but
71  // may have structural differences.
72  Expr expr = 4;
73}
74
75// Represents a CEL type.
76message Type {
77  // List type with typed elements, e.g. `list<example.proto.MyMessage>`.
78  message ListType {
79    // The element type.
80    Type elem_type = 1;
81  }
82
83  // Map type with parameterized key and value types, e.g. `map<string, int>`.
84  message MapType {
85    // The type of the key.
86    Type key_type = 1;
87
88    // The type of the value.
89    Type value_type = 2;
90  }
91
92  // Function type with result and arg types.
93  message FunctionType {
94    // Result type of the function.
95    Type result_type = 1;
96
97    // Argument types of the function.
98    repeated Type arg_types = 2;
99  }
100
101  // Application defined abstract type.
102  message AbstractType {
103    // The fully qualified name of this abstract type.
104    string name = 1;
105
106    // Parameter types for this abstract type.
107    repeated Type parameter_types = 2;
108  }
109
110  // CEL primitive types.
111  enum PrimitiveType {
112    // Unspecified type.
113    PRIMITIVE_TYPE_UNSPECIFIED = 0;
114
115    // Boolean type.
116    BOOL = 1;
117
118    // Int64 type.
119    //
120    // Proto-based integer values are widened to int64.
121    INT64 = 2;
122
123    // Uint64 type.
124    //
125    // Proto-based unsigned integer values are widened to uint64.
126    UINT64 = 3;
127
128    // Double type.
129    //
130    // Proto-based float values are widened to double values.
131    DOUBLE = 4;
132
133    // String type.
134    STRING = 5;
135
136    // Bytes type.
137    BYTES = 6;
138  }
139
140  // Well-known protobuf types treated with first-class support in CEL.
141  enum WellKnownType {
142    // Unspecified type.
143    WELL_KNOWN_TYPE_UNSPECIFIED = 0;
144
145    // Well-known protobuf.Any type.
146    //
147    // Any types are a polymorphic message type. During type-checking they are
148    // treated like `DYN` types, but at runtime they are resolved to a specific
149    // message type specified at evaluation time.
150    ANY = 1;
151
152    // Well-known protobuf.Timestamp type, internally referenced as `timestamp`.
153    TIMESTAMP = 2;
154
155    // Well-known protobuf.Duration type, internally referenced as `duration`.
156    DURATION = 3;
157  }
158
159  // The kind of type.
160  oneof type_kind {
161    // Dynamic type.
162    google.protobuf.Empty dyn = 1;
163
164    // Null value.
165    google.protobuf.NullValue null = 2;
166
167    // Primitive types: `true`, `1u`, `-2.0`, `'string'`, `b'bytes'`.
168    PrimitiveType primitive = 3;
169
170    // Wrapper of a primitive type, e.g. `google.protobuf.Int64Value`.
171    PrimitiveType wrapper = 4;
172
173    // Well-known protobuf type such as `google.protobuf.Timestamp`.
174    WellKnownType well_known = 5;
175
176    // Parameterized list with elements of `list_type`, e.g. `list<timestamp>`.
177    ListType list_type = 6;
178
179    // Parameterized map with typed keys and values.
180    MapType map_type = 7;
181
182    // Function type.
183    FunctionType function = 8;
184
185    // Protocol buffer message type.
186    //
187    // The `message_type` string specifies the qualified message type name. For
188    // example, `google.plus.Profile`.
189    string message_type = 9;
190
191    // Type param type.
192    //
193    // The `type_param` string specifies the type parameter name, e.g. `list<E>`
194    // would be a `list_type` whose element type was a `type_param` type
195    // named `E`.
196    string type_param = 10;
197
198    // Type type.
199    //
200    // The `type` value specifies the target type. e.g. int is type with a
201    // target type of `Primitive.INT`.
202    Type type = 11;
203
204    // Error type.
205    //
206    // During type-checking if an expression is an error, its type is propagated
207    // as the `ERROR` type. This permits the type-checker to discover other
208    // errors present in the expression.
209    google.protobuf.Empty error = 12;
210
211    // Abstract, application defined type.
212    AbstractType abstract_type = 14;
213  }
214}
215
216// Represents a declaration of a named value or function.
217//
218// A declaration is part of the contract between the expression, the agent
219// evaluating that expression, and the caller requesting evaluation.
220message Decl {
221  // Identifier declaration which specifies its type and optional `Expr` value.
222  //
223  // An identifier without a value is a declaration that must be provided at
224  // evaluation time. An identifier with a value should resolve to a constant,
225  // but may be used in conjunction with other identifiers bound at evaluation
226  // time.
227  message IdentDecl {
228    // Required. The type of the identifier.
229    Type type = 1;
230
231    // The constant value of the identifier. If not specified, the identifier
232    // must be supplied at evaluation time.
233    Constant value = 2;
234
235    // Documentation string for the identifier.
236    string doc = 3;
237  }
238
239  // Function declaration specifies one or more overloads which indicate the
240  // function's parameter types and return type.
241  //
242  // Functions have no observable side-effects (there may be side-effects like
243  // logging which are not observable from CEL).
244  message FunctionDecl {
245    // An overload indicates a function's parameter types and return type, and
246    // may optionally include a function body described in terms of
247    // [Expr][google.api.expr.v1alpha1.Expr] values.
248    //
249    // Functions overloads are declared in either a function or method
250    // call-style. For methods, the `params[0]` is the expected type of the
251    // target receiver.
252    //
253    // Overloads must have non-overlapping argument types after erasure of all
254    // parameterized type variables (similar as type erasure in Java).
255    message Overload {
256      // Required. Globally unique overload name of the function which reflects
257      // the function name and argument types.
258      //
259      // This will be used by a [Reference][google.api.expr.v1alpha1.Reference]
260      // to indicate the `overload_id` that was resolved for the function
261      // `name`.
262      string overload_id = 1;
263
264      // List of function parameter [Type][google.api.expr.v1alpha1.Type]
265      // values.
266      //
267      // Param types are disjoint after generic type parameters have been
268      // replaced with the type `DYN`. Since the `DYN` type is compatible with
269      // any other type, this means that if `A` is a type parameter, the
270      // function types `int<A>` and `int<int>` are not disjoint. Likewise,
271      // `map<string, string>` is not disjoint from `map<K, V>`.
272      //
273      // When the `result_type` of a function is a generic type param, the
274      // type param name also appears as the `type` of on at least one params.
275      repeated Type params = 2;
276
277      // The type param names associated with the function declaration.
278      //
279      // For example, `function ex<K,V>(K key, map<K, V> map) : V` would yield
280      // the type params of `K, V`.
281      repeated string type_params = 3;
282
283      // Required. The result type of the function. For example, the operator
284      // `string.isEmpty()` would have `result_type` of `kind: BOOL`.
285      Type result_type = 4;
286
287      // Whether the function is to be used in a method call-style `x.f(...)`
288      // or a function call-style `f(x, ...)`.
289      //
290      // For methods, the first parameter declaration, `params[0]` is the
291      // expected type of the target receiver.
292      bool is_instance_function = 5;
293
294      // Documentation string for the overload.
295      string doc = 6;
296    }
297
298    // Required. List of function overloads, must contain at least one overload.
299    repeated Overload overloads = 1;
300  }
301
302  // The fully qualified name of the declaration.
303  //
304  // Declarations are organized in containers and this represents the full path
305  // to the declaration in its container, as in `google.api.expr.Decl`.
306  //
307  // Declarations used as
308  // [FunctionDecl.Overload][google.api.expr.v1alpha1.Decl.FunctionDecl.Overload]
309  // parameters may or may not have a name depending on whether the overload is
310  // function declaration or a function definition containing a result
311  // [Expr][google.api.expr.v1alpha1.Expr].
312  string name = 1;
313
314  // Required. The declaration kind.
315  oneof decl_kind {
316    // Identifier declaration.
317    IdentDecl ident = 2;
318
319    // Function declaration.
320    FunctionDecl function = 3;
321  }
322}
323
324// Describes a resolved reference to a declaration.
325message Reference {
326  // The fully qualified name of the declaration.
327  string name = 1;
328
329  // For references to functions, this is a list of `Overload.overload_id`
330  // values which match according to typing rules.
331  //
332  // If the list has more than one element, overload resolution among the
333  // presented candidates must happen at runtime because of dynamic types. The
334  // type checker attempts to narrow down this list as much as possible.
335  //
336  // Empty if this is not a reference to a
337  // [Decl.FunctionDecl][google.api.expr.v1alpha1.Decl.FunctionDecl].
338  repeated string overload_id = 3;
339
340  // For references to constants, this may contain the value of the
341  // constant if known at compile time.
342  Constant value = 4;
343}
344