18
18
19
19
import static com .google .common .base .Preconditions .checkNotNull ;
20
20
21
+ import com .github .xds .type .v3 .CelExpression ;
21
22
import dev .cel .common .CelAbstractSyntaxTree ;
23
+ import dev .cel .common .CelProtoAbstractSyntaxTree ;
24
+ import dev .cel .expr .CheckedExpr ;
22
25
import dev .cel .runtime .CelEvaluationException ;
26
+ import io .grpc .xds .client .XdsResourceType .ResourceInvalidException ;
23
27
import java .util .function .Predicate ;
24
28
25
29
/** Unified Matcher API: xds.type.matcher.v3.CelMatcher. */
@@ -42,6 +46,31 @@ public static CelMatcher create(CelAbstractSyntaxTree ast, String description)
42
46
return new CelMatcher (ast , description );
43
47
}
44
48
49
+ public static CelMatcher fromEnvoyProto (com .github .xds .type .matcher .v3 .CelMatcher proto )
50
+ throws ResourceInvalidException {
51
+ CelExpression exprMatch = proto .getExprMatch ();
52
+ // TODO(sergiitk): do i need this?
53
+ // checkNotNull(exprMatch);
54
+
55
+ if (!exprMatch .hasCelExprChecked ()) {
56
+ throw new ResourceInvalidException ("RateLimitQuotaFilterConfig domain is required" );
57
+ }
58
+
59
+ // Canonical CEL.
60
+ CheckedExpr celExprChecked = exprMatch .getCelExprChecked ();
61
+
62
+ // TODO(sergiitk): catch tree build errors?
63
+ CelAbstractSyntaxTree ast = CelProtoAbstractSyntaxTree .fromCheckedExpr (celExprChecked ).getAst ();
64
+
65
+ try {
66
+ return new CelMatcher (ast , proto .getDescription ());
67
+ } catch (CelEvaluationException e ) {
68
+ throw ResourceInvalidException .ofResource (exprMatch ,
69
+ "Error Building CEL Program cel_expr_checked: " + e .getErrorCode () + " "
70
+ + e .getMessage ());
71
+ }
72
+ }
73
+
45
74
public String description () {
46
75
return description ;
47
76
}
0 commit comments