Skip to content

Commit 2203ac8

Browse files
l46kokcopybara-github
authored andcommitted
Support parsed-only evaluation for lists extensions, remove check for heterogeneous numeric comparisons for sorting
PiperOrigin-RevId: 899880149
1 parent 3075687 commit 2203ac8

File tree

2 files changed

+99
-113
lines changed

2 files changed

+99
-113
lines changed

extensions/src/main/java/dev/cel/extensions/CelListsExtensions.java

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,20 @@ public enum Function {
128128
"list_sort",
129129
"Sorts a list with comparable elements.",
130130
ListType.create(TypeParamType.create("T")),
131-
ListType.create(TypeParamType.create("T"))))),
131+
ListType.create(TypeParamType.create("T")))),
132+
CelFunctionBinding.from("list_sort", Collection.class, CelListsExtensions::sort)),
132133
SORT_BY(
133134
CelFunctionDecl.newFunctionDeclaration(
134135
"lists.@sortByAssociatedKeys",
135136
CelOverloadDecl.newGlobalOverload(
136137
"list_sortByAssociatedKeys",
137138
"Sorts a list by a key value. Used by the 'sortBy' macro",
138139
ListType.create(TypeParamType.create("T")),
139-
ListType.create(TypeParamType.create("T")))));
140+
ListType.create(TypeParamType.create("T")))),
141+
CelFunctionBinding.from(
142+
"list_sortByAssociatedKeys",
143+
Collection.class,
144+
CelListsExtensions::sortByAssociatedKeys));
140145

141146
private final CelFunctionDecl functionDecl;
142147
private final ImmutableSet<CelFunctionBinding> functionBindings;
@@ -147,7 +152,10 @@ String getFunction() {
147152

148153
Function(CelFunctionDecl functionDecl, CelFunctionBinding... functionBindings) {
149154
this.functionDecl = functionDecl;
150-
this.functionBindings = ImmutableSet.copyOf(functionBindings);
155+
this.functionBindings =
156+
functionBindings.length > 0
157+
? CelFunctionBinding.fromOverloads(functionDecl.name(), functionBindings)
158+
: ImmutableSet.of();
151159
}
152160
}
153161

@@ -240,32 +248,13 @@ public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
240248
@Override
241249
public void setRuntimeOptions(
242250
CelRuntimeBuilder runtimeBuilder, RuntimeEquality runtimeEquality, CelOptions celOptions) {
243-
for (Function function : functions) {
244-
runtimeBuilder.addFunctionBindings(function.functionBindings);
245-
for (CelOverloadDecl overload : function.functionDecl.overloads()) {
246-
switch (overload.overloadId()) {
247-
case "list_distinct":
248-
runtimeBuilder.addFunctionBindings(
249-
CelFunctionBinding.from(
250-
"list_distinct", Collection.class, (list) -> distinct(list, runtimeEquality)));
251-
break;
252-
case "list_sort":
253-
runtimeBuilder.addFunctionBindings(
254-
CelFunctionBinding.from(
255-
"list_sort", Collection.class, (list) -> sort(list, celOptions)));
256-
break;
257-
case "list_sortByAssociatedKeys":
258-
runtimeBuilder.addFunctionBindings(
259-
CelFunctionBinding.from(
260-
"list_sortByAssociatedKeys",
261-
Collection.class,
262-
(list) -> sortByAssociatedKeys(list, celOptions)));
263-
break;
264-
default:
265-
// Nothing to add
266-
}
267-
}
268-
}
251+
functions.forEach(function -> runtimeBuilder.addFunctionBindings(function.functionBindings));
252+
253+
runtimeBuilder.addFunctionBindings(
254+
CelFunctionBinding.fromOverloads(
255+
"distinct",
256+
CelFunctionBinding.from(
257+
"list_distinct", Collection.class, (list) -> distinct(list, runtimeEquality))));
269258
}
270259

271260
private static ImmutableList<Object> slice(Collection<Object> list, long from, long to) {
@@ -369,22 +358,18 @@ private static List<Object> reverse(Collection<Object> list) {
369358
}
370359
}
371360

372-
private static ImmutableList<Object> sort(Collection<Object> objects, CelOptions options) {
373-
return ImmutableList.sortedCopyOf(
374-
new CelObjectComparator(options.enableHeterogeneousNumericComparisons()), objects);
361+
private static ImmutableList<Object> sort(Collection<Object> objects) {
362+
return ImmutableList.sortedCopyOf(new CelObjectComparator(), objects);
375363
}
376364

377365
private static class CelObjectComparator implements Comparator<Object> {
378-
private final boolean enableHeterogeneousNumericComparisons;
379366

380-
CelObjectComparator(boolean enableHeterogeneousNumericComparisons) {
381-
this.enableHeterogeneousNumericComparisons = enableHeterogeneousNumericComparisons;
382-
}
367+
CelObjectComparator() {}
383368

384369
@SuppressWarnings({"unchecked"})
385370
@Override
386371
public int compare(Object o1, Object o2) {
387-
if (o1 instanceof Number && o2 instanceof Number && enableHeterogeneousNumericComparisons) {
372+
if (o1 instanceof Number && o2 instanceof Number) {
388373
return ComparisonFunctions.numericCompare((Number) o1, (Number) o2);
389374
}
390375

@@ -444,12 +429,9 @@ private static Optional<CelExpr> sortByMacro(
444429

445430
@SuppressWarnings({"unchecked", "rawtypes"})
446431
private static ImmutableList<Object> sortByAssociatedKeys(
447-
Collection<List<Object>> keyValuePairs, CelOptions options) {
432+
Collection<List<Object>> keyValuePairs) {
448433
List<Object>[] array = keyValuePairs.toArray(new List[0]);
449-
Arrays.sort(
450-
array,
451-
new CelObjectByKeyComparator(
452-
new CelObjectComparator(options.enableHeterogeneousNumericComparisons())));
434+
Arrays.sort(array, new CelObjectByKeyComparator(new CelObjectComparator()));
453435
ImmutableList.Builder<Object> builder = ImmutableList.builderWithExpectedSize(array.length);
454436
for (List<Object> pair : array) {
455437
builder.add(pair.get(1));

0 commit comments

Comments
 (0)