-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathCelBuilder.java
More file actions
318 lines (277 loc) · 12.4 KB
/
Copy pathCelBuilder.java
File metadata and controls
318 lines (277 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dev.cel.bundle;
import dev.cel.expr.Decl;
import dev.cel.expr.Type;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.protobuf.DescriptorProtos.FileDescriptorSet;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.Message;
import dev.cel.checker.ProtoTypeMask;
import dev.cel.checker.TypeProvider;
import dev.cel.common.CelContainer;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOptions;
import dev.cel.common.CelVarDecl;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.values.CelValueProvider;
import dev.cel.compiler.CelCompilerLibrary;
import dev.cel.parser.CelMacro;
import dev.cel.parser.CelStandardMacro;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.runtime.CelRuntimeLibrary;
import java.util.function.Function;
/** Interface for building an instance of Cel. */
public interface CelBuilder {
/** Set the {@code CelOptions} used to enable fixes and feautres for this CEL instance. */
@CanIgnoreReturnValue
CelBuilder setOptions(CelOptions options);
/** Set the {@link CelStandardMacro} values for use with this instance. */
@CanIgnoreReturnValue
CelBuilder setStandardMacros(CelStandardMacro... macros);
/** Set the {@link CelStandardMacro} values for use with this instance. */
@CanIgnoreReturnValue
CelBuilder setStandardMacros(Iterable<CelStandardMacro> macros);
/**
* Registers the given macros, replacing any previous macros with the same key.
*
* <p>Use this to register a set of user-defined custom macro implementation for the parser. For
* registering macros defined as part of CEL standard library, use {@link #setStandardMacros}
* instead.
*
* <p>Custom macros should not use the same function names as the ones found in {@link
* CelStandardMacro} (ex: has, all, exists, etc.). Build method will throw if both standard macros
* and custom macros are set with the same name.
*/
@CanIgnoreReturnValue
CelBuilder addMacros(CelMacro... macros);
/**
* Registers the given macros, replacing any previous macros with the same key.
*
* <p>Use this to register a set of user-defined custom macro implementation for the parser. For
* registering macros defined as part of CEL standard library, use {@link #setStandardMacros}
* instead.
*
* <p>Custom macros should not use the same function names as the ones found in {@link
* CelStandardMacro} (ex: has, all, exists, etc.). Build method will throw if both standard macros
* and custom macros are set with the same name.
*/
@CanIgnoreReturnValue
CelBuilder addMacros(Iterable<CelMacro> macros);
/**
* @deprecated Use {@link #setContainer(CelContainer)} instead.
*/
@CanIgnoreReturnValue
@Deprecated
CelBuilder setContainer(String container);
/** Retrieves the currently configured {@link CelContainer} in the builder. */
CelContainer container();
/**
* Set the {@link CelContainer} to use as the namespace for resolving CEL expression variables and
* functions.
*/
@CanIgnoreReturnValue
CelBuilder setContainer(CelContainer container);
/** Add a variable declaration with a given {@code name} and {@link Type}. */
@CanIgnoreReturnValue
CelBuilder addVar(String name, Type type);
/** Add a variable declaration with a given {@code name} and {@link CelType}. */
@CanIgnoreReturnValue
CelBuilder addVar(String name, CelType type);
/** Add variable and function {@code declarations} to the CEL environment. */
@CanIgnoreReturnValue
CelBuilder addDeclarations(Decl... declarations);
/** Add variable and function {@code declarations} to the CEL environment. */
@CanIgnoreReturnValue
CelBuilder addDeclarations(Iterable<Decl> declarations);
/** Add function declaration {@code CelFunctionDecl} to the CEL environment. */
@CanIgnoreReturnValue
CelBuilder addFunctionDeclarations(CelFunctionDecl... celFunctionDecls);
/** Add function declaration {@code CelFunctionDecl} to the CEL environment. */
@CanIgnoreReturnValue
CelBuilder addFunctionDeclarations(Iterable<CelFunctionDecl> celFunctionDecls);
/** Add variable declaration {@code CelVarDecl} to the CEL environment. */
@CanIgnoreReturnValue
CelBuilder addVarDeclarations(CelVarDecl... celVarDecls);
/** Add variable declaration {@code CelVarDecl} to the CEL environment. */
@CanIgnoreReturnValue
CelBuilder addVarDeclarations(Iterable<CelVarDecl> celVarDecls);
/**
* Add one or more {@link ProtoTypeMask} values. The {@code ProtoTypeMask} values will be used to
* compute a set of {@code Decl} values using a protobuf message's fields as the names and types
* of the variables if {@link ProtoTypeMask#fieldsAreVariableDeclarations} is {@code true}.
*
* <p>Note, this feature may not work with custom {@link TypeProvider} implementations out of the
* box, as it requires the implementation of {@link TypeProvider#lookupFieldNames} to return the
* set of all fields declared on the protobuf type.
*/
@CanIgnoreReturnValue
CelBuilder addProtoTypeMasks(ProtoTypeMask... typeMasks);
/**
* Add one or more {@link ProtoTypeMask} values. The {@code ProtoTypeMask} values will be used to
* compute a set of {@code Decl} values using a protobuf message's fields as the names and types
* of the variables if {@link ProtoTypeMask#fieldsAreVariableDeclarations} is {@code true}.
*
* <p>Note, this feature may not work with custom {@link TypeProvider} implementations out of the
* box, as it requires the implementation of {@link TypeProvider#lookupFieldNames} to return the
* set of all fields declared on the protobuf type.
*/
@CanIgnoreReturnValue
CelBuilder addProtoTypeMasks(Iterable<ProtoTypeMask> typeMasks);
/**
* Add one or more {@link CelFunctionBinding} objects to the CEL runtime.
*
* <p>Functions with duplicate overload ids will be replaced in favor of the new overload.
*/
@CanIgnoreReturnValue
CelBuilder addFunctionBindings(CelFunctionBinding... bindings);
/**
* Bind a collection of {@link CelFunctionBinding} objects to the runtime.
*
* <p>Functions with duplicate overload ids will be replaced in favor of the new overload.
*/
@CanIgnoreReturnValue
CelBuilder addFunctionBindings(Iterable<CelFunctionBinding> bindings);
/** Set the expected {@code resultType} for the type-checked expression. */
@CanIgnoreReturnValue
CelBuilder setResultType(CelType resultType);
/**
* Set the expected {@code resultType} in proto format described in checked.proto for the
* type-checked expression.
*/
@CanIgnoreReturnValue
CelBuilder setProtoResultType(Type resultType);
/**
* Set a custom type factory for the runtime.
*
* <p>Note: it is valid to combine type factory methods within the runtime. Only the options which
* have been configured will be used.
*
* <p>The type creation search order is as follows:
*
* <ul>
* <li/>Custom type factory ({@link #setTypeFactory})
* <li/>Custom descriptor set {{@link #addMessageTypes})
* </ul>
*/
@CanIgnoreReturnValue
CelBuilder setTypeFactory(Function<String, Message.Builder> typeFactory);
/**
* Sets the {@code celValueProvider} for resolving values during evaluation. The provided value
* provider will be used first before falling back to the built-in {@link
* dev.cel.common.values.ProtoMessageValueProvider} for resolving protobuf messages.
*
* <p>Note that {@link CelOptions#enableCelValue()} must be enabled or this method will be a
* no-op.
*/
@CanIgnoreReturnValue
CelBuilder setValueProvider(CelValueProvider celValueProvider);
/**
* Set the {@code typeProvider} for use with type-checking expressions.
*
* @deprecated Use {@link #setTypeProvider(CelTypeProvider)} instead.
*/
@CanIgnoreReturnValue
@Deprecated
CelBuilder setTypeProvider(TypeProvider typeProvider);
/** Set the {@code celTypeProvider} for use with type-checking expressions. */
@CanIgnoreReturnValue
CelBuilder setTypeProvider(CelTypeProvider celTypeProvider);
/**
* Add message {@link Descriptor}s to the use for type-checking and object creation at
* interpretation time.
*
* <p>This method may be safely combined with {@link #setTypeFactory}, {@link #setTypeProvider},
* and {@link #addFileTypes} calls.
*
* <p>If either a {@code typeFactory} or {@code typeProvider} are configured, these classes will
* take precedence over any dynamic resolution of data related to the descriptors.
*/
@CanIgnoreReturnValue
CelBuilder addMessageTypes(Descriptor... descriptors);
/**
* Add message {@link Descriptor}s to the use for type-checking and object creation at
* interpretation time.
*
* <p>This method may be safely combined with {@link #setTypeFactory}, {@link #setTypeProvider},
* and {@link #addFileTypes} calls.
*
* <p>If either a {@code typeFactory} or {@code typeProvider} are configured, these classes will
* take precedence over any dynamic resolution of data related to the descriptors.
*/
@CanIgnoreReturnValue
CelBuilder addMessageTypes(Iterable<Descriptor> descriptors);
/**
* Add {@link FileDescriptor}s to the use for type-checking, and for object creation at
* interpretation time.
*
* <p>This method may be safely combined with {@link #setTypeFactory}, {@link #setTypeProvider},
* and {@link #addMessageTypes} calls.
*
* <p>If either a {@code typeFactory} or {@code typeProvider} are configured, these classes will
* take precedence over any dynamic resolution of data related to the descriptors.
*/
@CanIgnoreReturnValue
CelBuilder addFileTypes(FileDescriptor... fileDescriptors);
/**
* Add {@link FileDescriptor}s to the use for type-checking, and for object creation at
* interpretation time.
*
* <p>This method may be safely combined with {@link #setTypeFactory}, {@link #setTypeProvider},
* and {@link #addMessageTypes} calls.
*
* <p>If either a {@code typeFactory} or {@code typeProvider} are configured, these classes will
* take precedence over any dynamic resolution of data related to the descriptors.
*/
@CanIgnoreReturnValue
CelBuilder addFileTypes(Iterable<FileDescriptor> fileDescriptors);
/**
* Add all of the {@link FileDescriptor}s in a {@code FileDescriptorSet} to the use for
* type-checking, and for object creation at interpretation time.
*
* <p>This method may be safely combined with {@link #setTypeFactory}, {@link #setTypeProvider},
* and {@link #addMessageTypes} calls.
*
* <p>If either a {@code typeFactory} or {@code typeProvider} are configured, these classes will
* take precedence over any dynamic resolution of data related to the descriptors.
*/
@CanIgnoreReturnValue
CelBuilder addFileTypes(FileDescriptorSet fileDescriptorSet);
/** Enable or disable the standard CEL library functions and variables */
@CanIgnoreReturnValue
CelBuilder setStandardEnvironmentEnabled(boolean value);
/** Adds one or more libraries for parsing and type-checking. */
@CanIgnoreReturnValue
CelBuilder addCompilerLibraries(CelCompilerLibrary... libraries);
/** Adds a collection of libraries for parsing and type-checking. */
@CanIgnoreReturnValue
CelBuilder addCompilerLibraries(Iterable<CelCompilerLibrary> libraries);
/** Adds one or more libraries for runtime. */
@CanIgnoreReturnValue
CelBuilder addRuntimeLibraries(CelRuntimeLibrary... libraries);
/** Adds a collection of libraries for runtime. */
@CanIgnoreReturnValue
CelBuilder addRuntimeLibraries(Iterable<CelRuntimeLibrary> libraries);
/**
* Sets a proto ExtensionRegistry to assist with unpacking Any messages containing a proto2
extension field.
*/
@CanIgnoreReturnValue
CelBuilder setExtensionRegistry(ExtensionRegistry extensionRegistry);
/** Construct a new {@code Cel} instance from the provided configuration. */
Cel build();
}