Skip to content

Commit 3b60bdc

Browse files
committed
Add CelMatcher.fromEnvoyProto - just to import dev.cel.expr
1 parent 401da42 commit 3b60bdc

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java

+29
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21+
import com.github.xds.type.v3.CelExpression;
2122
import dev.cel.common.CelAbstractSyntaxTree;
23+
import dev.cel.common.CelProtoAbstractSyntaxTree;
24+
import dev.cel.expr.CheckedExpr;
2225
import dev.cel.runtime.CelEvaluationException;
26+
import io.grpc.xds.client.XdsResourceType.ResourceInvalidException;
2327
import 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

Comments
 (0)