Repository navigation
Expand file tree
/
Copy pathentity.cpp
More file actions
55 lines (53 loc) · 2.09 KB
/
Copy pathentity.cpp
File metadata and controls
55 lines (53 loc) · 2.09 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
#include <teaql/core/entity.hpp>
#include <teaql/core/detail/debug.hpp>
namespace teaql {
Record BaseEntityData::to_values_map(Memory r) const {
Record values(r);
values.emplace("id", Value(r, id));
values.emplace("version", Value(r, version));
// Dynamic fields deliberately win on collision, just as the Rust map insertion does.
for (const auto& [key, value] : dynamic_values) {
values.insert_or_assign(String(key, r), Value(r, value));
}
return values;
}
Result<BaseEntityData, EntityError> BaseEntityData::from_values_map(Memory r,
const Record& values) {
using Outcome = Result<BaseEntityData, EntityError>;
BaseEntityData data(std::allocator_arg, Allocator(r));
const auto invalid = [&](std::string_view field, const Value& value) {
String message("invalid ", r);
message += field;
message += " field: ";
message += "Some(";
message += detail::debug_value(r, value);
message += ")";
return Outcome::err(r,
EntityError(std::allocator_arg, Allocator(r), "BaseEntity", message));
};
if (auto it = values.find("id"); it != values.end()) {
const auto& v = it->second;
if (auto n = std::get_if<std::uint64_t>(&v.data)) {
data.id = *n;
} else if (auto n = std::get_if<std::int64_t>(&v.data); n && *n >= 0) {
data.id = std::uint64_t(*n);
} else if (!std::holds_alternative<std::monostate>(v.data)) {
return invalid("id", v);
}
}
if (auto it = values.find("version"); it != values.end()) {
const auto& v = it->second;
if (auto n = std::get_if<std::int64_t>(&v.data)) {
data.version = *n;
} else if (!std::holds_alternative<std::monostate>(v.data)) {
return invalid("version", v);
}
}
for (const auto& [key, value] : values) {
if (key != "id" && key != "version") {
data.dynamic_values.emplace(key, value);
}
}
return Outcome::ok(r, std::move(data));
}
}