Skip to content

Commit ed21ee4

Browse files
TristonianJonescopybara-github
authored andcommitted
Create a proto_to_predicate compiler for converting proto messages
into CEL expressions PiperOrigin-RevId: 912808489
1 parent d301931 commit ed21ee4

9 files changed

Lines changed: 1253 additions & 7 deletions

File tree

common/expr_factory.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class MacroExprFactory;
3434
class ParserMacroExprFactory;
3535
class OptimizerExprFactory;
3636

37+
namespace tools {
38+
class ProtoToPredicateBuilder;
39+
}
40+
3741
class ExprFactory {
3842
protected:
3943
// `IsExprLike` determines whether `T` is some `Expr`. Currently that means
@@ -380,6 +384,7 @@ class ExprFactory {
380384
friend class MacroExprFactory;
381385
friend class ParserMacroExprFactory;
382386
friend class OptimizerExprFactory;
387+
friend class tools::ProtoToPredicateBuilder;
383388

384389
ExprFactory() : accu_var_(kAccumulatorVariableName) {}
385390

eval/compiler/flat_expr_builder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,7 @@ void BinaryCondVisitor::PreVisit(const cel::Expr* expr) {
21542154
case BinaryCond::kOr:
21552155
visitor_->ValidateOrError(
21562156
!expr->call_expr().has_target() &&
2157-
expr->call_expr().args().size() == 2,
2157+
expr->call_expr().args().size() >= 2,
21582158
"Invalid argument count for a binary function call.");
21592159
break;
21602160
case BinaryCond::kOptionalOr:

internal/json.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace cel::internal {
2727

2828
// Converts the given message to its `google.protobuf.Value` equivalent
29-
// representation. This is similar to `proto2::json::MessageToJsonString()`,
29+
// representation. This is similar to `google::protobuf::json::MessageToJsonString()`,
3030
// except that this results in structured serialization.
3131
absl::Status MessageToJson(
3232
const google::protobuf::Message& message,
@@ -45,7 +45,7 @@ absl::Status MessageToJson(
4545
google::protobuf::Message* absl_nonnull result);
4646

4747
// Converts the given message field to its `google.protobuf.Value` equivalent
48-
// representation. This is similar to `proto2::json::MessageToJsonString()`,
48+
// representation. This is similar to `google::protobuf::json::MessageToJsonString()`,
4949
// except that this results in structured serialization.
5050
absl::Status MessageFieldToJson(
5151
const google::protobuf::Message& message,

tools/BUILD

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,56 @@ cc_library(
204204
],
205205
)
206206

207+
cc_library(
208+
name = "proto_to_predicate",
209+
srcs = ["proto_to_predicate.cc"],
210+
hdrs = ["proto_to_predicate.h"],
211+
deps = [
212+
"//common:ast",
213+
"//common:expr",
214+
"//common:expr_factory",
215+
"//common:operators",
216+
"//internal:status_macros",
217+
"@com_google_absl//absl/log:absl_log",
218+
"@com_google_absl//absl/status",
219+
"@com_google_absl//absl/status:statusor",
220+
"@com_google_absl//absl/strings",
221+
"@com_google_absl//absl/types:span",
222+
"@com_google_protobuf//:protobuf",
223+
],
224+
)
225+
226+
cc_test(
227+
name = "proto_to_predicate_test",
228+
srcs = ["proto_to_predicate_test.cc"],
229+
deps = [
230+
":cel_unparser",
231+
":proto_to_predicate",
232+
"//common:ast",
233+
"//common:ast_proto",
234+
"//common:value",
235+
"//env:config",
236+
"//env:env_runtime",
237+
"//env:env_yaml",
238+
"//env:runtime_std_extensions",
239+
"//eval/testutil:test_message_cc_proto",
240+
"//extensions/protobuf:value",
241+
"//internal:status_macros",
242+
"//internal:testing",
243+
"//internal:testing_descriptor_pool",
244+
"//parser",
245+
"//runtime",
246+
"//runtime:activation",
247+
"//tools/testdata:test_policy_cc_proto",
248+
"@com_google_absl//absl/status",
249+
"@com_google_absl//absl/status:status_matchers",
250+
"@com_google_absl//absl/status:statusor",
251+
"@com_google_absl//absl/strings:string_view",
252+
"@com_google_absl//absl/types:span",
253+
"@com_google_protobuf//:protobuf",
254+
],
255+
)
256+
207257
cc_test(
208258
name = "descriptor_pool_builder_test",
209259
srcs = ["descriptor_pool_builder_test.cc"],

0 commit comments

Comments
 (0)