Skip to content

Update the "strings" extension to be compatible with CelEnvironmentExporter #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,14 @@ public abstract static class Builder {
@CanIgnoreReturnValue
public Builder addStandardExtensions(CelOptions options) {
addExtensionLibraries(
CelExtensions.getExtensionLibrary("bindings", options),
CelExtensions.getExtensionLibrary("encoders", options),
CelExtensions.getExtensionLibrary("lists", options),
CelExtensions.getExtensionLibrary("math", options),
CelExtensions.getExtensionLibrary("lists", options));
CelExtensions.getExtensionLibrary("protos", options),
CelExtensions.getExtensionLibrary("regex", options),
CelExtensions.getExtensionLibrary("sets", options),
CelExtensions.getExtensionLibrary("strings", options));
// TODO: add support for remaining standard extensions
return this;
}
Expand Down
10 changes: 10 additions & 0 deletions extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ java_library(
"//common/internal",
"//common/types",
"//compiler:compiler_builder",
"//extensions:extension_library",
"//parser:macro",
"//runtime",
"//runtime:evaluation_exception_builder",
"//runtime:function_binding",
Expand All @@ -101,6 +103,7 @@ java_library(
"//common/ast",
"//common/internal",
"//compiler:compiler_builder",
"//extensions:extension_library",
"//parser:macro",
"//parser:parser_builder",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -138,6 +141,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 All @@ -153,6 +157,8 @@ java_library(
"//common:compiler_common",
"//common/types",
"//compiler:compiler_builder",
"//extensions:extension_library",
"//parser:macro",
"//runtime",
"//runtime:function_binding",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -200,6 +206,8 @@ java_library(
"//common/internal:dynamic_proto",
"//common/types",
"//compiler:compiler_builder",
"//extensions:extension_library",
"//parser:macro",
"//runtime",
"//runtime:proto_message_runtime_equality",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -275,6 +283,8 @@ java_library(
"//common:compiler_common",
"//common/types",
"//compiler:compiler_builder",
"//extensions:extension_library",
"//parser:macro",
"//runtime",
"//runtime:function_binding",
"@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 @@ -14,6 +14,8 @@

package dev.cel.extensions;

import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.ByteString;
Expand All @@ -22,6 +24,7 @@
import dev.cel.common.CelOverloadDecl;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompilerLibrary;
import dev.cel.parser.CelMacro;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.runtime.CelRuntimeBuilder;
import dev.cel.runtime.CelRuntimeLibrary;
Expand All @@ -31,7 +34,9 @@

/** Internal implementation of Encoder Extensions. */
@Immutable
public class CelEncoderExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
public class CelEncoderExtensions
implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet {

private static final Encoder BASE64_ENCODER = Base64.getEncoder();

private static final Decoder BASE64_DECODER = Base64.getDecoder();
Expand Down Expand Up @@ -74,6 +79,40 @@ String getFunction() {
}
}

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

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

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

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

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

@Override
public ImmutableSet<CelFunctionDecl> functions() {
return functions.stream().map(f -> f.functionDecl).collect(toImmutableSet());
}

@Override
public ImmutableSet<CelMacro> macros() {
return ImmutableSet.of();
}

@Override
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));
Expand Down
16 changes: 14 additions & 2 deletions extensions/src/main/java/dev/cel/extensions/CelExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,22 @@ public static ImmutableSet<String> getAllFunctionNames() {
public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getExtensionLibrary(
String name, CelOptions options) {
switch (name) {
case "math":
return CelMathExtensions.library(options);
case "bindings":
return CelBindingsExtensions.library();
case "encoders":
return CelEncoderExtensions.library();
case "lists":
return CelListsExtensions.library();
case "math":
return CelMathExtensions.library(options);
case "protos":
return CelProtoExtensions.library();
case "regex":
return CelRegexExtensions.library();
case "sets":
return CelSetsExtensions.library(options);
case "strings":
return CelStringExtensions.library();
// TODO: add support for remaining standard extensions
default:
throw new IllegalArgumentException("Unknown standard extension '" + name + "'");
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.common.internal.Constants;
Expand All @@ -30,18 +32,52 @@

/** Internal implementation of CEL proto extensions. */
@Immutable
final class CelProtoExtensions implements CelCompilerLibrary {
final class CelProtoExtensions implements CelCompilerLibrary, CelExtensionLibrary.FeatureSet {

private static final String PROTO_NAMESPACE = "proto";
private static final CelExpr ERROR = CelExpr.newBuilder().setConstant(Constants.ERROR).build();

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

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

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

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

@Override
public void setParserOptions(CelParserBuilder parserBuilder) {
parserBuilder.addMacros(
public int version() {
return 0;
}

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

@Override
public ImmutableSet<CelMacro> macros() {
return ImmutableSet.of(
CelMacro.newReceiverMacro("hasExt", 2, CelProtoExtensions::expandHasProtoExt),
CelMacro.newReceiverMacro("getExt", 2, CelProtoExtensions::expandGetProtoExt));
}

@Override
public void setParserOptions(CelParserBuilder parserBuilder) {
parserBuilder.addMacros(macros());
}

private static Optional<CelExpr> expandHasProtoExt(
CelMacroExprFactory exprFactory, CelExpr target, ImmutableList<CelExpr> arguments) {
return expandProtoExt(exprFactory, target, arguments, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package dev.cel.extensions;

import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
Expand All @@ -27,6 +29,7 @@
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompilerLibrary;
import dev.cel.parser.CelMacro;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.runtime.CelRuntimeBuilder;
import dev.cel.runtime.CelRuntimeLibrary;
Expand All @@ -35,7 +38,8 @@

/** Internal implementation of CEL regex extensions. */
@Immutable
final class CelRegexExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
final class CelRegexExtensions
implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet {

private static final String REGEX_REPLACE_FUNCTION = "regex.replace";
private static final String REGEX_EXTRACT_FUNCTION = "regex.extract";
Expand Down Expand Up @@ -124,6 +128,25 @@ String getFunction() {
}
}

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

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

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

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

private final ImmutableSet<Function> functions;

CelRegexExtensions() {
Expand All @@ -134,6 +157,21 @@ String getFunction() {
this.functions = ImmutableSet.copyOf(functions);
}

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

@Override
public ImmutableSet<CelFunctionDecl> functions() {
return functions.stream().map(f -> f.functionDecl).collect(toImmutableSet());
}

@Override
public ImmutableSet<CelMacro> macros() {
return ImmutableSet.of();
}

@Override
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));
Expand Down
Loading