Repository navigation
Expand file tree
/
Copy pathentity.cpp
More file actions
104 lines (99 loc) · 4.07 KB
/
Copy pathentity.cpp
File metadata and controls
104 lines (99 loc) · 4.07 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
#include <teaql/core/entity.hpp>
#include <teaql/core/detail/debug.hpp>
#include <limits>
namespace teaql {
Result<const RuntimeSubtypeIdentity*, EntityError>
decode_runtime_subtype(Memory resource,
const CompactRow& row,
std::string_view owner,
std::span<const RuntimeSubtypeIdentity* const> identities) {
using Output = Result<const RuntimeSubtypeIdentity*, EntityError>;
const auto* value = row.get("_type_");
if (!value || std::holds_alternative<std::monostate>(value->data)) {
return Output::ok(resource, nullptr);
}
if (const auto* name = std::get_if<String>(&value->data)) {
for (const auto* identity : identities) {
if (identity && identity->for_view(owner) == std::string_view(*name)) {
return Output::ok(resource, identity);
}
}
}
return Output::err(resource,
EntityError(std::allocator_arg,
Allocator(resource),
owner,
"invalid or ambiguous subtype metadata for typed view"));
}
void EntityJsonTraversal::reject_dynamic_property_collision() noexcept {
failure_ = EntityJsonTraversalError::DynamicPropertyCollision;
}
bool EntityJsonTraversal::enter(std::string_view entity, std::uintptr_t address) {
if (visited_ != std::numeric_limits<std::size_t>::max()) {
++visited_;
}
const bool cycle = std::ranges::any_of(path_, [&](const auto& entry) {
return entry.first == entity && entry.second == address;
});
if (strict_limits_) {
const auto [max_depth, max_nodes] = *strict_limits_;
if (path_.size() >= max_depth || visited_ > max_nodes) {
failure_ = EntityJsonTraversalError::Limit;
} else if (cycle) {
failure_ = EntityJsonTraversalError::Cycle;
}
}
const bool expand = path_.size() < 128 && !cycle && !failure_;
path_.emplace_back(String(entity, memory_resource()), address);
return expand;
}
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));
}
}