Skip to content

Commit 2c6bd52

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
This change introduces a new CEL extension that provides types and functions for working with IP addresses and CIDR ranges. The extension defines net.IP and net.CIDR opaque types.
PiperOrigin-RevId: 850215963
1 parent bee8115 commit 2c6bd52

File tree

8 files changed

+1205
-35
lines changed

8 files changed

+1205
-35
lines changed

extensions/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ java_library(
3737
exports = ["//extensions/src/main/java/dev/cel/extensions:math"],
3838
)
3939

40+
java_library(
41+
name = "network",
42+
exports = ["//extensions/src/main/java/dev/cel/extensions:network"],
43+
)
44+
4045
java_library(
4146
name = "optional_library",
4247
exports = ["//extensions/src/main/java/dev/cel/extensions:optional_library"],

extensions/src/main/java/dev/cel/extensions/BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ java_library(
3434
":encoders",
3535
":lists",
3636
":math",
37+
":network",
3738
":optional_library",
3839
":protos",
3940
":regex",
@@ -135,6 +136,25 @@ java_library(
135136
],
136137
)
137138

139+
java_library(
140+
name = "network",
141+
srcs = ["CelNetworkExtensions.java"],
142+
deps = [
143+
":extension_library",
144+
"//checker:checker_builder",
145+
"//common:compiler_common",
146+
"//common/types",
147+
"//common/types:type_providers",
148+
"//compiler:compiler_builder",
149+
"//java/com/google/common/net",
150+
"//java/com/google/net/base:base-core",
151+
"//runtime",
152+
"//runtime:function_binding",
153+
"@maven//:com_google_errorprone_error_prone_annotations",
154+
"@maven//:com_google_guava_guava",
155+
],
156+
)
157+
138158
java_library(
139159
name = "bindings",
140160
srcs = ["CelBindingsExtensions.java"],

extensions/src/main/java/dev/cel/extensions/CelExtensions.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public final class CelExtensions {
3636
private static final CelRegexExtensions REGEX_EXTENSIONS = new CelRegexExtensions();
3737
private static final CelComprehensionsExtensions COMPREHENSIONS_EXTENSIONS =
3838
new CelComprehensionsExtensions();
39+
private static final CelNetworkExtensions NETWORK_EXTENSIONS = new CelNetworkExtensions();
3940

4041
/**
4142
* Implementation of optional values.
@@ -319,6 +320,18 @@ public static CelComprehensionsExtensions comprehensions() {
319320
return COMPREHENSIONS_EXTENSIONS;
320321
}
321322

323+
/**
324+
* Extended functions for Network manipulation.
325+
*
326+
* <p>Refer to README.md for available functions.
327+
*
328+
* <p>This will include all functions denoted in {@link CelNetworkExtensions.Function}, including
329+
* any future additions.
330+
*/
331+
public static CelNetworkExtensions network() {
332+
return NETWORK_EXTENSIONS;
333+
}
334+
322335
/**
323336
* Retrieves all function names used by every extension libraries.
324337
*
@@ -339,38 +352,30 @@ public static ImmutableSet<String> getAllFunctionNames() {
339352
.map(CelListsExtensions.Function::getFunction),
340353
stream(CelRegexExtensions.Function.values())
341354
.map(CelRegexExtensions.Function::getFunction),
355+
stream(CelNetworkExtensions.Function.values())
356+
.map(CelNetworkExtensions.Function::getFunction),
342357
stream(CelComprehensionsExtensions.Function.values())
343358
.map(CelComprehensionsExtensions.Function::getFunction))
344359
.collect(toImmutableSet());
345360
}
346361

347362
public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getExtensionLibrary(
348363
String name, CelOptions options) {
349-
switch (name) {
350-
case "bindings":
351-
return CelBindingsExtensions.library();
352-
case "encoders":
353-
return CelEncoderExtensions.library(options);
354-
case "lists":
355-
return CelListsExtensions.library();
356-
case "math":
357-
return CelMathExtensions.library(options);
358-
case "optional":
359-
return CelOptionalLibrary.library();
360-
case "protos":
361-
return CelProtoExtensions.library();
362-
case "regex":
363-
return CelRegexExtensions.library();
364-
case "sets":
365-
return CelSetsExtensions.library(options);
366-
case "strings":
367-
return CelStringExtensions.library();
368-
case "comprehensions":
369-
return CelComprehensionsExtensions.library();
364+
return switch (name) {
365+
case "bindings" -> CelBindingsExtensions.library();
366+
case "encoders" -> CelEncoderExtensions.library(options);
367+
case "lists" -> CelListsExtensions.library();
368+
case "math" -> CelMathExtensions.library(options);
369+
case "network" -> CelNetworkExtensions.library();
370+
case "optional" -> CelOptionalLibrary.library();
371+
case "protos" -> CelProtoExtensions.library();
372+
case "regex" -> CelRegexExtensions.library();
373+
case "sets" -> CelSetsExtensions.library(options);
374+
case "strings" -> CelStringExtensions.library();
375+
case "comprehensions" -> CelComprehensionsExtensions.library();
370376
// TODO: add support for remaining standard extensions
371-
default:
372-
throw new IllegalArgumentException("Unknown standard extension '" + name + "'");
373-
}
377+
default -> throw new IllegalArgumentException("Unknown standard extension '" + name + "'");
378+
};
374379
}
375380

376381
private CelExtensions() {}

0 commit comments

Comments
 (0)