Skip to content

Commit

Permalink
Add the capability to extend CEL environment from parsed config
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 646664981
  • Loading branch information
l46kok authored and copybara-github committed Jul 2, 2024
1 parent 99b52f5 commit 4e7ec14
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 9 deletions.
5 changes: 5 additions & 0 deletions bundle/src/main/java/dev/cel/bundle/CelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public CelValidationResult check(CelAbstractSyntaxTree ast) {
return compiler.get().check(ast);
}

@Override
public CelTypeProvider getTypeProvider() {
return compiler.get().getTypeProvider();
}

@Override
public CelRuntime.Program createProgram(CelAbstractSyntaxTree ast) throws CelEvaluationException {
return runtime.get().createProgram(ast);
Expand Down
4 changes: 4 additions & 0 deletions checker/src/main/java/dev/cel/checker/CelChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelValidationResult;
import dev.cel.common.types.CelTypeProvider;

/** Public interface for type-checking parsed CEL expressions. */
@Immutable
Expand All @@ -29,5 +30,8 @@ public interface CelChecker {
*/
CelValidationResult check(CelAbstractSyntaxTree ast);

/** Returns the underlying type provider. */
CelTypeProvider getTypeProvider();

CelCheckerBuilder toCheckerBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
@SuppressWarnings("Immutable")
private final TypeProvider typeProvider;

private final CelTypeProvider celTypeProvider;
private final boolean standardEnvironmentEnabled;

// Builder is mutable by design. APIs must make defensive copies in and out of this class.
Expand All @@ -98,6 +99,11 @@ public CelValidationResult check(CelAbstractSyntaxTree ast) {
return new CelValidationResult(checkedAst, ImmutableList.of());
}

@Override
public CelTypeProvider getTypeProvider() {
return this.celTypeProvider;
}

@Override
public CelCheckerBuilder toCheckerBuilder() {
return new Builder(checkerBuilder);
Expand Down Expand Up @@ -422,6 +428,7 @@ public CelCheckerLegacyImpl build() {
functionDeclarations.build(),
Optional.fromNullable(expectedResultType),
legacyProvider,
messageTypeProvider,
standardEnvironmentEnabled,
this);
}
Expand Down Expand Up @@ -469,6 +476,7 @@ private CelCheckerLegacyImpl(
ImmutableSet<CelFunctionDecl> functionDeclarations,
Optional<CelType> expectedResultType,
TypeProvider typeProvider,
CelTypeProvider celTypeProvider,
boolean standardEnvironmentEnabled,
Builder checkerBuilder) {
this.celOptions = celOptions;
Expand All @@ -477,6 +485,7 @@ private CelCheckerLegacyImpl(
this.functionDeclarations = functionDeclarations;
this.expectedResultType = expectedResultType;
this.typeProvider = typeProvider;
this.celTypeProvider = celTypeProvider;
this.standardEnvironmentEnabled = standardEnvironmentEnabled;
this.checkerBuilder = new Builder(checkerBuilder);
}
Expand Down
20 changes: 20 additions & 0 deletions common/src/main/java/dev/cel/common/types/SimpleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package dev.cel.common.types;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.Immutable;
import java.util.Optional;

/** Simple types represent scalar, dynamic, and error values. */
@AutoValue
Expand All @@ -42,6 +44,19 @@ public abstract class SimpleType extends CelType {
public static final CelType TIMESTAMP = create(CelKind.TIMESTAMP, "google.protobuf.Timestamp");
public static final CelType UINT = create(CelKind.UINT, "uint");

private static final ImmutableMap<String, CelType> TYPE_MAP =
ImmutableMap.of(
DYN.name(), DYN,
BOOL.name(), BOOL,
BYTES.name(), BYTES,
DOUBLE.name(), DOUBLE,
DURATION.name(), DURATION,
INT.name(), INT,
NULL_TYPE.name(), NULL_TYPE,
STRING.name(), STRING,
TIMESTAMP.name(), TIMESTAMP,
UINT.name(), UINT);

@Override
public abstract CelKind kind();

Expand All @@ -56,6 +71,11 @@ public boolean isAssignableFrom(CelType other) {
|| (other instanceof NullableType && other.isAssignableFrom(this));
}

/** Returns a matching SimpleType by its name if one exists. */
public static Optional<CelType> findByName(String typeName) {
return Optional.ofNullable(TYPE_MAP.get(typeName));
}

private static CelType create(CelKind kind, String name) {
return new AutoValue_SimpleType(kind, name);
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/main/java/dev/cel/compiler/CelCompilerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public CelValidationResult check(CelAbstractSyntaxTree ast) {
return checker.check(ast);
}

@Override
public CelTypeProvider getTypeProvider() {
return checker.getTypeProvider();
}

@Override
public void accept(EnvVisitor envVisitor) {
if (checker instanceof EnvVisitable) {
Expand Down
8 changes: 8 additions & 0 deletions policy/src/main/java/dev/cel/policy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ java_library(
deps = [
":required_fields_checker",
":source",
":validation_exception",
"//:auto_value",
"//bundle:cel",
"//common:compiler_common",
"//common:options",
"//common/types",
"//common/types:type_providers",
"//extensions",
"//extensions:optional_library",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
Expand Down
Loading

0 comments on commit 4e7ec14

Please sign in to comment.