Skip to content

Commit

Permalink
Implement bidirectional conversion between cel::Expr and `google::a…
Browse files Browse the repository at this point in the history
…pi::expr::Expr`

PiperOrigin-RevId: 627733291
  • Loading branch information
jcking authored and copybara-github committed Apr 26, 2024
1 parent 4b77f6e commit 786244e
Show file tree
Hide file tree
Showing 5 changed files with 880 additions and 0 deletions.
24 changes: 24 additions & 0 deletions common/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ class CallExpr final {

void set_args(absl::Span<Expr> args);

Expr& add_args() ABSL_ATTRIBUTE_LIFETIME_BOUND;

ABSL_MUST_USE_RESULT std::vector<Expr> release_args();

friend void swap(CallExpr& lhs, CallExpr& rhs) noexcept {
Expand Down Expand Up @@ -386,6 +388,8 @@ class ListExpr final {

void set_elements(absl::Span<ListExprElement> elements);

ListExprElement& add_elements() ABSL_ATTRIBUTE_LIFETIME_BOUND;

ABSL_MUST_USE_RESULT std::vector<ListExprElement> release_elements();

friend void swap(ListExpr& lhs, ListExpr& rhs) noexcept {
Expand Down Expand Up @@ -452,6 +456,8 @@ class StructExpr final {

void set_fields(absl::Span<StructExprField> fields);

StructExprField& add_fields() ABSL_ATTRIBUTE_LIFETIME_BOUND;

ABSL_MUST_USE_RESULT std::vector<StructExprField> release_fields();

friend void swap(StructExpr& lhs, StructExpr& rhs) noexcept {
Expand Down Expand Up @@ -510,6 +516,8 @@ class MapExpr final {

void set_entries(absl::Span<MapExprEntry> entries);

MapExprEntry& add_entries() ABSL_ATTRIBUTE_LIFETIME_BOUND;

ABSL_MUST_USE_RESULT std::vector<MapExprEntry> release_entries();

friend void swap(MapExpr& lhs, MapExpr& rhs) noexcept {
Expand Down Expand Up @@ -1036,6 +1044,10 @@ inline void CallExpr::set_args(absl::Span<Expr> args) {
}
}

inline Expr& CallExpr::add_args() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return mutable_args().emplace_back();
}

inline std::vector<Expr> CallExpr::release_args() {
std::vector<Expr> args;
args.swap(args_);
Expand Down Expand Up @@ -1228,6 +1240,10 @@ inline void ListExpr::set_elements(absl::Span<ListExprElement> elements) {
}
}

inline ListExprElement& ListExpr::add_elements() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return mutable_elements().emplace_back();
}

inline std::vector<ListExprElement> ListExpr::release_elements() {
std::vector<ListExprElement> elements;
elements.swap(elements_);
Expand Down Expand Up @@ -1342,6 +1358,10 @@ inline void StructExpr::set_fields(absl::Span<StructExprField> fields) {
}
}

inline StructExprField& StructExpr::add_fields() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return mutable_fields().emplace_back();
}

inline std::vector<StructExprField> StructExpr::release_fields() {
std::vector<StructExprField> fields;
fields.swap(fields_);
Expand Down Expand Up @@ -1463,6 +1483,10 @@ inline void MapExpr::set_entries(absl::Span<MapExprEntry> entries) {
}
}

inline MapExprEntry& MapExpr::add_entries() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return mutable_entries().emplace_back();
}

inline std::vector<MapExprEntry> MapExpr::release_entries() {
std::vector<MapExprEntry> entries;
entries.swap(entries_);
Expand Down
33 changes: 33 additions & 0 deletions extensions/protobuf/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,39 @@ cc_test(
],
)

cc_library(
name = "ast",
srcs = ["ast.cc"],
hdrs = ["ast.h"],
deps = [
"//common:ast",
"//common:constant",
"//internal:proto_time_encoding",
"//internal:status_macros",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/functional:overload",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:variant",
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
"@com_google_protobuf//:protobuf",
],
)

cc_test(
name = "ast_test",
srcs = ["ast_test.cc"],
deps = [
":ast",
"//common:ast",
"//internal:proto_matchers",
"//internal:testing",
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "duration_lite",
srcs = ["duration_lite.cc"],
Expand Down
Loading

0 comments on commit 786244e

Please # to comment.