Skip to content

Commit e2eac8d

Browse files
committed
Adding CEL
1 parent ce9b4f8 commit e2eac8d

27 files changed

Lines changed: 5081 additions & 0 deletions
Lines changed: 392 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,392 @@
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+
15+
syntax = "proto3";
16+
17+
package cel.expr;
18+
19+
import "cel/expr/syntax.proto";
20+
import "google/protobuf/empty.proto";
21+
import "google/protobuf/struct.proto";
22+
23+
option cc_enable_arenas = true;
24+
option go_package = "cel.dev/expr";
25+
option java_multiple_files = true;
26+
option java_outer_classname = "DeclProto";
27+
option java_package = "dev.cel.expr";
28+
29+
// Protos for representing CEL declarations and typed checked expressions.
30+
31+
// A CEL expression which has been successfully type checked.
32+
message 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.
76+
message 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+
// 32-bit integer values are widened to int64.
121+
INT64 = 2;
122+
123+
// Uint64 type.
124+
//
125+
// 32-bit unsigned integer values are widened to uint64.
126+
UINT64 = 3;
127+
128+
// Double type.
129+
//
130+
// 32-bit 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.type.PhoneNumber`.
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.INT64`.
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+
//
213+
// An abstract type has no accessible field names, and it can only be
214+
// inspected via helper / member functions.
215+
AbstractType abstract_type = 14;
216+
}
217+
}
218+
219+
// Represents a declaration of a named value or function.
220+
//
221+
// A declaration is part of the contract between the expression, the agent
222+
// evaluating that expression, and the caller requesting evaluation.
223+
message Decl {
224+
// Identifier declaration which specifies its type and optional `Expr` value.
225+
//
226+
// An identifier without a value is a declaration that must be provided at
227+
// evaluation time. An identifier with a value should resolve to a constant,
228+
// but may be used in conjunction with other identifiers bound at evaluation
229+
// time.
230+
message IdentDecl {
231+
// Required. The type of the identifier.
232+
Type type = 1;
233+
234+
// The constant value of the identifier. If not specified, the identifier
235+
// must be supplied at evaluation time.
236+
Constant value = 2;
237+
238+
// Documentation string for the identifier.
239+
//
240+
// Provide a brief description of what the variable represents and whether
241+
// there are any constraints on the formatting or supported value range.
242+
//
243+
// Examples:
244+
//
245+
// 'request.auth.principal' - string which uniquely identifies an
246+
// authenticated principal. For JSON Web Tokens (JWTs), the principal
247+
// is the combination of the issuer ('iss') and subject ('sub') token
248+
// fields concatenated by a forward slash: iss + `/` + sub.
249+
//
250+
// 'min_cpus' - integer value indicates the minimum number of CPUs
251+
// required for a compute cluster. The 'min_cpus' value must be
252+
// greater than zero and less than 'max_cpus' or 64 whichever is less.
253+
string doc = 3;
254+
}
255+
256+
// Function declaration specifies one or more overloads which indicate the
257+
// function's parameter types and return type.
258+
//
259+
// Functions have no observable side-effects (there may be side-effects like
260+
// logging which are not observable from CEL).
261+
message FunctionDecl {
262+
// An overload indicates a function's parameter types and return type, and
263+
// may optionally include a function body described in terms of
264+
// [Expr][cel.expr.Expr] values.
265+
//
266+
// Functions overloads are declared in either a function or method
267+
// call-style. For methods, the `params[0]` is the expected type of the
268+
// target receiver.
269+
//
270+
// Overloads must have non-overlapping argument types after erasure of all
271+
// parameterized type variables (similar as type erasure in Java).
272+
message Overload {
273+
// Required. Globally unique overload name of the function which reflects
274+
// the function name and argument types.
275+
//
276+
// This will be used by a [Reference][cel.expr.Reference] to
277+
// indicate the `overload_id` that was resolved for the function `name`.
278+
string overload_id = 1;
279+
280+
// List of function parameter [Type][cel.expr.Type] values.
281+
//
282+
// Param types are disjoint after generic type parameters have been
283+
// replaced with the type `DYN`. Since the `DYN` type is compatible with
284+
// any other type, this means that if `A` is a type parameter, the
285+
// function types `int<A>` and `int<int>` are not disjoint. Likewise,
286+
// `map<string, string>` is not disjoint from `map<K, V>`.
287+
//
288+
// When the `result_type` of a function is a generic type param, the
289+
// type param name also appears as the `type` of on at least one params.
290+
repeated Type params = 2;
291+
292+
// The type param names associated with the function declaration.
293+
//
294+
// For example, `function ex<K,V>(K key, map<K, V> map) : V` would yield
295+
// the type params of `K, V`.
296+
repeated string type_params = 3;
297+
298+
// Required. The result type of the function. For example, the operator
299+
// `string.isEmpty()` would have `result_type` of `kind: BOOL`.
300+
Type result_type = 4;
301+
302+
// Whether the function is to be used in a method call-style `x.f(...)`
303+
// of a function call-style `f(x, ...)`.
304+
//
305+
// For methods, the first parameter declaration, `params[0]` is the
306+
// expected type of the target receiver.
307+
bool is_instance_function = 5;
308+
309+
// Documentation string for the overload.
310+
//
311+
// Provide examples of the overload behavior, preferring to use literal
312+
// values as input with a comment on the return value.
313+
//
314+
// Examples:
315+
//
316+
// // Determine whether a value of type <V> exists within a list<V>.
317+
// 2 in [1, 2, 3] // returns true
318+
//
319+
// // Determine whether a key of type <K> exists within a map<K,V>.
320+
// 'hello' in {'hi': 'you', 'hello': 'there'} // returns true
321+
// 'help' in {'hi': 'you', 'hello': 'there'} // returns false
322+
//
323+
// // Take the substring of a string starting at a specific character
324+
// // offset (inclusive).
325+
// "tacocat".substring(1) // returns "acocat"
326+
// "tacocat".substring(20) // error
327+
//
328+
// // Take the substring of a string starting at a specific character
329+
// // offset (inclusive) and ending at the given offset (exclusive).
330+
// "tacocat".substring(1, 6) // returns "acoca"
331+
string doc = 6;
332+
}
333+
334+
// Required. List of function overloads, must contain at least one overload.
335+
repeated Overload overloads = 1;
336+
337+
// Documentation string for the function that indicates the general purpose
338+
// of the function and its behavior.
339+
//
340+
// Documentation strings for the function should be general purpose with
341+
// specific examples provided in the overload doc string.
342+
//
343+
// Examples:
344+
//
345+
// The 'in' operator tests whether an item exists in a collection.
346+
//
347+
// The 'substring' function returns a substring of a target string.
348+
string doc = 2;
349+
}
350+
351+
// The fully qualified name of the declaration.
352+
//
353+
// Declarations are organized in containers and this represents the full path
354+
// to the declaration in its container, as in `cel.expr.Decl`.
355+
//
356+
// Declarations used as
357+
// [FunctionDecl.Overload][cel.expr.Decl.FunctionDecl.Overload]
358+
// parameters may or may not have a name depending on whether the overload is
359+
// function declaration or a function definition containing a result
360+
// [Expr][cel.expr.Expr].
361+
string name = 1;
362+
363+
// Required. The declaration kind.
364+
oneof decl_kind {
365+
// Identifier declaration.
366+
IdentDecl ident = 2;
367+
368+
// Function declaration.
369+
FunctionDecl function = 3;
370+
}
371+
}
372+
373+
// Describes a resolved reference to a declaration.
374+
message Reference {
375+
// The fully qualified name of the declaration.
376+
string name = 1;
377+
378+
// For references to functions, this is a list of `Overload.overload_id`
379+
// values which match according to typing rules.
380+
//
381+
// If the list has more than one element, overload resolution among the
382+
// presented candidates must happen at runtime because of dynamic types. The
383+
// type checker attempts to narrow down this list as much as possible.
384+
//
385+
// Empty if this is not a reference to a
386+
// [Decl.FunctionDecl][cel.expr.Decl.FunctionDecl].
387+
repeated string overload_id = 3;
388+
389+
// For references to constants, this may contain the value of the
390+
// constant if known at compile time.
391+
Constant value = 4;
392+
}

0 commit comments

Comments
 (0)