Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.common.collect.ImmutableList;
import com.google.common.primitives.UnsignedLong;
Expand Down Expand Up @@ -58,6 +59,7 @@ public enum Function {
HAS_VALUE("hasValue"),
OPTIONAL_NONE("optional.none"),
OPTIONAL_OF("optional.of"),
OPTIONAL_UNWRAP("optional.unwrap"),
OPTIONAL_OF_NON_ZERO_VALUE("optional.ofNonZeroValue"),
OR("or"),
OR_VALUE("orValue");
Expand Down Expand Up @@ -117,6 +119,10 @@ public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
CelFunctionDecl.newFunctionDeclaration(
Function.HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload("optional_hasValue", SimpleType.BOOL, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list", listTypeV, ListType.create(optionalTypeV))),
// Note: Implementation of "or" and "orValue" are special-cased inside the interpreter.
// Hence, their bindings are not provided here.
CelFunctionDecl.newFunctionDeclaration(
Expand Down Expand Up @@ -165,6 +171,7 @@ public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
}

@Override
@SuppressWarnings("unchecked")
public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
runtimeBuilder.addFunctionBindings(
CelRuntime.CelFunctionBinding.from("optional_of", Object.class, Optional::of),
Expand All @@ -177,6 +184,8 @@ public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
}
return Optional.of(val);
}),
CelRuntime.CelFunctionBinding.from(
"optional_unwrap_list", Collection.class, CelOptionalLibrary::elideOptionalCollection),
CelRuntime.CelFunctionBinding.from(
"optional_none", ImmutableList.of(), val -> Optional.empty()),
CelRuntime.CelFunctionBinding.from(
Expand All @@ -185,6 +194,10 @@ public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
"optional_hasValue", Object.class, val -> ((Optional<?>) val).isPresent()));
}

private static ImmutableList<Object> elideOptionalCollection(Collection<Optional<Object>> list) {
return list.stream().filter(Optional::isPresent).map(Optional::get).collect(toImmutableList());
}

// TODO: This will need to be adapted to handle an intermediate CelValue instead,
// akin to Zeroer interface in Go. Currently, it is unable to handle zero-values for a
// user-defined custom type.
Expand Down
12 changes: 12 additions & 0 deletions 12 runtime/src/test/resources/optional.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Source: optional.unwrap([])
=====>
bindings: {}
result: []

Source: optional.unwrap([optional.none(), optional.of(1), optional.of(str)])
declare str {
value string
}
=====>
bindings: {str=foo}
result: [1, foo]
5 changes: 5 additions & 0 deletions 5 runtime/src/test/resources/optional_errors.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Source: optional.unwrap([dyn(1)])
=====>
bindings: {}
error: evaluation error: Function 'optional_unwrap_list' failed with arg(s) '[1]'
error_code: INTERNAL_ERROR
2 changes: 2 additions & 0 deletions 2 testing/src/main/java/dev/cel/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ java_library(
"//common/internal:file_descriptor_converter",
"//common/resources/testdata/proto3:standalone_global_enum_java_proto",
"//common/types:cel_types",
"//common/types:type_providers",
"//extensions:optional_library",
"//runtime",
"//runtime:runtime_helper",
"@@protobuf~//java/core",
Expand Down
27 changes: 27 additions & 0 deletions 27 testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
import dev.cel.common.CelProtoAbstractSyntaxTree;
import dev.cel.common.internal.DefaultDescriptorPool;
import dev.cel.common.internal.FileDescriptorSetConverter;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.CelTypes;
import dev.cel.extensions.CelOptionalLibrary;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntime.CelFunctionBinding;
Expand Down Expand Up @@ -127,11 +129,19 @@ public BaseInterpreterTest(CelOptions celOptions, boolean useNativeCelType) {
this.celOptions = celOptions;
this.celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
.addLibraries(CelOptionalLibrary.INSTANCE)
.addFileTypes(TEST_FILE_DESCRIPTORS)
.setOptions(celOptions)
.build();
}

@Override
protected void prepareCompiler(CelTypeProvider typeProvider) {
super.prepareCompiler(typeProvider);
this.celCompiler =
celCompiler.toCompilerBuilder().addLibraries(CelOptionalLibrary.INSTANCE).build();
}

private CelAbstractSyntaxTree compileTestCase() {
CelAbstractSyntaxTree ast = prepareTest(TEST_FILE_DESCRIPTORS);
if (ast == null) {
Expand Down Expand Up @@ -494,6 +504,23 @@ public void messages_error() {
runTest();
}

@Test
public void optional() {
// TODO: Move existing optional tests here to also test CelValue runtime
source = "optional.unwrap([])";
runTest();

declareVariable("str", CelTypes.STRING);
source = "optional.unwrap([optional.none(), optional.of(1), optional.of(str)])";
runTest(ImmutableMap.of("str", "foo"));
}

@Test
public void optional_errors() {
source = "optional.unwrap([dyn(1)])";
runTest();
}

@Test
public void has() throws Exception {
TestAllTypes nestedMessage =
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.