From c342d15ef5cc9edfc8bdd408ab512c1eca70b554 Mon Sep 17 00:00:00 2001 From: Sokwhan Huh Date: Wed, 4 Dec 2024 18:44:54 -0800 Subject: [PATCH] Migrate CelAttributeParser away from proto based expr to canonical cel expr PiperOrigin-RevId: 702937113 --- .../src/main/java/dev/cel/runtime/BUILD.bazel | 3 +- .../dev/cel/runtime/CelAttributeParser.java | 62 +++++++++---------- .../cel/runtime/CelAttributeParserTest.java | 6 +- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/runtime/src/main/java/dev/cel/runtime/BUILD.bazel b/runtime/src/main/java/dev/cel/runtime/BUILD.bazel index 4ec25799..93e8fe8e 100644 --- a/runtime/src/main/java/dev/cel/runtime/BUILD.bazel +++ b/runtime/src/main/java/dev/cel/runtime/BUILD.bazel @@ -213,11 +213,10 @@ java_library( ":unknown_attributes", "//common", "//common:compiler_common", - "//common:proto_ast", + "//common/ast", "//parser", "//parser:operator", "//parser:parser_builder", - "@cel_spec//proto/cel/expr:expr_java_proto", "@maven//:com_google_guava_guava", ], ) diff --git a/runtime/src/main/java/dev/cel/runtime/CelAttributeParser.java b/runtime/src/main/java/dev/cel/runtime/CelAttributeParser.java index 4ad4722d..a14d8bfd 100644 --- a/runtime/src/main/java/dev/cel/runtime/CelAttributeParser.java +++ b/runtime/src/main/java/dev/cel/runtime/CelAttributeParser.java @@ -16,15 +16,15 @@ import static com.google.common.collect.ImmutableList.toImmutableList; -import dev.cel.expr.Constant; -import dev.cel.expr.Expr; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; -import com.google.common.primitives.UnsignedLong; import dev.cel.common.CelAbstractSyntaxTree; -import dev.cel.common.CelProtoAbstractSyntaxTree; import dev.cel.common.CelValidationException; import dev.cel.common.CelValidationResult; +import dev.cel.common.ast.CelConstant; +import dev.cel.common.ast.CelExpr; +import dev.cel.common.ast.CelExpr.CelCall; +import dev.cel.common.ast.CelExpr.ExprKind.Kind; import dev.cel.parser.CelParser; import dev.cel.parser.CelParserFactory; import dev.cel.parser.Operator; @@ -78,19 +78,18 @@ private static String unescape(String s) { return b.toString(); } - private static CelAttribute.Qualifier parseConst(Constant constExpr) { - switch (constExpr.getConstantKindCase()) { - case BOOL_VALUE: - return CelAttribute.Qualifier.ofBool(constExpr.getBoolValue()); + private static CelAttribute.Qualifier parseConst(CelConstant constExpr) { + switch (constExpr.getKind()) { + case BOOLEAN_VALUE: + return CelAttribute.Qualifier.ofBool(constExpr.booleanValue()); case INT64_VALUE: - return CelAttribute.Qualifier.ofInt(constExpr.getInt64Value()); + return CelAttribute.Qualifier.ofInt(constExpr.int64Value()); case UINT64_VALUE: - return CelAttribute.Qualifier.ofUint(UnsignedLong.fromLongBits(constExpr.getUint64Value())); + return CelAttribute.Qualifier.ofUint(constExpr.uint64Value()); case STRING_VALUE: - return CelAttribute.Qualifier.ofString(unescape(constExpr.getStringValue())); + return CelAttribute.Qualifier.ofString(unescape(constExpr.stringValue())); default: - throw new IllegalArgumentException( - "Unsupported const expr kind: " + constExpr.getConstantKindCase()); + throw new IllegalArgumentException("Unsupported const expr kind: " + constExpr.getKind()); } } @@ -111,35 +110,34 @@ public static CelAttributePattern parsePattern(String attribute) { try { CelAbstractSyntaxTree ast = result.getAst(); ArrayDeque qualifiers = new ArrayDeque<>(); - // TODO: Traverse using CelExpr - Expr node = CelProtoAbstractSyntaxTree.fromCelAst(ast).getExpr(); + CelExpr node = ast.getExpr(); while (node != null) { - switch (node.getExprKindCase()) { - case IDENT_EXPR: - qualifiers.addFirst(CelAttribute.Qualifier.ofString(node.getIdentExpr().getName())); + switch (node.getKind()) { + case IDENT: + qualifiers.addFirst(CelAttribute.Qualifier.ofString(node.ident().name())); node = null; break; - case CALL_EXPR: - Expr.Call callExpr = node.getCallExpr(); - if (!callExpr.getFunction().equals(Operator.INDEX.getFunction()) - || callExpr.getArgsCount() != 2 - || !callExpr.getArgs(1).hasConstExpr()) { + case CALL: + CelCall callExpr = node.call(); + if (!callExpr.function().equals(Operator.INDEX.getFunction()) + || callExpr.args().size() != 2 + || !callExpr.args().get(1).getKind().equals(Kind.CONSTANT)) { throw new IllegalArgumentException( String.format( "Unsupported call expr: %s(%s)", - callExpr.getFunction(), + callExpr.function(), Joiner.on(", ") .join( - callExpr.getArgsList().stream() - .map(Expr::getExprKindCase) + callExpr.args().stream() + .map(CelExpr::getKind) .collect(toImmutableList())))); } - qualifiers.addFirst(parseConst(callExpr.getArgs(1).getConstExpr())); - node = callExpr.getArgs(0); + qualifiers.addFirst(parseConst(callExpr.args().get(1).constant())); + node = callExpr.args().get(0); break; - case SELECT_EXPR: - String field = node.getSelectExpr().getField(); - node = node.getSelectExpr().getOperand(); + case SELECT: + String field = node.select().field(); + node = node.select().operand(); if (field.equals("_" + WILDCARD_ESCAPE)) { qualifiers.addFirst(CelAttribute.Qualifier.ofWildCard()); break; @@ -148,7 +146,7 @@ public static CelAttributePattern parsePattern(String attribute) { break; default: throw new IllegalArgumentException( - "Unsupported expr kind in attribute: " + node.getExprKindCase()); + "Unsupported expr kind in attribute: " + node.exprKind()); } } return CelAttributePattern.create(ImmutableList.copyOf(qualifiers)); diff --git a/runtime/src/test/java/dev/cel/runtime/CelAttributeParserTest.java b/runtime/src/test/java/dev/cel/runtime/CelAttributeParserTest.java index 58e5f273..e2d46355 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelAttributeParserTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelAttributeParserTest.java @@ -101,19 +101,19 @@ public void parse_unsupportedExprKindThrows() { Assert.assertThrows( IllegalArgumentException.class, () -> CelAttributeParser.parse("1 / 2")); - assertThat(iae).hasMessageThat().contains("_/_(CONST_EXPR, CONST_EXPR)"); + assertThat(iae).hasMessageThat().contains("_/_(CONSTANT, CONSTANT)"); iae = Assert.assertThrows( IllegalArgumentException.class, () -> CelAttributeParser.parse("123.field")); - assertThat(iae).hasMessageThat().contains("CONST_EXPR"); + assertThat(iae).hasMessageThat().contains("CelConstant"); iae = Assert.assertThrows( IllegalArgumentException.class, () -> CelAttributeParser.parse("a && b")); - assertThat(iae).hasMessageThat().contains("_&&_(IDENT_EXPR, IDENT_EXPR)"); + assertThat(iae).hasMessageThat().contains("_&&_(IDENT, IDENT)"); } @Test