1818
1919import static com .google .common .base .Preconditions .checkNotNull ;
2020
21+ import com .github .xds .type .v3 .CelExpression ;
2122import dev .cel .common .CelAbstractSyntaxTree ;
23+ import dev .cel .common .CelProtoAbstractSyntaxTree ;
24+ import dev .cel .expr .CheckedExpr ;
2225import dev .cel .runtime .CelEvaluationException ;
26+ import io .grpc .xds .client .XdsResourceType .ResourceInvalidException ;
2327import java .util .function .Predicate ;
2428
2529/** Unified Matcher API: xds.type.matcher.v3.CelMatcher. */
@@ -42,6 +46,31 @@ public static CelMatcher create(CelAbstractSyntaxTree ast, String description)
4246 return new CelMatcher (ast , description );
4347 }
4448
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+
4574 public String description () {
4675 return description ;
4776 }
0 commit comments