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
12 changes: 12 additions & 0 deletions 12 runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,15 @@ java_library(
"//runtime/src/main/java/dev/cel/runtime:metadata",
],
)

java_library(
name = "lite_runtime_impl",
visibility = ["//:internal"],
exports = ["//runtime/src/main/java/dev/cel/runtime:lite_runtime_impl"],
)

cel_android_library(
name = "lite_runtime_impl_android",
visibility = ["//:internal"],
exports = ["//runtime/src/main/java/dev/cel/runtime:lite_runtime_impl_android"],
)
86 changes: 44 additions & 42 deletions 86 runtime/src/main/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,8 @@ java_library(
java_library(
name = "lite_runtime_impl",
srcs = LITE_RUNTIME_IMPL_SOURCES,
visibility = ["//visibility:private"],
tags = [
],
deps = [
":activation",
":dispatcher",
Expand All @@ -725,6 +726,35 @@ java_library(
],
)

cel_android_library(
name = "lite_runtime_impl_android",
srcs = LITE_RUNTIME_IMPL_SOURCES,
tags = [
],
deps = [
":activation_android",
":dispatcher_android",
":evaluation_exception",
":function_binding_android",
":interpretable_android",
":interpreter_android",
":lite_runtime_android",
":runtime_equality_android",
":runtime_helpers_android",
":runtime_type_provider_android",
":standard_functions_android",
":type_resolver_android",
"//:auto_value",
"//common:cel_ast_android",
"//common:options",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven_android//:com_google_guava_guava",
],
)

java_library(
name = "lite_runtime_factory",
srcs = [
Expand All @@ -738,6 +768,19 @@ java_library(
],
)

cel_android_library(
name = "lite_runtime_factory_android",
srcs = [
"CelLiteRuntimeFactory.java",
],
tags = [
],
deps = [
":lite_runtime_android",
":lite_runtime_impl_android",
],
)

# keep sorted
UNKNOWN_ATTRIBUTE_SOURCES = [
"CelAttribute.java",
Expand Down Expand Up @@ -882,44 +925,3 @@ cel_android_library(
"@maven_android//:com_google_guava_guava",
],
)

cel_android_library(
name = "lite_runtime_impl_android",
srcs = LITE_RUNTIME_IMPL_SOURCES,
visibility = ["//visibility:private"],
deps = [
":activation_android",
":dispatcher_android",
":evaluation_exception",
":function_binding_android",
":interpretable_android",
":interpreter_android",
":lite_runtime_android",
":runtime_equality_android",
":runtime_helpers_android",
":runtime_type_provider_android",
":standard_functions_android",
":type_resolver_android",
"//:auto_value",
"//common:cel_ast_android",
"//common:options",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven_android//:com_google_guava_guava",
],
)

cel_android_library(
name = "lite_runtime_factory_android",
srcs = [
"CelLiteRuntimeFactory.java",
],
tags = [
],
deps = [
":lite_runtime_android",
":lite_runtime_impl_android",
],
)
2 changes: 2 additions & 0 deletions 2 runtime/src/main/java/dev/cel/runtime/CelLiteRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public interface CelLiteRuntime {

Program createProgram(CelAbstractSyntaxTree ast) throws CelEvaluationException;

CelLiteRuntimeBuilder toRuntimeBuilder();

/** Creates an evaluable {@code Program} instance which is thread-safe and immutable. */
@Immutable
interface Program {
Expand Down
31 changes: 25 additions & 6 deletions 31 runtime/src/main/java/dev/cel/runtime/LiteRuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.common.base.Preconditions.checkState;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import javax.annotation.concurrent.ThreadSafe;
Expand All @@ -30,19 +31,29 @@
@ThreadSafe
final class LiteRuntimeImpl implements CelLiteRuntime {
private final Interpreter interpreter;
private final CelOptions celOptions;
private final ImmutableList<CelFunctionBinding> customFunctionBindings;
private final CelStandardFunctions celStandardFunctions;

@Override
public Program createProgram(CelAbstractSyntaxTree ast) {
checkState(ast.isChecked(), "programs must be created from checked expressions");
return LiteProgramImpl.plan(interpreter.createInterpretable(ast));
}

static final class Builder implements CelLiteRuntimeBuilder {

private final HashMap<String, CelFunctionBinding> customFunctionBindings;
@Override
public CelLiteRuntimeBuilder toRuntimeBuilder() {
return new Builder()
.setOptions(celOptions)
.setStandardFunctions(celStandardFunctions)
.addFunctionBindings(customFunctionBindings);
}

private CelOptions celOptions;
static final class Builder implements CelLiteRuntimeBuilder {

// Following is visible to test `toBuilder`.
@VisibleForTesting CelOptions celOptions;
@VisibleForTesting final HashMap<String, CelFunctionBinding> customFunctionBindings;
@VisibleForTesting CelStandardFunctions celStandardFunctions;

@Override
Expand Down Expand Up @@ -158,7 +169,8 @@ public Object adapt(Object message) {
dispatcher,
celOptions);

return new LiteRuntimeImpl(interpreter);
return new LiteRuntimeImpl(
interpreter, celOptions, customFunctionBindings.values(), celStandardFunctions);
}

private Builder() {
Expand All @@ -171,7 +183,14 @@ static CelLiteRuntimeBuilder newBuilder() {
return new Builder();
}

private LiteRuntimeImpl(Interpreter interpreter) {
private LiteRuntimeImpl(
Interpreter interpreter,
CelOptions celOptions,
Iterable<CelFunctionBinding> customFunctionBindings,
CelStandardFunctions celStandardFunctions) {
this.interpreter = interpreter;
this.celOptions = celOptions;
this.customFunctionBindings = ImmutableList.copyOf(customFunctionBindings);
this.celStandardFunctions = celStandardFunctions;
}
}
2 changes: 2 additions & 0 deletions 2 runtime/src/test/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ java_library(
"//runtime:interpreter_util",
"//runtime:lite_runtime",
"//runtime:lite_runtime_factory",
"//runtime:lite_runtime_impl",
"//runtime:proto_message_activation_factory",
"//runtime:proto_message_runtime_equality",
"//runtime:proto_message_runtime_helpers",
Expand Down Expand Up @@ -168,6 +169,7 @@ cel_android_local_test(
"//runtime:function_binding_android",
"//runtime:lite_runtime_android",
"//runtime:lite_runtime_factory_android",
"//runtime:lite_runtime_impl_android",
"//runtime:standard_functions_android",
"@cel_spec//proto/cel/expr:checked_java_proto_lite",
"@maven//:com_google_protobuf_protobuf_java",
Expand Down
31 changes: 31 additions & 0 deletions 31 runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,37 @@ public void programConstruction() throws Exception {
assertThat(program).isNotNull();
}

@Test
public void toRuntimeBuilder_isNewInstance() {
CelLiteRuntimeBuilder runtimeBuilder = CelLiteRuntimeFactory.newLiteRuntimeBuilder();
CelLiteRuntime runtime = runtimeBuilder.build();

CelLiteRuntimeBuilder newRuntimeBuilder = runtime.toRuntimeBuilder();

assertThat(newRuntimeBuilder).isNotEqualTo(runtimeBuilder);
}

@Test
public void toRuntimeBuilder_propertiesCopied() {
CelOptions celOptions = CelOptions.current().enableCelValue(true).build();
CelStandardFunctions celStandardFunctions = CelStandardFunctions.newBuilder().build();
CelLiteRuntimeBuilder runtimeBuilder =
CelLiteRuntimeFactory.newLiteRuntimeBuilder()
.setOptions(celOptions)
.setStandardFunctions(celStandardFunctions)
.addFunctionBindings(
CelFunctionBinding.from("string_isEmpty", String.class, String::isEmpty));
CelLiteRuntime runtime = runtimeBuilder.build();

LiteRuntimeImpl.Builder newRuntimeBuilder =
(LiteRuntimeImpl.Builder) runtime.toRuntimeBuilder();

assertThat(newRuntimeBuilder.celOptions).isEqualTo(celOptions);
assertThat(newRuntimeBuilder.celStandardFunctions).isEqualTo(celStandardFunctions);
assertThat(newRuntimeBuilder.customFunctionBindings).hasSize(1);
assertThat(newRuntimeBuilder.customFunctionBindings).containsKey("string_isEmpty");
}

@Test
public void setCelOptions_unallowedOptionsSet_throws(@TestParameter CelOptionsTestCase testCase) {
assertThrows(
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.