Skip to content
Draft
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 .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.5.0
25 changes: 20 additions & 5 deletions bundle/src/main/java/dev/cel/bundle/CelEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import dev.cel.common.types.TypeParamType;
import dev.cel.common.types.TypeType;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerBuilder;
import dev.cel.compiler.CelCompilerLibrary;
Expand All @@ -69,9 +70,10 @@ public abstract class CelEnvironment {
"math", CanonicalCelExtension.MATH,
"optional", CanonicalCelExtension.OPTIONAL,
"protos", CanonicalCelExtension.PROTOS,
"regex", CanonicalCelExtension.REGEX,
"sets", CanonicalCelExtension.SETS,
"strings", CanonicalCelExtension.STRINGS,
"comprehensions", CanonicalCelExtension.COMPREHENSIONS);
"two-var-comprehensions", CanonicalCelExtension.COMPREHENSIONS);

/** Environment source in textual format (ex: textproto, YAML). */
public abstract Optional<Source> source();
Expand All @@ -82,7 +84,7 @@ public abstract class CelEnvironment {
/**
* Container, which captures default namespace and aliases for value resolution.
*/
public abstract CelContainer container();
public abstract Optional<CelContainer> container();

/**
* An optional description of the environment (example: location of the file containing the config
Expand Down Expand Up @@ -186,7 +188,6 @@ public static Builder newBuilder() {
return new AutoValue_CelEnvironment.Builder()
.setName("")
.setDescription("")
.setContainer(CelContainer.ofName(""))
.setVariables(ImmutableSet.of())
.setFunctions(ImmutableSet.of());
}
Expand All @@ -199,7 +200,6 @@ public CelCompiler extend(CelCompiler celCompiler, CelOptions celOptions)
CelCompilerBuilder compilerBuilder =
celCompiler
.toCompilerBuilder()
.setContainer(container())
.setTypeProvider(celTypeProvider)
.addVarDeclarations(
variables().stream()
Expand All @@ -210,6 +210,9 @@ public CelCompiler extend(CelCompiler celCompiler, CelOptions celOptions)
.map(f -> f.toCelFunctionDecl(celTypeProvider))
.collect(toImmutableList()));


container().ifPresent(compilerBuilder::setContainer);

addAllCompilerExtensions(compilerBuilder, celOptions);

applyStandardLibrarySubset(compilerBuilder);
Expand Down Expand Up @@ -349,6 +352,9 @@ public abstract static class VariableDecl {
/** The type of the variable. */
public abstract TypeDecl type();

/** Description of the variable. */
public abstract Optional<String> description();

/** Builder for {@link VariableDecl}. */
@AutoValue.Builder
public abstract static class Builder implements RequiredFieldsChecker {
Expand All @@ -361,6 +367,8 @@ public abstract static class Builder implements RequiredFieldsChecker {

public abstract VariableDecl.Builder setType(TypeDecl typeDecl);

public abstract VariableDecl.Builder setDescription(String name);

@Override
public ImmutableList<RequiredField> requiredFields() {
return ImmutableList.of(
Expand Down Expand Up @@ -600,6 +608,9 @@ public CelType toCelType(CelTypeProvider celTypeProvider) {
CelType keyType = params().get(0).toCelType(celTypeProvider);
CelType valueType = params().get(1).toCelType(celTypeProvider);
return MapType.create(keyType, valueType);
case "type":
checkState(params().size() == 1, "Expected 1 parameter for type, got " + params().size());
return TypeType.create(params().get(0).toCelType(celTypeProvider));
default:
if (isTypeParam()) {
return TypeParamType.create(name());
Expand Down Expand Up @@ -734,10 +745,14 @@ enum CanonicalCelExtension {
SETS(
(options, version) -> CelExtensions.sets(options),
(options, version) -> CelExtensions.sets(options)),
REGEX(
(options, version) -> CelExtensions.regex(),
(options, version) -> CelExtensions.regex()),
LISTS((options, version) -> CelExtensions.lists(), (options, version) -> CelExtensions.lists()),
COMPREHENSIONS(
(options, version) -> CelExtensions.comprehensions(),
(options, version) -> CelExtensions.comprehensions());
(options, version) -> CelExtensions.comprehensions())
;

@SuppressWarnings("ImmutableEnumChecker")
private final CompilerExtensionProvider compilerExtensionProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ private VariableDecl parseVariable(ParserContext<Node> ctx, Node node) {
case "name":
builder.setName(newString(ctx, valueNode));
break;
case "description":
builder.setDescription(newString(ctx, valueNode));
break;
case "type":
if (typeDeclBuilder != null) {
ctx.reportError(
Expand Down Expand Up @@ -318,6 +321,9 @@ private FunctionDecl parseFunction(ParserContext<Node> ctx, Node node) {
case "overloads":
builder.setOverloads(parseOverloads(ctx, valueNode));
break;
case "description":
// TODO: Set description
break;
default:
ctx.reportError(keyId, String.format("Unsupported function tag: %s", keyName));
break;
Expand Down Expand Up @@ -369,6 +375,9 @@ private static ImmutableSet<OverloadDecl> parseOverloads(ParserContext<Node> ctx
case "target":
overloadDeclBuilder.setTarget(parseTypeDecl(ctx, valueNode));
break;
case "examples":
// TODO: Set examples
break;
default:
ctx.reportError(keyId, String.format("Unsupported overload tag: %s", fieldName));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ public Node representData(Object data) {
if (!environment.description().isEmpty()) {
configMap.put("description", environment.description());
}
if (!environment.container().name().isEmpty()
|| !environment.container().abbreviations().isEmpty()
|| !environment.container().aliases().isEmpty()) {
configMap.put("container", environment.container());

if (environment.container().isPresent()) {
configMap.put("container", environment.container().get());
}
if (!environment.extensions().isEmpty()) {
configMap.put("extensions", environment.extensions().asList());
Expand Down
12 changes: 8 additions & 4 deletions policy/src/main/java/dev/cel/policy/CelPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -77,8 +78,7 @@ public abstract static class Builder {

public abstract Builder setPolicySource(CelPolicySource policySource);

// This should stay package-private to encourage add/set methods to be used instead.
abstract ImmutableMap.Builder<String, Object> metadataBuilder();
private final HashMap<String, Object> metadata = new HashMap<>();

public abstract Builder setMetadata(ImmutableMap<String, Object> value);

Expand All @@ -90,6 +90,10 @@ public List<Import> imports() {
return Collections.unmodifiableList(importList);
}

public Map<String, Object> metadata() {
return Collections.unmodifiableMap(metadata);
}

@CanIgnoreReturnValue
public Builder addImport(Import value) {
importList.add(value);
Expand All @@ -104,13 +108,13 @@ public Builder addImports(Collection<Import> values) {

@CanIgnoreReturnValue
public Builder putMetadata(String key, Object value) {
metadataBuilder().put(key, value);
metadata.put(key, value);
return this;
}

@CanIgnoreReturnValue
public Builder putMetadata(Map<String, Object> map) {
metadataBuilder().putAll(map);
metadata.putAll(map);
return this;
}

Expand Down
29 changes: 29 additions & 0 deletions policy/src/main/java/dev/cel/policy/testing/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@rules_java//java:defs.bzl", "java_library")

package(
default_applicable_licenses = ["//:license"],
default_visibility = [
"//policy/testing:__pkg__",
],
)

java_library(
name = "policy_test_suite_helper",
testonly = True,
srcs = [
"PolicyTestSuiteHelper.java",
],
deps = [
"//bundle:cel",
"//common:cel_ast",
"//common:compiler_common",
"//common/formats:value_string",
"//policy",
"//policy:parser",
"//policy:parser_builder",
"//policy:policy_parser_context",
"//runtime:evaluation_exception",
"@maven//:com_google_guava_guava",
"@maven//:org_yaml_snakeyaml",
],
)
Loading
Loading