Add option to specify folding designated custom functions only by copybara-service[bot] · Pull Request #402 · cel-expr/cel-java · GitHub
Skip to content
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
1 change: 1 addition & 0 deletions extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package dev.cel.extensions;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.ByteString;
import dev.cel.checker.CelCheckerBuilder;
Expand All @@ -35,31 +36,58 @@ public class CelEncoderExtensions implements CelCompilerLibrary, CelRuntimeLibra

private static final Decoder BASE64_DECODER = Base64.getDecoder();

@Override
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
checkerBuilder.addFunctionDeclarations(
private final ImmutableSet<Function> functions;

enum Function {
DECODE(
CelFunctionDecl.newFunctionDeclaration(
"base64.decode",
CelOverloadDecl.newGlobalOverload(
"base64_decode_string", SimpleType.BYTES, SimpleType.STRING)),
ImmutableSet.of(
CelRuntime.CelFunctionBinding.from(
"base64_decode_string",
String.class,
str -> ByteString.copyFrom(BASE64_DECODER.decode(str))))),
ENCODE(
CelFunctionDecl.newFunctionDeclaration(
"base64.encode",
CelOverloadDecl.newGlobalOverload(
"base64_encode_bytes", SimpleType.STRING, SimpleType.BYTES)));
"base64_encode_bytes", SimpleType.STRING, SimpleType.BYTES)),
ImmutableSet.of(
CelRuntime.CelFunctionBinding.from(
"base64_encode_bytes",
ByteString.class,
bytes -> BASE64_ENCODER.encodeToString(bytes.toByteArray())))),
;

private final CelFunctionDecl functionDecl;
private final ImmutableSet<CelRuntime.CelFunctionBinding> functionBindings;

String getFunction() {
return functionDecl.name();
}

Function(
CelFunctionDecl functionDecl,
ImmutableSet<CelRuntime.CelFunctionBinding> functionBindings) {
this.functionDecl = functionDecl;
this.functionBindings = functionBindings;
}
}

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

@SuppressWarnings("Immutable") // Instances of java.util.Base64 are immutable
@Override
public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
runtimeBuilder.addFunctionBindings(
CelRuntime.CelFunctionBinding.from(
"base64_decode_string",
String.class,
str -> ByteString.copyFrom(BASE64_DECODER.decode(str))),
CelRuntime.CelFunctionBinding.from(
"base64_encode_bytes",
ByteString.class,
bytes -> BASE64_ENCODER.encodeToString(bytes.toByteArray())));
functions.forEach(function -> runtimeBuilder.addFunctionBindings(function.functionBindings));
}
}

public CelEncoderExtensions() {
this.functions = ImmutableSet.copyOf(Function.values());
}
}
24 changes: 24 additions & 0 deletions extensions/src/main/java/dev/cel/extensions/CelExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package dev.cel.extensions;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.Arrays.stream;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import dev.cel.common.CelOptions;
import java.util.Set;

Expand Down Expand Up @@ -206,5 +210,25 @@ public static CelSetsExtensions sets(Set<CelSetsExtensions.Function> functions)
return new CelSetsExtensions(functions);
}

/**
* Retrieves all function names used by every extension libraries.
*
* <p>Note: Certain extensions such as {@link CelProtoExtensions} and {@link
* CelBindingsExtensions} are implemented via macros, not functions, and those are not included
* here.
*/
public static ImmutableSet<String> getAllFunctionNames() {
return Streams.concat(
stream(CelMathExtensions.Function.values())
.map(CelMathExtensions.Function::getFunction),
stream(CelStringExtensions.Function.values())
.map(CelStringExtensions.Function::getFunction),
stream(CelSetsExtensions.Function.values())
.map(CelSetsExtensions.Function::getFunction),
stream(CelEncoderExtensions.Function.values())
.map(CelEncoderExtensions.Function::getFunction))
.collect(toImmutableSet());
}

private CelExtensions() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ final class CelMathExtensions implements CelCompilerLibrary, CelRuntimeLibrary {
return builder.buildOrThrow();
}

public enum Function {
enum Function {
MAX(
CelFunctionDecl.newFunctionDeclaration(
MATH_MAX_FUNCTION,
Expand Down Expand Up @@ -341,6 +341,10 @@ public enum Function {
private final ImmutableSet<CelRuntime.CelFunctionBinding> functionBindingsULongSigned;
private final ImmutableSet<CelRuntime.CelFunctionBinding> functionBindingsULongUnsigned;

String getFunction() {
return functionDecl.name();
}

Function(
CelFunctionDecl functionDecl,
ImmutableSet<CelRuntime.CelFunctionBinding> functionBindings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public enum Function {
private final CelFunctionDecl functionDecl;
private final ImmutableSet<CelRuntime.CelFunctionBinding> functionBindings;

String getFunction() {
return functionDecl.name();
}

Function(CelFunctionDecl functionDecl, CelRuntime.CelFunctionBinding... functionBindings) {
this.functionDecl = functionDecl;
this.functionBindings = ImmutableSet.copyOf(functionBindings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ public enum Function {
private final CelFunctionDecl functionDecl;
private final ImmutableSet<CelRuntime.CelFunctionBinding> functionBindings;

String getFunction() {
return functionDecl.name();
}

Function(CelFunctionDecl functionDecl, CelRuntime.CelFunctionBinding... functionBindings) {
this.functionDecl = functionDecl;
this.functionBindings = ImmutableSet.copyOf(functionBindings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,27 @@ public void addEncoderExtension_success() throws Exception {

assertThat(evaluatedResult).isTrue();
}
}

@Test
public void getAllFunctionNames() {
assertThat(CelExtensions.getAllFunctionNames())
.containsExactly(
"math.@max",
"math.@min",
"charAt",
"indexOf",
"join",
"lastIndexOf",
"lowerAscii",
"replace",
"split",
"substring",
"trim",
"upperAscii",
"sets.contains",
"sets.equivalent",
"sets.intersects",
"base64.decode",
"base64.encode");
}
}
21 changes: 18 additions & 3 deletions optimizer/src/main/java/dev/cel/optimizer/optimizers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ java_library(
tags = [
],
deps = [
":default_optimizer_constants",
"//:auto_value",
"//bundle:cel",
"//common",
Expand All @@ -29,6 +30,7 @@ java_library(
"//optimizer:optimization_exception",
"//parser:operator",
"//runtime",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)
Expand All @@ -41,9 +43,9 @@ java_library(
tags = [
],
deps = [
":default_optimizer_constants",
"//:auto_value",
"//bundle:cel",
"//checker:checker_legacy_environment",
"//common",
"//common:compiler_common",
"//common:mutable_ast",
Expand All @@ -55,12 +57,25 @@ java_library(
"//common/navigation:mutable_navigation",
"//common/types",
"//common/types:type_providers",
"//extensions:optional_library",
"//optimizer:ast_optimizer",
"//optimizer:mutable_ast",
"//parser:operator",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:org_jspecify_jspecify",
],
)

java_library(
name = "default_optimizer_constants",
srcs = [
"DefaultOptimizerConstants.java",
],
visibility = ["//visibility:private"],
deps = [
"//checker:checker_legacy_environment",
"//extensions",
"//extensions:optional_library",
"//parser:operator",
"@maven//:com_google_guava_guava",
],
)
Loading