Skip to content
Open
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
55 changes: 26 additions & 29 deletions bundle/src/main/java/dev/cel/bundle/CelEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,19 @@ public abstract class CelEnvironment {

@VisibleForTesting
static final ImmutableMap<String, CanonicalCelExtension> CEL_EXTENSION_CONFIG_MAP =
ImmutableMap.of(
"bindings", CanonicalCelExtension.BINDINGS,
"encoders", CanonicalCelExtension.ENCODERS,
"lists", CanonicalCelExtension.LISTS,
"math", CanonicalCelExtension.MATH,
"optional", CanonicalCelExtension.OPTIONAL,
"protos", CanonicalCelExtension.PROTOS,
"regex", CanonicalCelExtension.REGEX,
"sets", CanonicalCelExtension.SETS,
"strings", CanonicalCelExtension.STRINGS,
"two-var-comprehensions", CanonicalCelExtension.COMPREHENSIONS);
ImmutableMap.<String, CanonicalCelExtension>builder()
.put("bindings", CanonicalCelExtension.BINDINGS)
.put("encoders", CanonicalCelExtension.ENCODERS)
.put("lists", CanonicalCelExtension.LISTS)
.put("math", CanonicalCelExtension.MATH)
.put("network", CanonicalCelExtension.NETWORK)
.put("optional", CanonicalCelExtension.OPTIONAL)
.put("protos", CanonicalCelExtension.PROTOS)
.put("regex", CanonicalCelExtension.REGEX)
.put("sets", CanonicalCelExtension.SETS)
.put("strings", CanonicalCelExtension.STRINGS)
.put("two-var-comprehensions", CanonicalCelExtension.COMPREHENSIONS)
.buildOrThrow();

private static final ImmutableMap<String, ObjIntConsumer<CelOptions.Builder>> LIMIT_HANDLERS =
ImmutableMap.of(
Expand Down Expand Up @@ -279,10 +281,9 @@ public CelCompiler extend(CelCompiler celCompiler, CelOptions celOptions)
applyStandardLibrarySubset(compilerBuilder);

contextVariable()
.ifPresent(
cv ->
compilerBuilder.addProtoTypeMasks(
ProtoTypeMask.ofAllFields(cv.typeName()).withFieldsAsVariableDeclarations()));
.ifPresent(cv ->
compilerBuilder.addProtoTypeMasks(
ProtoTypeMask.ofAllFields(cv.typeName()).withFieldsAsVariableDeclarations()));

return compilerBuilder.build();
} catch (RuntimeException e) {
Expand Down Expand Up @@ -392,19 +393,17 @@ private void applyStandardLibrarySubset(CelCompilerBuilder compilerBuilder) {
.setStandardEnvironmentEnabled(false)
.setStandardDeclarations(
CelStandardDeclarations.newBuilder()
.filterFunctions(
(function, overload) ->
FunctionSelector.matchesAny(function, overload, includedFunctions))
.filterFunctions((function, overload) ->
FunctionSelector.matchesAny(function, overload, includedFunctions))
.build());
} else if (!librarySubset.excludedFunctions().isEmpty()) {
ImmutableSet<FunctionSelector> excludedFunctions = librarySubset.excludedFunctions();
compilerBuilder
.setStandardEnvironmentEnabled(false)
.setStandardDeclarations(
CelStandardDeclarations.newBuilder()
.filterFunctions(
(function, overload) ->
!FunctionSelector.matchesAny(function, overload, excludedFunctions))
.filterFunctions((function, overload) ->
!FunctionSelector.matchesAny(function, overload, excludedFunctions))
.build());
}
}
Expand Down Expand Up @@ -655,10 +654,7 @@ private CelOverloadDecl toCelOverloadDecl(Function<String, Optional<CelType>> ty

target()
.ifPresent(
t ->
builder
.setIsInstanceFunction(true)
.addParameterTypes(t.toCelType(typeFinder)));
t -> builder.setIsInstanceFunction(true).addParameterTypes(t.toCelType(typeFinder)));

for (TypeDecl type : arguments()) {
builder.addParameterTypes(type.toCelType(typeFinder));
Expand Down Expand Up @@ -823,16 +819,14 @@ private CelType toCelType(Function<String, Optional<CelType>> typeFinder) {

Optional<CelType> builtinType = findCelTypeByName(name());
if (builtinType.isPresent()) {
checkArgument(
params().isEmpty(), "Type %s cannot have type parameters", name());
checkArgument(params().isEmpty(), "Type %s cannot have type parameters", name());
return builtinType.get();
}

CelType resolvedType =
typeFinder
.apply(name())
.orElseThrow(
() -> new IllegalArgumentException("Undefined type name: " + name()));
.orElseThrow(() -> new IllegalArgumentException("Undefined type name: " + name()));
if (!params().isEmpty()) {
return resolvedType.withParameters(
params().stream().map(p -> p.toCelType(typeFinder)).collect(toImmutableList()));
Expand Down Expand Up @@ -1007,6 +1001,9 @@ enum CanonicalCelExtension {
MATH(
(options, version) -> CelExtensions.math(options, version),
(options, version) -> CelExtensions.math(options, version)),
NETWORK(
(options, version) -> CelExtensions.network(version),
(options, version) -> CelExtensions.network(version)),
OPTIONAL(
(options, version) -> CelExtensions.optional(version),
(options, version) -> CelExtensions.optional(version)),
Expand Down
5 changes: 4 additions & 1 deletion bundle/src/test/java/dev/cel/bundle/CelEnvironmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void extend_allExtensions() throws Exception {
ExtensionConfig.latest("encoders"),
ExtensionConfig.latest("lists"),
ExtensionConfig.latest("math"),
ExtensionConfig.latest("network"),
ExtensionConfig.latest("optional"),
ExtensionConfig.latest("protos"),
ExtensionConfig.latest("regex"),
Expand All @@ -104,7 +105,9 @@ public void extend_allExtensions() throws Exception {
CelAbstractSyntaxTree ast =
cel.compile(
"cel.bind(x, 10, math.greatest([1,x])) < int(' 11 '.trim()) &&"
+ " optional.none().orValue(true) && [].flatten() == []")
+ " optional.none().orValue(true) && [].flatten() == [] &&"
+ " cidr('10.0.0.0/8').containsIP(ip('10.0.0.1')) &&"
+ " type(ip('10.0.0.1')) == net.IP")
.getAst();
boolean result = (boolean) cel.createProgram(ast).eval();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ private DefaultTypeProvider() {
"optional_type",
// TODO: Move to CelOptionalLibrary and register it on demand
OptionalType.create(SimpleType.DYN));
typeMapBuilder.put(
"net.IP",
// TODO: Move to CelNetworkExtensions and register it on demand
OpaqueType.create("net.IP"));
typeMapBuilder.put(
"net.CIDR",
// TODO: Move to CelNetworkExtensions and register it on demand
OpaqueType.create("net.CIDR"));
this.commonTypes = typeMapBuilder.buildOrThrow();
}
}
10 changes: 10 additions & 0 deletions conformance/src/test/java/dev/cel/conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ java_library(
"//extensions:encoders_runtime_library",
"//extensions:lists_runtime_library",
"//extensions:math_runtime_library",
"//extensions:network_runtime_library",
"//extensions:optional_library",
"//extensions:optional_runtime_library",
"//extensions:sets_runtime_library",
Expand Down Expand Up @@ -123,6 +124,7 @@ _ALL_TESTS = [
"@cel_conformance//tests/simple:testdata/macros2.textproto",
"@cel_conformance//tests/simple:testdata/math_ext.textproto",
"@cel_conformance//tests/simple:testdata/namespace.textproto",
"@cel_conformance//tests/simple:testdata/network_ext.textproto",
"@cel_conformance//tests/simple:testdata/optionals.textproto",
"@cel_conformance//tests/simple:testdata/parse.textproto",
"@cel_conformance//tests/simple:testdata/plumbing.textproto",
Expand Down Expand Up @@ -165,6 +167,10 @@ _TESTS_TO_SKIP_LEGACY = [
# "enums/legacy_proto3/assign_standalone_int_big",
# "enums/legacy_proto3/assign_standalone_int_neg",

# SimpleTest expects an evaluation error while disable_check is false, but this triggers a
# compile-time type-check error before evaluation.
"network_ext/ip_type/is_ip_cidr_compile_error",

# Type inference edgecases around null(able) assignability.
# These type check, but resolve to a different type.
# list(int), want list(wrapper(int))
Expand All @@ -182,6 +188,10 @@ _TESTS_TO_SKIP_PLANNER = [
# Not worth fixing until we see a concrete need.
"basic/functions/unbound_is_runtime_error",

# SimpleTest expects an evaluation error while disable_check is false, but this triggers a
# compile-time type-check error before evaluation.
"network_ext/ip_type/is_ip_cidr_compile_error",

# Type inference edgecases around null(able) assignability.
# These type check, but resolve to a different type.
# list(int), want list(wrapper(int))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import dev.cel.extensions.CelExtensions;
import dev.cel.extensions.CelListsRuntimeLibrary;
import dev.cel.extensions.CelMathRuntimeLibrary;
import dev.cel.extensions.CelNetworkRuntimeLibrary;
import dev.cel.extensions.CelOptionalLibrary;
import dev.cel.extensions.CelOptionalRuntimeLibrary;
import dev.cel.extensions.CelSetsRuntimeLibrary;
Expand Down Expand Up @@ -104,6 +105,7 @@ public final class ConformanceTest extends Statement {
CelExtensions.encoders(OPTIONS),
CelExtensions.lists(),
CelExtensions.math(),
CelExtensions.network(),
CelExtensions.protos(),
CelExtensions.sets(OPTIONS),
CelExtensions.strings(),
Expand All @@ -116,6 +118,7 @@ public final class ConformanceTest extends Statement {
CelExtensions.encoders(OPTIONS),
CelExtensions.lists(),
CelExtensions.math(),
CelExtensions.network(),
CelExtensions.sets(OPTIONS),
CelExtensions.strings(),
CelOptionalLibrary.INSTANCE);
Expand All @@ -126,6 +129,7 @@ public final class ConformanceTest extends Statement {
CelEncoderRuntimeLibrary.encoders(),
CelListsRuntimeLibrary.lists(),
CelMathRuntimeLibrary.math(),
CelNetworkRuntimeLibrary.network(),
CelOptionalRuntimeLibrary.optional(),
CelSetsRuntimeLibrary.sets(),
CelStringRuntimeLibrary.strings());
Expand Down
32 changes: 32 additions & 0 deletions extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,35 @@ cel_android_library(
name = "regex_runtime_library_android",
exports = ["//extensions/src/main/java/dev/cel/extensions:regex_runtime_library_android"],
)

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

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

cel_android_library(
name = "network_types_android",
exports = ["//extensions/src/main/java/dev/cel/extensions:network_types_android"],
)

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

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

cel_android_library(
name = "network_runtime_library_android",
exports = ["//extensions/src/main/java/dev/cel/extensions:network_runtime_library_android"],
)
104 changes: 104 additions & 0 deletions extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ java_library(
":lists",
":math",
":native",
":network",
":optional_library",
":protos",
":regex",
Expand Down Expand Up @@ -599,6 +600,109 @@ cel_android_library(
],
)

java_library(
name = "network_types",
srcs = [
"CelCidr.java",
"CelIp.java",
],
tags = [
],
deps = [
"//common/types",
"//common/values",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:org_jspecify_jspecify",
],
)

cel_android_library(
name = "network_types_android",
srcs = [
"CelCidr.java",
"CelIp.java",
],
tags = [
],
deps = [
"//common/types:types_android",
"//common/values:values_android",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:org_jspecify_jspecify",
],
)

java_library(
name = "network",
srcs = ["CelNetworkExtensions.java"],
tags = [
],
exports = [":network_types"],
deps = [
":extension_library",
":network_compiler_library",
":network_runtime_library",
"//checker:checker_builder",
"//common:cel_function_decl",
"//common:cel_var_decl",
"//compiler:compiler_builder",
"//runtime",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "network_compiler_library",
srcs = ["CelNetworkCompilerLibrary.java"],
tags = [
],
deps = [
":extension_library",
":network_types",
"//checker:checker_builder",
"//common:cel_function_decl",
"//common:cel_overload_decl",
"//common:cel_var_decl",
"//common/types",
"//compiler:compiler_builder",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "network_runtime_library",
srcs = ["CelNetworkRuntimeLibrary.java"],
tags = [
],
exports = [":network_types"],
deps = [
":network_types",
"//common/exceptions:bad_format",
"//runtime:function_binding",
"//runtime:lite_runtime",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

cel_android_library(
name = "network_runtime_library_android",
srcs = ["CelNetworkRuntimeLibrary.java"],
tags = [
],
exports = [":network_types_android"],
deps = [
":network_types_android",
"//common/exceptions:bad_format",
"//runtime:function_binding_android",
"//runtime:lite_runtime_android",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
],
)

java_library(
name = "comprehensions",
srcs = ["CelComprehensionsExtensions.java"],
Expand Down
Loading
Loading