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
12 changes: 11 additions & 1 deletion bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package(

java_library(
name = "cel",
exports = ["//bundle/src/main/java/dev/cel/bundle:cel"],
exports = [
"//bundle/src/main/java/dev/cel/bundle:cel",
"//bundle/src/main/java/dev/cel/bundle:cel_factory",
],
)

java_library(
Expand All @@ -29,3 +32,10 @@ java_library(
name = "environment_exporter",
exports = ["//bundle/src/main/java/dev/cel/bundle:environment_exporter"],
)

java_library(
name = "cel_impl",
testonly = 1,
visibility = ["//:internal"],
exports = ["//bundle/src/main/java/dev/cel/bundle:cel_impl"],
)
54 changes: 48 additions & 6 deletions bundle/src/main/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ package(
CEL_SOURCES = [
"Cel.java",
"CelBuilder.java",
"CelFactory.java",
"CelImpl.java",
]

java_library(
Expand All @@ -21,31 +19,74 @@ java_library(
tags = [
],
deps = [
"//checker:checker_legacy_environment",
"//checker:proto_type_mask",
"//checker:standard_decl",
"//common:compiler_common",
"//common:container",
"//common:options",
"//common/types:type_providers",
"//common/values:cel_value_provider",
"//compiler:compiler_builder",
"//parser:macro",
"//runtime",
"//runtime:function_binding",
"//runtime:standard_functions",
"@cel_spec//proto/cel/expr:checked_java_proto",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
],
)

java_library(
name = "cel_factory",
srcs = ["CelFactory.java"],
tags = [
],
deps = [
":cel",
":cel_impl",
"//checker",
"//common:options",
"//compiler",
"//compiler:compiler_builder",
"//parser",
"//runtime",
],
)

java_library(
name = "cel_impl",
srcs = ["CelImpl.java"],
tags = [
],
deps = [
":cel",
"//checker:checker_builder",
"//checker:checker_legacy_environment",
"//checker:proto_type_mask",
"//checker:standard_decl",
"//checker:type_provider_legacy",
"//common:cel_ast",
"//common:cel_source",
"//common:compiler_common",
"//common:container",
"//common:options",
"//common/annotations",
"//common/internal:env_visitor",
"//common/internal:file_descriptor_converter",
"//common/types:cel_proto_types",
"//common/types:type_providers",
"//common/values:cel_value_provider",
"//compiler",
"//compiler:compiler_builder",
"//parser",
"//parser:macro",
"//parser:parser_builder",
"//runtime",
"//runtime:function_binding",
"//runtime:runtime_planner_impl",
"//runtime:standard_functions",
"@cel_spec//proto/cel/expr:checked_java_proto",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
Expand All @@ -60,6 +101,7 @@ java_library(
tags = [
],
deps = [
":cel_factory",
":environment_exception",
":required_fields_checker",
"//:auto_value",
Expand Down
20 changes: 18 additions & 2 deletions bundle/src/main/java/dev/cel/bundle/CelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import dev.cel.common.CelSource;
import dev.cel.common.CelValidationResult;
import dev.cel.common.CelVarDecl;
import dev.cel.common.annotations.Internal;
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.internal.FileDescriptorSetConverter;
Expand All @@ -54,6 +55,7 @@
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntimeBuilder;
import dev.cel.runtime.CelRuntimeImpl;
import dev.cel.runtime.CelRuntimeLibrary;
import dev.cel.runtime.CelStandardFunctions;
import java.util.Arrays;
Expand All @@ -63,9 +65,14 @@
* Implementation of the synchronous CEL stack.
*
* <p>Note, the underlying {@link CelCompiler} and {@link CelRuntime} values are constructed lazily.
*
* <p>CEL Library Internals. Do Not Use. Consumers should use {@code CelFactory} instead.
*
* <p>TODO: Restrict visibility once factory is introduced
*/
@Immutable
final class CelImpl implements Cel, EnvVisitable {
@Internal
public final class CelImpl implements Cel, EnvVisitable {

// The lazily constructed compiler and runtime values are memoized and guaranteed to be
// constructed only once without side effects, thus making them effectively immutable.
Expand Down Expand Up @@ -142,8 +149,13 @@ static CelImpl combine(CelCompiler compiler, CelRuntime runtime) {
* Create a new builder for constructing a {@code CelImpl} instance.
*
* <p>By default, {@link CelOptions#DEFAULT} are enabled, as is the CEL standard environment.
*
* <p>CEL Library Internals. Do Not Use. Consumers should use {@code CelFactory} instead.
*
* <p>TODO: Restrict visibility once factory is introduced
*/
static CelBuilder newBuilder(
@Internal
public static CelBuilder newBuilder(
CelCompilerBuilder compilerBuilder, CelRuntimeBuilder celRuntimeBuilder) {
return new CelImpl.Builder(compilerBuilder, celRuntimeBuilder);
}
Expand Down Expand Up @@ -199,6 +211,10 @@ public CelContainer container() {
@Override
public CelBuilder setContainer(CelContainer container) {
compilerBuilder.setContainer(container);
if (runtimeBuilder instanceof CelRuntimeImpl.Builder) {
runtimeBuilder.setContainer(container);
}

return this;
}

Expand Down
1 change: 1 addition & 0 deletions bundle/src/test/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ java_library(
deps = [
"//:java_truth",
"//bundle:cel",
"//bundle:cel_impl",
"//bundle:environment",
"//bundle:environment_exception",
"//bundle:environment_exporter",
Expand Down
108 changes: 63 additions & 45 deletions extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
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 static dev.cel.common.Operator.INDEX;
import static dev.cel.common.Operator.OPTIONAL_INDEX;
import static dev.cel.common.Operator.OPTIONAL_SELECT;
import static dev.cel.extensions.CelOptionalLibrary.Function.FIRST;
import static dev.cel.extensions.CelOptionalLibrary.Function.HAS_VALUE;
import static dev.cel.extensions.CelOptionalLibrary.Function.LAST;
Expand Down Expand Up @@ -342,54 +345,69 @@ public void setRuntimeOptions(
"optional_hasValue", Object.class, val -> ((Optional<?>) val).isPresent())));

runtimeBuilder.addFunctionBindings(
CelFunctionBinding.from(
"select_optional_field", // This only handles map selection. Proto selection is
// special cased inside the interpreter.
Map.class,
String.class,
runtimeEquality::findInMap),
CelFunctionBinding.from(
"map_optindex_optional_value", Map.class, Object.class, runtimeEquality::findInMap),
CelFunctionBinding.from(
"optional_map_optindex_optional_value",
Optional.class,
Object.class,
(Optional optionalMap, Object key) ->
indexOptionalMap(optionalMap, key, runtimeEquality)),
CelFunctionBinding.from(
"optional_map_index_value",
Optional.class,
Object.class,
(Optional optionalMap, Object key) ->
indexOptionalMap(optionalMap, key, runtimeEquality)),
CelFunctionBinding.from(
"optional_list_index_int",
Optional.class,
Long.class,
CelOptionalLibrary::indexOptionalList),
CelFunctionBinding.from(
"list_optindex_optional_int",
List.class,
Long.class,
(List list, Long index) -> {
int castIndex = Ints.checkedCast(index);
if (castIndex < 0 || castIndex >= list.size()) {
return Optional.empty();
}
return Optional.of(list.get(castIndex));
}),
CelFunctionBinding.from(
"optional_list_optindex_optional_int",
Optional.class,
Long.class,
CelOptionalLibrary::indexOptionalList));
fromOverloads(
OPTIONAL_SELECT.getFunction(),
CelFunctionBinding.from(
"select_optional_field", // This only handles map selection. Proto selection is
// special cased inside the interpreter.
Map.class,
String.class,
runtimeEquality::findInMap)));

runtimeBuilder.addFunctionBindings(
fromOverloads(
OPTIONAL_INDEX.getFunction(),
CelFunctionBinding.from(
"list_optindex_optional_int",
List.class,
Long.class,
(List list, Long index) -> {
int castIndex = Ints.checkedCast(index);
if (castIndex < 0 || castIndex >= list.size()) {
return Optional.empty();
}
return Optional.of(list.get(castIndex));
}),
CelFunctionBinding.from(
"optional_list_optindex_optional_int",
Optional.class,
Long.class,
CelOptionalLibrary::indexOptionalList),
CelFunctionBinding.from(
"map_optindex_optional_value", Map.class, Object.class, runtimeEquality::findInMap),
CelFunctionBinding.from(
"optional_map_optindex_optional_value",
Optional.class,
Object.class,
(Optional optionalMap, Object key) ->
indexOptionalMap(optionalMap, key, runtimeEquality))));

runtimeBuilder.addFunctionBindings(
fromOverloads(
INDEX.getFunction(),
CelFunctionBinding.from(
"optional_list_index_int",
Optional.class,
Long.class,
CelOptionalLibrary::indexOptionalList),
CelFunctionBinding.from(
"optional_map_index_value",
Optional.class,
Object.class,
(Optional optionalMap, Object key) ->
indexOptionalMap(optionalMap, key, runtimeEquality))));

if (version >= 2) {
runtimeBuilder.addFunctionBindings(
CelFunctionBinding.from(
"optional_list_first", Collection.class, CelOptionalLibrary::listOptionalFirst),
CelFunctionBinding.from(
"optional_list_last", Collection.class, CelOptionalLibrary::listOptionalLast));
fromOverloads(
"first",
CelFunctionBinding.from(
"optional_list_first", Collection.class, CelOptionalLibrary::listOptionalFirst)));
runtimeBuilder.addFunctionBindings(
fromOverloads(
"last",
CelFunctionBinding.from(
"optional_list_last", Collection.class, CelOptionalLibrary::listOptionalLast)));
}
}

Expand Down
4 changes: 4 additions & 0 deletions extensions/src/test/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ java_library(
deps = [
"//:java_truth",
"//bundle:cel",
"//bundle:cel_impl",
"//checker",
"//common:cel_ast",
"//common:compiler_common",
"//common:container",
Expand All @@ -30,13 +32,15 @@ java_library(
"//extensions:sets",
"//extensions:sets_function",
"//extensions:strings",
"//parser",
"//parser:macro",
"//parser:unparser",
"//runtime",
"//runtime:function_binding",
"//runtime:interpreter_util",
"//runtime:lite_runtime",
"//runtime:lite_runtime_factory",
"//runtime:runtime_planner_impl",
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
"@cel_spec//proto/cel/expr/conformance/test:simple_java_proto",
Expand Down
Loading
Loading