Repository navigation
Expand file tree
/
Copy pathCelFunction.java
More file actions
242 lines (224 loc) · 9.26 KB
/
Copy pathCelFunction.java
File metadata and controls
242 lines (224 loc) · 9.26 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
// Copyright 2026 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 static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.auto.value.AutoValue;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOverloadDecl;
import dev.cel.common.types.CelType;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.runtime.CelFunctionOverload;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
/**
* Bundles a {@link CelFunctionDecl} and its corresponding runtime {@link CelFunctionBinding}s into
* a single unit that can be registered on {@link CelBuilder#addFunctions}.
*
* <p>Overload IDs are automatically generated from the function name and parameter types.
*/
@AutoValue
@Immutable
public abstract class CelFunction {
CelFunction() {}
/** The compile-time declaration for this function. */
public abstract CelFunctionDecl functionDecl();
/** The runtime bindings for this function's overloads. */
public abstract ImmutableSet<CelFunctionBinding> functionBindings();
/**
* Creates a new builder to construct a {@link CelFunction} with the given {@code functionName}.
*/
public static Builder newBuilder(String functionName) {
return new Builder(functionName);
}
/** Builder for configuring a {@link CelFunction}. */
public static final class Builder {
private final String functionName;
private final List<CelOverloadDecl> overloadDecls = new ArrayList<>();
// List of functions that take an overload ID and create a CelFunctionBinding. Since the
// overload ID is not always known until build time, this list is applied to the overload IDs
// in `build()`
private final List<Function<String, CelFunctionBinding>> bindings = new ArrayList<>();
private Builder(String functionName) {
checkArgument(!Strings.isNullOrEmpty(functionName), "Function name cannot be null or empty");
this.functionName = functionName;
}
/**
* Adds a unary member overload (e.g. {@code x.f()}) with an automatically generated overload ID
* (e.g. {@code "targetType.f()"}).
*/
@CanIgnoreReturnValue
public <T> Builder addMemberOverload(
CelType resultType,
CelType targetType,
Class<T> targetClass,
CelFunctionOverload.Unary<T> impl) {
checkNotNull(resultType);
checkNotNull(targetType);
checkNotNull(targetClass);
checkNotNull(impl);
overloadDecls.add(CelOverloadDecl.newMemberOverload(resultType, targetType));
bindings.add(overloadId -> CelFunctionBinding.from(overloadId, targetClass, impl));
return this;
}
/**
* Adds a binary member overload (e.g. {@code x.f(y)}) with an automatically generated overload
* ID (e.g. {@code "targetType.f(argType)"}).
*/
@CanIgnoreReturnValue
public <T1, T2> Builder addMemberOverload(
CelType resultType,
CelType targetType,
CelType argType,
Class<T1> targetClass,
Class<T2> argClass,
CelFunctionOverload.Binary<T1, T2> impl) {
checkNotNull(resultType);
checkNotNull(targetType);
checkNotNull(argType);
checkNotNull(targetClass);
checkNotNull(argClass);
checkNotNull(impl);
overloadDecls.add(CelOverloadDecl.newMemberOverload(resultType, targetType, argType));
bindings.add(overloadId -> CelFunctionBinding.from(overloadId, targetClass, argClass, impl));
return this;
}
/**
* Adds an N-ary member overload (e.g. {@code x.f(y, z, ...)}) with an automatically generated
* overload ID (e.g. {@code "targetType.f(arg1Type,arg2Type,...)"}), where the first element of
* {@code argTypes} and {@code argClasses} is the receiver {@code x}.
*/
@CanIgnoreReturnValue
public Builder addMemberOverload(
CelType resultType,
List<CelType> argTypes,
List<Class<?>> argClasses,
CelFunctionOverload impl) {
return addNaryOverload(
resultType, argTypes, argClasses, impl, /* isInstanceFunction= */ true);
}
/**
* Adds a unary global overload (e.g. {@code f(x)}) with an automatically generated overload ID
* (e.g. {@code "f(argType)"}).
*/
@CanIgnoreReturnValue
public <T> Builder addGlobalOverload(
CelType resultType, CelType argType, Class<T> argClass, CelFunctionOverload.Unary<T> impl) {
checkNotNull(resultType);
checkNotNull(argType);
checkNotNull(argClass);
checkNotNull(impl);
overloadDecls.add(CelOverloadDecl.newGlobalOverload(resultType, argType));
bindings.add(overloadId -> CelFunctionBinding.from(overloadId, argClass, impl));
return this;
}
/**
* Adds a binary global overload (e.g. {@code f(x, y)}) with an automatically generated overload
* ID (e.g. {@code "f(arg1Type,arg2Type)"}).
*/
@CanIgnoreReturnValue
public <T1, T2> Builder addGlobalOverload(
CelType resultType,
CelType arg1Type,
CelType arg2Type,
Class<T1> arg1Class,
Class<T2> arg2Class,
CelFunctionOverload.Binary<T1, T2> impl) {
checkNotNull(resultType);
checkNotNull(arg1Type);
checkNotNull(arg2Type);
checkNotNull(arg1Class);
checkNotNull(arg2Class);
checkNotNull(impl);
overloadDecls.add(CelOverloadDecl.newGlobalOverload(resultType, arg1Type, arg2Type));
bindings.add(overloadId -> CelFunctionBinding.from(overloadId, arg1Class, arg2Class, impl));
return this;
}
/**
* Adds an N-ary global overload (including zero-argument {@code f()}) with an automatically
* generated overload ID (e.g. {@code "f(arg1Type,arg2Type,...)"}).
*/
@CanIgnoreReturnValue
public Builder addGlobalOverload(
CelType resultType,
List<CelType> argTypes,
List<Class<?>> argClasses,
CelFunctionOverload impl) {
return addNaryOverload(
resultType, argTypes, argClasses, impl, /* isInstanceFunction= */ false);
}
@CanIgnoreReturnValue
private Builder addNaryOverload(
CelType resultType,
List<CelType> argTypes,
List<Class<?>> argClasses,
CelFunctionOverload impl,
boolean isInstanceFunction) {
checkNotNull(resultType);
checkNotNull(argTypes);
checkNotNull(argClasses);
checkNotNull(impl);
checkArgument(!isInstanceFunction || !argTypes.isEmpty(), "Member function with no receiver");
checkArgument(
argTypes.size() == argClasses.size(),
"Number of CelTypes (%s) must match number of Java classes (%s)",
argTypes.size(),
argClasses.size());
overloadDecls.add(
isInstanceFunction
? CelOverloadDecl.newMemberOverload(resultType, ImmutableList.copyOf(argTypes))
: CelOverloadDecl.newGlobalOverload(resultType, ImmutableList.copyOf(argTypes)));
bindings.add(overloadId -> CelFunctionBinding.from(overloadId, argClasses, impl));
return this;
}
/**
* Builds a new {@link CelFunction} instance.
*
* @throws IllegalArgumentException if no overloads were added or if multiple overloads have the
* same canonical signature
*/
@CheckReturnValue
public CelFunction build() {
checkArgument(
!overloadDecls.isEmpty(), "Function '%s' must have at least one overload", functionName);
CelFunctionDecl functionDecl =
CelFunctionDecl.newFunctionDeclaration(functionName, overloadDecls);
ImmutableList<CelOverloadDecl> populatedOverloads = functionDecl.overloads().asList();
Set<CelOverloadDecl> seen = new HashSet<>();
ImmutableList.Builder<CelFunctionBinding> bindingsBuilder =
ImmutableList.builderWithExpectedSize(populatedOverloads.size());
for (int i = 0; i < overloadDecls.size(); i++) {
CelOverloadDecl decl = overloadDecls.get(i);
if (!seen.add(decl)) {
int firstIndex = overloadDecls.indexOf(decl);
throw new IllegalArgumentException(
"Duplicate overload signature: " + populatedOverloads.get(firstIndex).overloadId());
}
bindingsBuilder.add(this.bindings.get(i).apply(populatedOverloads.get(i).overloadId()));
}
ImmutableSet<CelFunctionBinding> bindings =
CelFunctionBinding.fromOverloads(functionName, bindingsBuilder.build());
return new AutoValue_CelFunction(functionDecl, bindings);
}
}
}