forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.cc
More file actions
120 lines (100 loc) · 3.12 KB
/
Copy pathtype.cc
File metadata and controls
120 lines (100 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "common/type.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
namespace google {
namespace api {
namespace expr {
namespace common {
namespace {
constexpr const std::size_t kBasicTypeNamesSize = 10;
const auto* kBasicTypeNames = new std::array<std::string, kBasicTypeNamesSize>({
"null_type", // kNull
"bool", // kBool
"int", // kInt
"uint", // kUInt
"double", // kDouble
"string", // kString,
"bytes", // kBytes
"type", // kType
"map", // kMap
"list", // kList
});
static_assert(kBasicTypeNamesSize ==
static_cast<int>(BasicTypeValue::DO_NOT_USE),
"unexpected size");
static const std::map<absl::string_view, BasicType>* const kBasicTypeMap =
[]() {
auto result = new std::map<absl::string_view, BasicType>();
for (std::size_t i = 0; i < kBasicTypeNames->size(); ++i) {
result->emplace(kBasicTypeNames->at(i),
BasicType(static_cast<BasicTypeValue>(i)));
}
return result;
}();
struct ToStringVisitor {
template <typename T>
const std::string& operator()(const T& value) {
return value.ToString();
}
};
struct FullNameVisitor {
template <typename T>
absl::string_view operator()(const T& value) {
return value.full_name();
}
};
} // namespace
const std::string& BasicType::ToString() const {
return kBasicTypeNames->at(static_cast<std::size_t>(value_));
}
std::unique_ptr<google::protobuf::Message> ObjectType::Unpack(
const google::protobuf::Any& value) {
auto msg = absl::WrapUnique(
google::protobuf::MessageFactory::generated_factory()->GetPrototype(value_)->New());
if (!value.UnpackTo(msg.get())) {
return nullptr;
}
return msg;
}
UnrecognizedType::UnrecognizedType(absl::string_view full_name)
: string_rep_(absl::StrCat("type(\"", full_name, "\")")),
hash_code_(internal::Hash(full_name)) {
assert(google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(
std::string(full_name)) == nullptr);
}
absl::string_view UnrecognizedType::full_name() const {
return absl::string_view(string_rep_).substr(6, string_rep_.size() - 8);
}
Type::Type(const std::string& full_name)
: data_(BasicType(BasicTypeValue::kNull)) {
auto itr = kBasicTypeMap->find(full_name);
if (itr != kBasicTypeMap->end()) {
data_ = itr->second;
return;
}
auto obj_desc =
google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(
full_name);
if (obj_desc != nullptr) {
data_ = ObjectType(obj_desc);
return;
}
auto enum_desc =
google::protobuf::DescriptorPool::generated_pool()->FindEnumTypeByName(full_name);
if (enum_desc != nullptr) {
data_ = EnumType(enum_desc);
return;
}
auto value = UnrecognizedType(full_name);
data_ = value;
}
absl::string_view Type::full_name() const {
return absl::visit(FullNameVisitor(), data_);
}
const std::string& Type::ToString() const {
return absl::visit(ToStringVisitor(), data_);
}
} // namespace common
} // namespace expr
} // namespace api
} // namespace google