Skip to content

Commit b5448e1

Browse files
l46kokcopybara-github
authored andcommitted
Migrate CelAttributeParser away from proto based expr to canonical cel expr
PiperOrigin-RevId: 704823334
1 parent e0d99d2 commit b5448e1

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

runtime/src/main/java/dev/cel/runtime/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,10 @@ java_library(
213213
":unknown_attributes",
214214
"//common",
215215
"//common:compiler_common",
216-
"//common:proto_ast",
216+
"//common/ast",
217217
"//parser",
218218
"//parser:operator",
219219
"//parser:parser_builder",
220-
"@cel_spec//proto/cel/expr:expr_java_proto",
221220
"@maven//:com_google_guava_guava",
222221
],
223222
)

runtime/src/main/java/dev/cel/runtime/CelAttributeParser.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
import static com.google.common.collect.ImmutableList.toImmutableList;
1818

19-
import dev.cel.expr.Constant;
20-
import dev.cel.expr.Expr;
2119
import com.google.common.base.Joiner;
2220
import com.google.common.collect.ImmutableList;
23-
import com.google.common.primitives.UnsignedLong;
2421
import dev.cel.common.CelAbstractSyntaxTree;
25-
import dev.cel.common.CelProtoAbstractSyntaxTree;
2622
import dev.cel.common.CelValidationException;
2723
import dev.cel.common.CelValidationResult;
24+
import dev.cel.common.ast.CelConstant;
25+
import dev.cel.common.ast.CelExpr;
26+
import dev.cel.common.ast.CelExpr.CelCall;
27+
import dev.cel.common.ast.CelExpr.ExprKind.Kind;
2828
import dev.cel.parser.CelParser;
2929
import dev.cel.parser.CelParserFactory;
3030
import dev.cel.parser.Operator;
@@ -78,19 +78,18 @@ private static String unescape(String s) {
7878
return b.toString();
7979
}
8080

81-
private static CelAttribute.Qualifier parseConst(Constant constExpr) {
82-
switch (constExpr.getConstantKindCase()) {
83-
case BOOL_VALUE:
84-
return CelAttribute.Qualifier.ofBool(constExpr.getBoolValue());
81+
private static CelAttribute.Qualifier parseConst(CelConstant constExpr) {
82+
switch (constExpr.getKind()) {
83+
case BOOLEAN_VALUE:
84+
return CelAttribute.Qualifier.ofBool(constExpr.booleanValue());
8585
case INT64_VALUE:
86-
return CelAttribute.Qualifier.ofInt(constExpr.getInt64Value());
86+
return CelAttribute.Qualifier.ofInt(constExpr.int64Value());
8787
case UINT64_VALUE:
88-
return CelAttribute.Qualifier.ofUint(UnsignedLong.fromLongBits(constExpr.getUint64Value()));
88+
return CelAttribute.Qualifier.ofUint(constExpr.uint64Value());
8989
case STRING_VALUE:
90-
return CelAttribute.Qualifier.ofString(unescape(constExpr.getStringValue()));
90+
return CelAttribute.Qualifier.ofString(unescape(constExpr.stringValue()));
9191
default:
92-
throw new IllegalArgumentException(
93-
"Unsupported const expr kind: " + constExpr.getConstantKindCase());
92+
throw new IllegalArgumentException("Unsupported const expr kind: " + constExpr.getKind());
9493
}
9594
}
9695

@@ -111,35 +110,34 @@ public static CelAttributePattern parsePattern(String attribute) {
111110
try {
112111
CelAbstractSyntaxTree ast = result.getAst();
113112
ArrayDeque<CelAttribute.Qualifier> qualifiers = new ArrayDeque<>();
114-
// TODO: Traverse using CelExpr
115-
Expr node = CelProtoAbstractSyntaxTree.fromCelAst(ast).getExpr();
113+
CelExpr node = ast.getExpr();
116114
while (node != null) {
117-
switch (node.getExprKindCase()) {
118-
case IDENT_EXPR:
119-
qualifiers.addFirst(CelAttribute.Qualifier.ofString(node.getIdentExpr().getName()));
115+
switch (node.getKind()) {
116+
case IDENT:
117+
qualifiers.addFirst(CelAttribute.Qualifier.ofString(node.ident().name()));
120118
node = null;
121119
break;
122-
case CALL_EXPR:
123-
Expr.Call callExpr = node.getCallExpr();
124-
if (!callExpr.getFunction().equals(Operator.INDEX.getFunction())
125-
|| callExpr.getArgsCount() != 2
126-
|| !callExpr.getArgs(1).hasConstExpr()) {
120+
case CALL:
121+
CelCall callExpr = node.call();
122+
if (!callExpr.function().equals(Operator.INDEX.getFunction())
123+
|| callExpr.args().size() != 2
124+
|| !callExpr.args().get(1).getKind().equals(Kind.CONSTANT)) {
127125
throw new IllegalArgumentException(
128126
String.format(
129127
"Unsupported call expr: %s(%s)",
130-
callExpr.getFunction(),
128+
callExpr.function(),
131129
Joiner.on(", ")
132130
.join(
133-
callExpr.getArgsList().stream()
134-
.map(Expr::getExprKindCase)
131+
callExpr.args().stream()
132+
.map(CelExpr::getKind)
135133
.collect(toImmutableList()))));
136134
}
137-
qualifiers.addFirst(parseConst(callExpr.getArgs(1).getConstExpr()));
138-
node = callExpr.getArgs(0);
135+
qualifiers.addFirst(parseConst(callExpr.args().get(1).constant()));
136+
node = callExpr.args().get(0);
139137
break;
140-
case SELECT_EXPR:
141-
String field = node.getSelectExpr().getField();
142-
node = node.getSelectExpr().getOperand();
138+
case SELECT:
139+
String field = node.select().field();
140+
node = node.select().operand();
143141
if (field.equals("_" + WILDCARD_ESCAPE)) {
144142
qualifiers.addFirst(CelAttribute.Qualifier.ofWildCard());
145143
break;
@@ -148,7 +146,7 @@ public static CelAttributePattern parsePattern(String attribute) {
148146
break;
149147
default:
150148
throw new IllegalArgumentException(
151-
"Unsupported expr kind in attribute: " + node.getExprKindCase());
149+
"Unsupported expr kind in attribute: " + node.exprKind());
152150
}
153151
}
154152
return CelAttributePattern.create(ImmutableList.copyOf(qualifiers));

runtime/src/test/java/dev/cel/runtime/CelAttributeParserTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,19 @@ public void parse_unsupportedExprKindThrows() {
101101
Assert.assertThrows(
102102
IllegalArgumentException.class, () -> CelAttributeParser.parse("1 / 2"));
103103

104-
assertThat(iae).hasMessageThat().contains("_/_(CONST_EXPR, CONST_EXPR)");
104+
assertThat(iae).hasMessageThat().contains("_/_(CONSTANT, CONSTANT)");
105105

106106
iae =
107107
Assert.assertThrows(
108108
IllegalArgumentException.class, () -> CelAttributeParser.parse("123.field"));
109109

110-
assertThat(iae).hasMessageThat().contains("CONST_EXPR");
110+
assertThat(iae).hasMessageThat().contains("CelConstant");
111111

112112
iae =
113113
Assert.assertThrows(
114114
IllegalArgumentException.class, () -> CelAttributeParser.parse("a && b"));
115115

116-
assertThat(iae).hasMessageThat().contains("_&&_(IDENT_EXPR, IDENT_EXPR)");
116+
assertThat(iae).hasMessageThat().contains("_&&_(IDENT, IDENT)");
117117
}
118118

119119
@Test

0 commit comments

Comments
 (0)