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
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public abstract static class Builder {
@CanIgnoreReturnValue
public Builder addStandardExtensions(CelOptions options) {
addExtensionLibraries(
CelExtensions.getExtensionLibrary("bindings", options),
CelExtensions.getExtensionLibrary("math", options),
CelExtensions.getExtensionLibrary("lists", options));
// TODO: add support for remaining standard extensions
Expand Down
1 change: 1 addition & 0 deletions 1 extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ java_library(
"//common:compiler_common",
"//common/ast",
"//compiler:compiler_builder",
"//extensions:extension_library",
"//parser:macro",
"//parser:parser_builder",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelIssue;
import dev.cel.common.ast.CelExpr;
import dev.cel.compiler.CelCompilerLibrary;
Expand All @@ -29,15 +31,47 @@

/** Internal implementation of the CEL local binding extensions. */
@Immutable
final class CelBindingsExtensions implements CelCompilerLibrary {

final class CelBindingsExtensions implements CelCompilerLibrary, CelExtensionLibrary.FeatureSet {
private static final String CEL_NAMESPACE = "cel";
private static final String UNUSED_ITER_VAR = "#unused";

private static final CelExtensionLibrary<CelBindingsExtensions> LIBRARY =
new CelExtensionLibrary<CelBindingsExtensions>() {
private final CelBindingsExtensions version0 = new CelBindingsExtensions();

@Override
public String name() {
return "bindings";
}

@Override
public ImmutableSet<CelBindingsExtensions> versions() {
return ImmutableSet.of(version0);
}
};

static CelExtensionLibrary<CelBindingsExtensions> library() {
return LIBRARY;
}

@Override
public int version() {
return 0;
}

@Override
public ImmutableSet<CelFunctionDecl> functions() {
return ImmutableSet.of();
}

@Override
public ImmutableSet<CelMacro> macros() {
return ImmutableSet.of(CelMacro.newReceiverMacro("bind", 3, CelBindingsExtensions::expandBind));
}

@Override
public void setParserOptions(CelParserBuilder parserBuilder) {
parserBuilder.addMacros(
CelMacro.newReceiverMacro("bind", 3, CelBindingsExtensions::expandBind));
parserBuilder.addMacros(macros());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ public static ImmutableSet<String> getAllFunctionNames() {
public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getExtensionLibrary(
String name, CelOptions options) {
switch (name) {
case "bindings":
return CelBindingsExtensions.library();
case "math":
return CelMathExtensions.library(options);
case "lists":
Expand Down
1 change: 1 addition & 0 deletions 1 extensions/src/test/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ java_library(
"//compiler",
"//compiler:compiler_builder",
"//extensions",
"//extensions:extension_library",
"//extensions:lite_extensions",
"//extensions:math",
"//extensions:optional_library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import com.google.testing.junit.testparameterinjector.TestParameters;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOptions;
import dev.cel.common.CelOverloadDecl;
import dev.cel.common.CelValidationException;
import dev.cel.common.types.SimpleType;
import dev.cel.common.types.StructTypeReference;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerFactory;
import dev.cel.expr.conformance.proto3.TestAllTypes;
import dev.cel.parser.CelMacro;
import dev.cel.parser.CelStandardMacro;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.runtime.CelRuntime;
Expand All @@ -54,6 +56,17 @@ public final class CelBindingsExtensionsTest {
.addLibraries(CelOptionalLibrary.INSTANCE)
.build();

@Test
public void library() {
CelExtensionLibrary<?> library =
CelExtensions.getExtensionLibrary("bindings", CelOptions.DEFAULT);
assertThat(library.name()).isEqualTo("bindings");
assertThat(library.latest().version()).isEqualTo(0);
assertThat(library.version(0).functions()).isEmpty();
assertThat(library.version(0).macros().stream().map(CelMacro::getFunction))
.containsExactly("bind");
}

private enum BindingTestCase {
BOOL_LITERAL("cel.bind(t, true, t)"),
STRING_CONCAT("cel.bind(msg, \"hello\", msg + msg + msg) == \"hellohellohello\""),
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.