Skip to content

Commit 2b9a5c4

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Add versions to the 'optional' library to gradually expose new functions.
Add functions `list.first()` and `list.last()`, both returning `optional` PiperOrigin-RevId: 789826893
1 parent 3167f60 commit 2b9a5c4

File tree

7 files changed

+385
-92
lines changed

7 files changed

+385
-92
lines changed

bundle/src/main/java/dev/cel/bundle/CelEnvironment.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import dev.cel.compiler.CelCompilerBuilder;
4848
import dev.cel.compiler.CelCompilerLibrary;
4949
import dev.cel.extensions.CelExtensions;
50-
import dev.cel.extensions.CelOptionalLibrary;
5150
import dev.cel.parser.CelStandardMacro;
5251
import dev.cel.runtime.CelRuntimeBuilder;
5352
import dev.cel.runtime.CelRuntimeLibrary;
@@ -694,8 +693,8 @@ enum CanonicalCelExtension {
694693
(options, version) -> CelExtensions.math(options, version),
695694
(options, version) -> CelExtensions.math(options, version)),
696695
OPTIONAL(
697-
(options, version) -> CelOptionalLibrary.INSTANCE,
698-
(options, version) -> CelOptionalLibrary.INSTANCE),
696+
(options, version) -> CelExtensions.optional(version),
697+
(options, version) -> CelExtensions.optional(version)),
699698
STRINGS(
700699
(options, version) -> CelExtensions.strings(),
701700
(options, version) -> CelExtensions.strings()),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ java_library(
3333
":encoders",
3434
":lists",
3535
":math",
36+
":optional_library",
3637
":protos",
3738
":regex",
3839
":sets",
@@ -177,6 +178,7 @@ java_library(
177178
"//common/ast",
178179
"//common/types",
179180
"//compiler:compiler_builder",
181+
"//extensions:extension_library",
180182
"//parser:macro",
181183
"//parser:operator",
182184
"//parser:parser_builder",

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.google.common.collect.ImmutableSet;
1818
import dev.cel.common.CelFunctionDecl;
19+
import dev.cel.common.CelVarDecl;
1920
import dev.cel.parser.CelMacro;
2021
import java.util.Comparator;
2122

@@ -66,6 +67,9 @@ default ImmutableSet<CelMacro> macros() {
6667
return ImmutableSet.of();
6768
}
6869

69-
// TODO - Add a method for variables.
70+
/** Returns the set of variables defined by this extension library. */
71+
default ImmutableSet<CelVarDecl> variables() {
72+
return ImmutableSet.of();
73+
}
7074
}
7175
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ public final class CelExtensions {
3636
private static final CelEncoderExtensions ENCODER_EXTENSIONS = new CelEncoderExtensions();
3737
private static final CelRegexExtensions REGEX_EXTENSIONS = new CelRegexExtensions();
3838

39+
/**
40+
* Implementation of optional values.
41+
*
42+
* <p>Refer to README.md for available functions.
43+
*/
44+
public static CelOptionalLibrary optional() {
45+
return CelOptionalLibrary.library().latest();
46+
}
47+
48+
/**
49+
* Implementation of optional values.
50+
*
51+
* <p>Refer to README.md for available functions for each supported version.
52+
*/
53+
public static CelOptionalLibrary optional(int version) {
54+
return CelOptionalLibrary.library().version(version);
55+
}
56+
3957
/**
4058
* Extended functions for string manipulation.
4159
*
@@ -311,6 +329,8 @@ public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getE
311329
return CelListsExtensions.library();
312330
case "math":
313331
return CelMathExtensions.library(options);
332+
case "optional":
333+
return CelOptionalLibrary.library();
314334
case "protos":
315335
return CelProtoExtensions.library();
316336
case "regex":

0 commit comments

Comments
 (0)