Skip to content

Commit

Permalink
Create CelProtoTypes utility class to handle CelExpr in proto format
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 703247372
  • Loading branch information
l46kok authored and copybara-github committed Dec 5, 2024
1 parent bee355b commit 25e5c7a
Show file tree
Hide file tree
Showing 16 changed files with 729 additions and 309 deletions.
2 changes: 1 addition & 1 deletion bundle/src/test/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ java_library(
"//common/resources/testdata/proto3:standalone_global_enum_java_proto",
"//common/testing",
"//common/types",
"//common/types:cel_types",
"//common/types:cel_proto_types",
"//common/types:message_type_provider",
"//common/types:type_providers",
"//compiler",
Expand Down
71 changes: 37 additions & 34 deletions bundle/src/test/java/dev/cel/bundle/CelImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
import dev.cel.common.ast.CelExpr.CelList;
import dev.cel.common.testing.RepeatedTestProvider;
import dev.cel.common.types.CelKind;
import dev.cel.common.types.CelProtoTypes;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypes;
import dev.cel.common.types.EnumType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
Expand Down Expand Up @@ -157,10 +157,10 @@ public final class CelImplTest {
private static final CheckedExpr CHECKED_EXPR =
CheckedExpr.newBuilder()
.setExpr(EXPR)
.putTypeMap(1L, CelTypes.BOOL)
.putTypeMap(2L, CelTypes.BOOL)
.putTypeMap(3L, CelTypes.BOOL)
.putTypeMap(4L, CelTypes.BOOL)
.putTypeMap(1L, CelProtoTypes.BOOL)
.putTypeMap(2L, CelProtoTypes.BOOL)
.putTypeMap(3L, CelProtoTypes.BOOL)
.putTypeMap(4L, CelProtoTypes.BOOL)
.putReferenceMap(2L, Reference.newBuilder().addOverloadId("logical_and").build())
.putReferenceMap(3L, Reference.newBuilder().addOverloadId("logical_not").build())
.build();
Expand Down Expand Up @@ -194,7 +194,7 @@ public void build_badFileDescriptorSet() {
.addFile(AttributeContext.getDescriptor().getFile().toProto())
.build())
.setProtoResultType(
CelTypes.createMessage("google.rpc.context.AttributeContext.Resource"))
CelProtoTypes.createMessage("google.rpc.context.AttributeContext.Resource"))
.build());
assertThat(e).hasMessageThat().contains("file descriptor set with unresolved proto file");
}
Expand All @@ -220,7 +220,7 @@ public void check() throws Exception {
public void compile(boolean useProtoResultType) throws Exception {
CelBuilder celBuilder = standardCelBuilderWithMacros();
if (useProtoResultType) {
celBuilder.setProtoResultType(CelTypes.BOOL);
celBuilder.setProtoResultType(CelProtoTypes.BOOL);
} else {
celBuilder.setResultType(SimpleType.BOOL);
}
Expand All @@ -234,7 +234,7 @@ public void compile(boolean useProtoResultType) throws Exception {
public void compile_resultTypeCheckFailure(boolean useProtoResultType) {
CelBuilder celBuilder = standardCelBuilderWithMacros();
if (useProtoResultType) {
celBuilder.setProtoResultType(CelTypes.STRING);
celBuilder.setProtoResultType(CelProtoTypes.STRING);
} else {
celBuilder.setResultType(SimpleType.STRING);
}
Expand All @@ -258,7 +258,7 @@ public void compile_combinedTypeProvider() {
.addProtoTypeMasks(
ImmutableList.of(ProtoTypeMask.ofAllFields("google.rpc.context.AttributeContext")))
.addVar("condition", StructTypeReference.create("google.type.Expr"))
.setProtoResultType(CelTypes.BOOL)
.setProtoResultType(CelProtoTypes.BOOL)
.build();
CelValidationResult result =
cel.compile("type.Expr{expression: \"'hello'\"}.expression == condition.expression");
Expand Down Expand Up @@ -290,7 +290,8 @@ public void compile_customTypesWithAliasingCombinedProviders() throws Exception
// However, the first type resolution from the alias to the qualified type name won't be
// sufficient as future checks will expect the resolved alias to also be a type.
TypeProvider customTypeProvider =
aliasingProvider(ImmutableMap.of("Condition", CelTypes.createMessage("google.type.Expr")));
aliasingProvider(
ImmutableMap.of("Condition", CelProtoTypes.createMessage("google.type.Expr")));

// The registration of the aliasing TypeProvider and the google.type.Expr descriptor
// ensures that once the alias is resolved, the additional details about the Expr type
Expand Down Expand Up @@ -322,9 +323,9 @@ public void compile_customTypesWithAliasingSelfContainedProvider() throws Except
aliasingProvider(
ImmutableMap.of(
"Condition",
CelTypes.createMessage("google.type.Expr"),
CelProtoTypes.createMessage("google.type.Expr"),
"google.type.Expr",
CelTypes.createMessage("google.type.Expr")));
CelProtoTypes.createMessage("google.type.Expr")));

// The registration of the aliasing TypeProvider and the google.type.Expr descriptor
// ensures that once the alias is resolved, the additional details about the Expr type
Expand Down Expand Up @@ -500,12 +501,14 @@ public void compile_overlappingVarsFailure() {
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(IdentDecl.newBuilder().setType(CelTypes.STRING))
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.STRING))
.build())
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(IdentDecl.newBuilder().setType(CelTypes.createList(CelTypes.STRING)))
.setIdent(
IdentDecl.newBuilder()
.setType(CelProtoTypes.createList(CelProtoTypes.STRING)))
.build())
.setResultType(SimpleType.BOOL)
.build();
Expand All @@ -529,7 +532,7 @@ public void program_withVars() throws Exception {
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(IdentDecl.newBuilder().setType(CelTypes.STRING))
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.STRING))
.build())
.setResultType(SimpleType.BOOL)
.build();
Expand All @@ -545,7 +548,7 @@ public void program_withCelValue() throws Exception {
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(IdentDecl.newBuilder().setType(CelTypes.STRING))
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.STRING))
.build())
.setResultType(SimpleType.BOOL)
.build();
Expand Down Expand Up @@ -655,11 +658,11 @@ public void program_withFunctions() throws Exception {
ImmutableList.of(
Decl.newBuilder()
.setName("one")
.setIdent(IdentDecl.newBuilder().setType(CelTypes.BOOL))
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.BOOL))
.build(),
Decl.newBuilder()
.setName("two")
.setIdent(IdentDecl.newBuilder().setType(CelTypes.BOOL))
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.BOOL))
.build(),
Decl.newBuilder()
.setName("any")
Expand All @@ -668,21 +671,21 @@ public void program_withFunctions() throws Exception {
.addOverloads(
Overload.newBuilder()
.setOverloadId("any_bool")
.addParams(CelTypes.BOOL)
.setResultType(CelTypes.BOOL))
.addParams(CelProtoTypes.BOOL)
.setResultType(CelProtoTypes.BOOL))
.addOverloads(
Overload.newBuilder()
.setOverloadId("any_bool_bool")
.addParams(CelTypes.BOOL)
.addParams(CelTypes.BOOL)
.setResultType(CelTypes.BOOL))
.addParams(CelProtoTypes.BOOL)
.addParams(CelProtoTypes.BOOL)
.setResultType(CelProtoTypes.BOOL))
.addOverloads(
Overload.newBuilder()
.setOverloadId("any_bool_bool_bool")
.addParams(CelTypes.BOOL)
.addParams(CelTypes.BOOL)
.addParams(CelTypes.BOOL)
.setResultType(CelTypes.BOOL)))
.addParams(CelProtoTypes.BOOL)
.addParams(CelProtoTypes.BOOL)
.addParams(CelProtoTypes.BOOL)
.setResultType(CelProtoTypes.BOOL)))
.build()))
.addFunctionBindings(CelFunctionBinding.from("any_bool", Boolean.class, (arg) -> arg))
.addFunctionBindings(
Expand Down Expand Up @@ -716,7 +719,7 @@ public void program_withThrowingFunction() throws Exception {
.addOverloads(
Overload.newBuilder()
.setOverloadId("throws")
.setResultType(CelTypes.BOOL)))
.setResultType(CelProtoTypes.BOOL)))
.build())
.addFunctionBindings(
CelFunctionBinding.from(
Expand Down Expand Up @@ -744,7 +747,7 @@ public void program_withThrowingFunctionShortcircuited() throws Exception {
.addOverloads(
Overload.newBuilder()
.setOverloadId("throws")
.setResultType(CelTypes.BOOL)))
.setResultType(CelProtoTypes.BOOL)))
.build())
.addFunctionBindings(
CelFunctionBinding.from(
Expand Down Expand Up @@ -960,7 +963,7 @@ public void program_protoActivation() throws Exception {
.setIdent(
IdentDecl.newBuilder()
.setType(
CelTypes.createMessage(
CelProtoTypes.createMessage(
"google.rpc.context.AttributeContext.Resource")))
.build())
.setResultType(SimpleType.STRING)
Expand Down Expand Up @@ -1107,7 +1110,7 @@ public void program_customVarResolver() throws Exception {
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(IdentDecl.newBuilder().setType(CelTypes.STRING))
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.STRING))
.build())
.setResultType(SimpleType.BOOL)
.build();
Expand Down Expand Up @@ -1150,7 +1153,7 @@ public void program_messageTypeAddedAsVarWithoutDescriptor_throwsHumanReadableEr
String packageName = CheckedExpr.getDescriptor().getFile().getPackage();
Cel cel =
standardCelBuilderWithMacros()
.addVar("parsedExprVar", CelTypes.createMessage(ParsedExpr.getDescriptor()))
.addVar("parsedExprVar", CelProtoTypes.createMessage(ParsedExpr.getDescriptor()))
.build();
CelValidationException exception =
assertThrows(
Expand Down Expand Up @@ -2056,7 +2059,7 @@ private static TypeProvider aliasingProvider(ImmutableMap<String, Type> typeAlia
public @Nullable Type lookupType(String typeName) {
Type alias = typeAliases.get(typeName);
if (alias != null) {
return CelTypes.create(alias);
return CelProtoTypes.create(alias);
}
return null;
}
Expand All @@ -2069,7 +2072,7 @@ private static TypeProvider aliasingProvider(ImmutableMap<String, Type> typeAlia
@Override
public TypeProvider.@Nullable FieldType lookupFieldType(Type type, String fieldName) {
if (typeAliases.containsKey(type.getMessageType())) {
return TypeProvider.FieldType.of(CelTypes.STRING);
return TypeProvider.FieldType.of(CelProtoTypes.STRING);
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion checker/src/main/java/dev/cel/checker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ java_library(
"//common/internal:env_visitor",
"//common/internal:errors",
"//common/types",
"//common/types:cel_types",
"//common/types:cel_proto_types",
"//common/types:message_type_provider",
"//common/types:type_providers",
"@cel_spec//proto/cel/expr:expr_java_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.internal.Errors;
import dev.cel.common.types.CelProtoTypes;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.CelTypes;
import dev.cel.common.types.ProtoMessageTypeProvider;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -199,7 +199,7 @@ public CelCheckerBuilder addDeclarations(Iterable<Decl> declarations) {
CelIdentDecl.Builder identBuilder =
CelIdentDecl.newBuilder()
.setName(decl.getName())
.setType(CelTypes.typeToCelType(decl.getIdent().getType()))
.setType(CelProtoTypes.typeToCelType(decl.getIdent().getType()))
// Note: Setting doc and constant value exists for compatibility reason. This
// should not be set by the users.
.setDoc(decl.getIdent().getDoc());
Expand Down Expand Up @@ -278,7 +278,7 @@ public CelCheckerBuilder setResultType(CelType resultType) {
@Override
public CelCheckerBuilder setProtoResultType(Type resultType) {
checkNotNull(resultType);
return setResultType(CelTypes.typeToCelType(resultType));
return setResultType(CelProtoTypes.typeToCelType(resultType));
}

@Override
Expand Down
1 change: 1 addition & 0 deletions checker/src/test/java/dev/cel/checker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ java_library(
"//common/internal:errors",
"//common/resources/testdata/proto3:standalone_global_enum_java_proto",
"//common/types",
"//common/types:cel_proto_types",
"//common/types:cel_types",
"//common/types:json",
"//common/types:message_type_provider",
Expand Down
Loading

0 comments on commit 25e5c7a

Please sign in to comment.