Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ java_library(
deps = [
"//common:operator",
"//common:options",
"//common/exceptions:overload_not_found",
"//common/values",
"//common/values:cel_byte_string",
"//common/values:cel_value",
Expand All @@ -377,6 +378,7 @@ cel_android_library(
deps = [
"//common:operator_android",
"//common:options",
"//common/exceptions:overload_not_found",
"//common/values:cel_byte_string",
"//common/values:cel_value_android",
"//common/values:values_android",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.protobuf.MessageLite;
import dev.cel.common.CelOptions;
import dev.cel.common.Operator;
import dev.cel.common.exceptions.CelOverloadNotFoundException;
import dev.cel.common.values.CelByteString;
import dev.cel.common.values.CelValue;
import dev.cel.common.values.NullValue;
Expand Down Expand Up @@ -223,36 +224,33 @@ public ImmutableSet<CelFunctionBinding> newFunctionBindings(RuntimeEquality runt
Operator.OPTIONAL_INDEX.getFunction(),
CelFunctionBinding.from(
"list_optindex_optional_int", List.class, Long.class, RuntimeHelpers::optIndexList),
CelFunctionBinding.from(
"optional_list_optindex_optional_int",
Optional.class,
Long.class,
CelOptionalRuntimeLibrary::indexOptionalList),
optionalIndexBinding(
"optional_list_optindex_optional_int", Operator.OPTIONAL_INDEX, runtimeEquality),
CelFunctionBinding.from(
"map_optindex_optional_value", Map.class, Object.class, runtimeEquality::findInMap),
CelFunctionBinding.from(
"optional_map_optindex_optional_value",
Optional.class,
Object.class,
(optionalMap, key) -> indexOptionalMap(optionalMap, key, runtimeEquality))));
optionalIndexBinding(
"optional_map_optindex_optional_value", Operator.OPTIONAL_INDEX, runtimeEquality)));

bindingBuilder.addAll(
CelFunctionBinding.fromOverloads(
Operator.INDEX.getFunction(),
CelFunctionBinding.from(
"optional_list_index_int",
Optional.class,
Long.class,
CelOptionalRuntimeLibrary::indexOptionalList),
CelFunctionBinding.from(
"optional_map_index_value",
Optional.class,
Object.class,
(optionalMap, key) -> indexOptionalMap(optionalMap, key, runtimeEquality))));
optionalIndexBinding("optional_list_index_int", Operator.INDEX, runtimeEquality),
optionalIndexBinding("optional_map_index_value", Operator.INDEX, runtimeEquality)));

return bindingBuilder.build();
}

private static CelFunctionBinding optionalIndexBinding(
String overloadId, Operator operator, RuntimeEquality runtimeEquality) {
String functionName = operator.getFunction();
return CelFunctionBinding.from(
overloadId,
Optional.class,
Object.class,
(optionalContainer, key) ->
indexOptional(functionName, optionalContainer, key, runtimeEquality));
}

private static ImmutableList<Object> elideOptionalCollection(Collection<?> list) {
ImmutableList.Builder<Object> builder = ImmutableList.builder();
for (Object element : list) {
Expand Down Expand Up @@ -298,23 +296,22 @@ private static boolean isZeroValue(Object val) {
return false;
}

private static Object indexOptionalMap(
Optional<?> optionalMap, Object key, RuntimeEquality runtimeEquality) {
if (!optionalMap.isPresent()) {
private static Object indexOptional(
String functionName,
Optional<?> optionalContainer,
Object key,
RuntimeEquality runtimeEquality) {
if (!optionalContainer.isPresent()) {
return Optional.empty();
}

Map<?, ?> map = (Map<?, ?>) optionalMap.get();

return runtimeEquality.findInMap(map, key);
}

private static Object indexOptionalList(Optional<?> optionalList, long index) {
if (!optionalList.isPresent()) {
return Optional.empty();
Object container = optionalContainer.get();
if (container instanceof Map) {
return runtimeEquality.findInMap((Map<?, ?>) container, key);
}
if (container instanceof List && key instanceof Long) {
return RuntimeHelpers.optIndexList((List<?>) container, (Long) key);
}
List<?> list = (List<?>) optionalList.get();
return RuntimeHelpers.optIndexList(list, index);
throw new CelOverloadNotFoundException(functionName);
}

private static Object listOptionalFirst(Collection<?> list) {
Expand Down
1 change: 1 addition & 0 deletions extensions/src/test/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ java_library(
"//common:cel_validation_result",
"//common:cel_var_decl",
"//common:container",
"//common:error_codes",
"//common:options",
"//common/ast",
"//common/exceptions:attribute_not_found",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,19 @@ public void getAllFunctionNames() {

@Test
public void crossExtensionSharedFunctionName_reverse_dispatchesAcrossListsAndStrings(
@TestParameter boolean isParseOnly) throws Exception {
@TestParameter({
"dyn('hello').reverse() == 'olleh' && dyn([1, 2]).reverse() == [2, 1]",
"(([['a']] + dyn(['hello']))[1]).reverse() == dyn('olleh')",
"(('a' == '' ? 'a' : (['a'] + dyn([[1, 2]]))[1])).reverse() == dyn([2, 1])",
})
String expression,
@TestParameter boolean isParseOnly)
throws Exception {
Cel cel =
CelFactory.plannerCelBuilder()
.addCompilerLibraries(CelExtensions.lists(), CelExtensions.strings())
.addRuntimeLibraries(CelExtensions.lists(), CelExtensions.strings())
.build();
String expression = "dyn('hello').reverse() == 'olleh' && dyn([1, 2]).reverse() == [2, 1]";
CelAbstractSyntaxTree ast =
isParseOnly ? cel.parse(expression).getAst() : cel.compile(expression).getAst();

Expand All @@ -239,7 +245,7 @@ public void crossExtensionSharedFunctionName_liteRuntime_reverseDispatchesAcross
.setStandardFunctions(CelStandardFunctions.ALL_STANDARD_FUNCTIONS)
.addLibraries(CelListsRuntimeLibrary.lists(), CelStringRuntimeLibrary.strings())
.build();
String expression = "dyn('hello').reverse() == 'olleh' && dyn([1, 2]).reverse() == [2, 1]";
String expression = "(([['a']] + dyn(['hello']))[1]).reverse() == dyn('olleh')";
CelAbstractSyntaxTree ast =
isParseOnly ? compiler.parse(expression).getAst() : compiler.compile(expression).getAst();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import dev.cel.bundle.CelFactory;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelContainer;
import dev.cel.common.CelErrorCode;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOptions;
import dev.cel.common.CelOverloadDecl;
Expand Down Expand Up @@ -2304,6 +2305,53 @@ public void optionalMacros_invalidExpanderArguments_throws() throws Exception {
.isTrue();
}

@Test
public void optionalIndex_narrowedAndIntegerKeyedOptionalContainers_evaluatesToTrue(
@TestParameter({
"dyn(dyn(optional.of(optional.of([2]))).orValue([1])[0]) == optional.of(2)",
"dyn(optional.of([2])).orValue({0: 1})[0] == 2",
"dyn(dyn(optional.of([2])).orValue(optional.of([1]))[0]) == 2",
"dyn(optional.of([2])).or(optional.of({0: 1}))[0] == optional.of(2)",
"dyn(dyn(optional.of(optional.of([2]))).orValue([1])[?0]) == optional.of(2)",
"optional.of({0: 2})[0] == optional.of(2)",
"optional.of({0: 2})[?0] == optional.of(2)",
"dyn(optional.of({0: 2}))[0] == optional.of(2)",
"dyn(optional.of({0: 2}))[?0] == optional.of(2)",
"dyn(dyn(optional.of(optional.of({0: 2}))).orValue([1])[0]) == optional.of(2)",
"TestAllTypes{}.?repeated_int64[dyn('a')] == optional.none()",
"TestAllTypes{}.?repeated_int64[?dyn('a')] == optional.none()",
"int(false ? timestamp(0) : (true ? dyn('42') : null)) == 42",
})
String source)
throws Exception {
TestCel cel = newCelBuilder().build();
Program program = cel.createProgram(compile(cel, source));

Object result = program.eval();

assertThat(result).isEqualTo(true);
}

@Test
public void optionalIndex_invalidRuntimeContainerOrKey_throwsOverloadNotFound(
@TestParameter({
"dyn(optional.of(42)).or(optional.of([1]))[0]",
"dyn(optional.of(42)).or(optional.of([1]))[?0]",
"dyn(optional.of([1])).or(optional.of({'a': 1}))['a']",
"dyn(optional.of([1])).or(optional.of({'a': 1}))[?'a']",
"(false ? optional.of([1]) : null)[0]",
"(false ? [1] : dyn(null))[?0]",
})
String source)
throws Exception {
TestCel cel = newCelBuilder().build();
Program program = cel.createProgram(compile(cel, source));

CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval);

assertThat(e.getErrorCode()).isEqualTo(CelErrorCode.OVERLOAD_NOT_FOUND);
}

/**
* A wrapper around {@link CelBuilder} (which uses {@link CelRuntimeBuilder}) and {@link
* CelLiteRuntimeBuilder} to allow for testing of both runtimes.
Expand Down
6 changes: 4 additions & 2 deletions runtime/src/main/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ java_library(
":function_resolver",
":resolved_overload",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

Expand All @@ -525,7 +524,6 @@ cel_android_library(
":function_resolver_android",
":resolved_overload_android",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
],
)

Expand Down Expand Up @@ -753,6 +751,7 @@ java_library(
"//common/exceptions:overload_not_found",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:org_jspecify_jspecify",
],
)

Expand All @@ -767,6 +766,7 @@ cel_android_library(
"//common/annotations",
"//common/exceptions:overload_not_found",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:org_jspecify_jspecify",
"@maven_android//:com_google_guava_guava",
],
)
Expand Down Expand Up @@ -1254,6 +1254,7 @@ java_library(
"//common/exceptions:overload_not_found",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:org_jspecify_jspecify",
],
)

Expand All @@ -1270,6 +1271,7 @@ cel_android_library(
"//common/annotations",
"//common/exceptions:overload_not_found",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:org_jspecify_jspecify",
"@maven_android//:com_google_guava_guava",
],
)
Expand Down
47 changes: 19 additions & 28 deletions runtime/src/main/java/dev/cel/runtime/CelLateFunctionBindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

package dev.cel.runtime;

import static com.google.common.collect.ImmutableMap.toImmutableMap;

import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.Immutable;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -29,49 +26,43 @@
@Immutable
public final class CelLateFunctionBindings implements CelFunctionResolver {

private final ImmutableMap<String, CelResolvedOverload> functions;

private CelLateFunctionBindings(ImmutableMap<String, CelResolvedOverload> functions) {
this.functions = functions;
}
private final DefaultDispatcher dispatcher;

@Override
public Optional<CelResolvedOverload> findOverloadMatchingArgs(
String functionName, Collection<String> overloadIds, Object[] args)
throws CelEvaluationException {
return DefaultDispatcher.findOverloadMatchingArgs(functionName, overloadIds, functions, args);
return dispatcher.findOverloadMatchingArgs(functionName, overloadIds, args);
}

@Override
public Optional<CelResolvedOverload> findOverloadMatchingArgs(String functionName, Object[] args)
throws CelEvaluationException {
return DefaultDispatcher.findOverloadMatchingArgs(
functionName, functions.keySet(), functions, args);
return dispatcher.findOverloadMatchingArgs(functionName, args);
}

public static CelLateFunctionBindings from(CelFunctionBinding... functions) {
return from(Arrays.asList(functions));
}

public static CelLateFunctionBindings from(Collection<CelFunctionBinding> functions) {
return new CelLateFunctionBindings(
functions.stream()
.collect(
toImmutableMap(
CelFunctionBinding::getOverloadId,
CelLateFunctionBindings::createResolvedOverload)));
DefaultDispatcher.Builder builder = DefaultDispatcher.newBuilder();
for (CelFunctionBinding binding : functions) {
String functionName =
binding instanceof InternalCelFunctionBinding
? ((InternalCelFunctionBinding) binding).getFunctionName()
: binding.getOverloadId();
builder.addOverload(
functionName,
binding.getOverloadId(),
binding.getArgTypes(),
binding.isStrict(),
binding.getDefinition());
}
return new CelLateFunctionBindings(builder.build());
}

private static CelResolvedOverload createResolvedOverload(CelFunctionBinding binding) {
String functionName = binding.getOverloadId();
if (binding instanceof InternalCelFunctionBinding) {
functionName = ((InternalCelFunctionBinding) binding).getFunctionName();
}
return CelResolvedOverload.of(
functionName,
binding.getOverloadId(),
binding.getDefinition(),
binding.isStrict(),
binding.getArgTypes());
private CelLateFunctionBindings(DefaultDispatcher dispatcher) {
this.dispatcher = dispatcher;
}
}
Loading
Loading