1616
1717import static com .google .common .collect .ImmutableList .toImmutableList ;
1818
19- import dev .cel .expr .Constant ;
20- import dev .cel .expr .Expr ;
2119import com .google .common .base .Joiner ;
2220import com .google .common .collect .ImmutableList ;
23- import com .google .common .primitives .UnsignedLong ;
2421import dev .cel .common .CelAbstractSyntaxTree ;
25- import dev .cel .common .CelProtoAbstractSyntaxTree ;
2622import dev .cel .common .CelValidationException ;
2723import 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 ;
2828import dev .cel .parser .CelParser ;
2929import dev .cel .parser .CelParserFactory ;
3030import 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 ));
0 commit comments