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