Repository navigation
Expand file tree
/
Copy pathvalue.cpp
More file actions
199 lines (184 loc) · 5.95 KB
/
Copy pathvalue.cpp
File metadata and controls
199 lines (184 loc) · 5.95 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <teaql/core/value.hpp>
#include <teaql/memory/scoped.hpp>
namespace teaql {
Value::Value(Memory r, String&& text) : Value(r) {
if (text.get_allocator().resource() == r) {
data.emplace<String>(std::move(text));
} else {
data.emplace<String>(text, r);
}
}
Value::Value(Memory r, List&& values) : Value(r) {
if (values.get_allocator().resource() == r) {
data.emplace<List>(std::move(values));
} else {
Value copy(r, std::as_const(values));
data = std::move(copy.data);
}
}
Value::Value(Memory r, Object&& values) : Value(r) {
if (values.get_allocator().resource() == r) {
data.emplace<Object>(std::move(values));
} else {
data.emplace<Object>(clone_record(r, values));
}
}
Value::Value(Memory r, const List& values) : Value(r) {
List copy(r);
copy.reserve(values.size());
for (const auto& value : values) {
copy.emplace_back(value);
}
data = std::move(copy);
}
Value::Value(Memory r, const Object& values) : Value(r) {
data = clone_record(r, values);
}
Value::Value(Memory r, const Value& value) : Value(r) {
std::visit(
[&](const auto& item) {
using T = std::decay_t<decltype(item)>;
if constexpr (std::same_as<T, String>) {
data = String(item, r);
} else if constexpr (std::same_as<T, Json>) {
data = Json(r, item);
} else if constexpr (std::same_as<T, List>) {
List copy(r);
copy.reserve(item.size());
for (const auto& nested : item) {
copy.emplace_back(nested);
}
data = std::move(copy);
} else if constexpr (std::same_as<T, Object>) {
data = clone_record(r, item);
} else {
data = item;
}
},
value.data);
}
Value::Value(Memory r, Value&& other) : Value(r) {
if (r == other.memory_resource()) {
data = std::move(other.data);
} else {
Value copy(r, static_cast<const Value&>(other));
data = std::move(copy.data);
}
}
Value& Value::operator=(Value&& other) {
return detail::assign_scoped(*this, std::move(other));
}
std::optional<double> Value::try_f64() const {
if (const auto* v = std::get_if<double>(&data)) {
return *v;
}
if (const auto* v = std::get_if<std::int64_t>(&data)) {
return static_cast<double>(*v);
}
if (const auto* v = std::get_if<std::uint64_t>(&data)) {
return static_cast<double>(*v);
}
if (const auto* v = std::get_if<Decimal>(&data)) {
return v->to_f64();
}
return {};
}
std::optional<bool> Value::try_bool() const {
if (const auto* v = std::get_if<bool>(&data)) {
return *v;
}
return {};
}
std::optional<std::string_view> Value::try_text() const {
if (const auto* v = std::get_if<String>(&data)) {
return *v;
}
return {};
}
std::optional<Decimal> Value::try_decimal() const {
if (const auto* v = std::get_if<Decimal>(&data)) {
return *v;
}
if (const auto* v = std::get_if<std::int64_t>(&data)) {
return Decimal::from_i64(*v);
}
if (const auto* v = std::get_if<std::uint64_t>(&data)) {
return Decimal::from_u64(*v);
}
if (const auto* v = std::get_if<double>(&data)) {
return Decimal::from_f64(*v);
}
if (const auto* v = std::get_if<String>(&data)) {
return Decimal::parse(*v);
}
return {};
}
std::optional<Date> Value::try_date() const {
if (const auto* v = std::get_if<Date>(&data)) {
return *v;
}
if (const auto* v = std::get_if<String>(&data)) {
return Date::parse(*v);
}
// Timestamp and Decimal are deliberately not accepted by Rust's try_date.
std::optional<std::int64_t> millis;
if (const auto* v = std::get_if<std::int64_t>(&data)) {
millis = *v;
}
if (const auto* v = std::get_if<std::uint64_t>(&data); v && *v <= std::uint64_t(INT64_MAX)) {
millis = std::int64_t(*v);
}
if (!millis) {
return {};
}
return Date::from_epoch_days(*millis / 86400000 - (*millis % 86400000 < 0));
}
std::optional<Timestamp> Value::try_timestamp() const {
if (const auto* v = std::get_if<Timestamp>(&data)) {
return *v;
}
if (const auto* v = std::get_if<String>(&data)) {
return Timestamp::parse(*v);
}
if (const auto* v = std::get_if<std::int64_t>(&data)) {
return Timestamp(*v);
}
if (const auto* v = std::get_if<std::uint64_t>(&data); v && *v <= std::uint64_t(INT64_MAX)) {
return Timestamp(std::int64_t(*v));
}
return {};
}
Json Value::to_json_value(Memory r) const {
return std::visit(
[&](const auto& v) -> Json {
using T = std::decay_t<decltype(v)>;
if constexpr (std::same_as<T, std::monostate> || std::same_as<T, TypedNull>) {
return Json(r);
} else if constexpr (std::same_as<T, Decimal> || std::same_as<T, Date>) {
return Json(r, v.to_string(r));
} else if constexpr (std::same_as<T, Timestamp>) {
return Json(r, v.as_millis());
} else if constexpr (std::same_as<T, List>) {
Json result(r);
Json::Array array(r);
array.reserve(v.size());
for (const auto& item : v) {
array.emplace_back(item.to_json_value(r));
}
result.data = std::move(array);
return result;
} else if constexpr (std::same_as<T, Object>) {
Json result(r);
Json::Object object(r);
for (const auto& [key, item] : v) {
object.emplace(key, item.to_json_value(r));
}
result.data = std::move(object);
return result;
} else {
return Json(r, v);
}
},
data);
}
}