Skip to content

Commit 23cd804

Browse files
CEL Dev Teamkyessenov
authored andcommitted
Add conversion utilities going from the proto type representations to native type representations of the AST.
PiperOrigin-RevId: 452047564
1 parent 21db34f commit 23cd804

5 files changed

Lines changed: 1443 additions & 17 deletions

File tree

base/BUILD

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,38 @@ cc_test(
240240
"@com_google_absl//absl/types:variant",
241241
],
242242
)
243+
244+
cc_library(
245+
name = "ast_utility",
246+
srcs = ["ast_utility.cc"],
247+
hdrs = ["ast_utility.h"],
248+
deps = [
249+
":ast",
250+
"@com_google_absl//absl/container:flat_hash_map",
251+
"@com_google_absl//absl/memory",
252+
"@com_google_absl//absl/status",
253+
"@com_google_absl//absl/status:statusor",
254+
"@com_google_absl//absl/time",
255+
"@com_google_googleapis//google/api/expr/v1alpha1:checked_cc_proto",
256+
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
257+
"@com_google_protobuf//:protobuf",
258+
],
259+
)
260+
261+
cc_test(
262+
name = "ast_utility_test",
263+
srcs = [
264+
"ast_utility_test.cc",
265+
],
266+
deps = [
267+
":ast",
268+
":ast_utility",
269+
"//internal:testing",
270+
"@com_google_absl//absl/status",
271+
"@com_google_absl//absl/time",
272+
"@com_google_absl//absl/types:variant",
273+
"@com_google_googleapis//google/api/expr/v1alpha1:checked_cc_proto",
274+
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
275+
"@com_google_protobuf//:protobuf",
276+
],
277+
)

base/ast.h

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,30 +200,22 @@ class CreateStruct {
200200
// Represents an entry.
201201
class Entry {
202202
public:
203+
using KeyKind = absl::variant<std::string, std::unique_ptr<Expr>>;
203204
Entry() {}
204-
Entry(int64_t id,
205-
absl::variant<std::string, std::unique_ptr<Expr>> key_kind,
206-
std::unique_ptr<Expr> value)
205+
Entry(int64_t id, KeyKind key_kind, std::unique_ptr<Expr> value)
207206
: id_(id), key_kind_(std::move(key_kind)), value_(std::move(value)) {}
208207

209208
void set_id(int64_t id) { id_ = id; }
210209

211-
void set_key_kind(
212-
absl::variant<std::string, std::unique_ptr<Expr>> key_kind) {
213-
key_kind_ = std::move(key_kind);
214-
}
210+
void set_key_kind(KeyKind key_kind) { key_kind_ = std::move(key_kind); }
215211

216212
void set_value(std::unique_ptr<Expr> value) { value_ = std::move(value); }
217213

218214
int64_t id() const { return id_; }
219215

220-
const absl::variant<std::string, std::unique_ptr<Expr>>& key_kind() const {
221-
return key_kind_;
222-
}
216+
const KeyKind& key_kind() const { return key_kind_; }
223217

224-
absl::variant<std::string, std::unique_ptr<Expr>>& mutable_key_kind() {
225-
return key_kind_;
226-
}
218+
KeyKind& mutable_key_kind() { return key_kind_; }
227219

228220
const Expr* value() const { return value_.get(); }
229221

@@ -240,7 +232,7 @@ class CreateStruct {
240232
// information and other attributes to the node.
241233
int64_t id_;
242234
// The `Entry` key kinds.
243-
absl::variant<std::string, std::unique_ptr<Expr>> key_kind_;
235+
KeyKind key_kind_;
244236
// Required. The value assigned to the key.
245237
std::unique_ptr<Expr> value_;
246238
};
@@ -759,6 +751,7 @@ class FunctionType {
759751
//
760752
// TODO(issues/5): decide on final naming for this.
761753
class AbstractType {
754+
public:
762755
AbstractType(std::string name, std::vector<Type> parameter_types)
763756
: name_(std::move(name)), parameter_types_(std::move(parameter_types)) {}
764757

@@ -791,7 +784,7 @@ class PrimitiveTypeWrapper {
791784

792785
const PrimitiveType& type() const { return type_; }
793786

794-
PrimitiveType& type() { return type_; }
787+
PrimitiveType& mutable_type() { return type_; }
795788

796789
private:
797790
PrimitiveType type_;
@@ -807,7 +800,7 @@ class MessageType {
807800

808801
void set_type(std::string type) { type_ = std::move(type); }
809802

810-
const std::string& type() { return type_; }
803+
const std::string& type() const { return type_; }
811804

812805
private:
813806
std::string type_;
@@ -824,7 +817,7 @@ class ParamType {
824817

825818
void set_type(std::string type) { type_ = std::move(type); }
826819

827-
const std::string& type() { return type_; }
820+
const std::string& type() const { return type_; }
828821

829822
private:
830823
std::string type_;

0 commit comments

Comments
 (0)