Repository navigation
Expand file tree
/
Copy pathchecker.cpp
More file actions
193 lines (177 loc) · 6.99 KB
/
Copy pathchecker.cpp
File metadata and controls
193 lines (177 loc) · 6.99 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
#include <teaql/runtime/checker.hpp>
#include <teaql/runtime/detail/checker_intent.hpp>
#include <teaql/runtime/entity_runtime.hpp>
#include <algorithm>
namespace teaql {
std::string_view check_object_status_name(CheckObjectStatus status) {
constexpr std::array<std::string_view, 3> names{"create", "update", "unknown"};
return names.at(std::size_t(status));
}
CheckObjectStatus check_object_status(const EntityValues& values) {
if (const auto* explicit_status = values.get(check_object_status_field)) {
if (auto* name = std::get_if<String>(&explicit_status->data)) {
if (*name == "create") {
return CheckObjectStatus::Create;
}
if (*name == "update") {
return CheckObjectStatus::Update;
}
}
}
return detail::missing_or_untyped_null(values.mutation_values(), "id")
? CheckObjectStatus::Create
: CheckObjectStatus::Update;
}
Value check_object_status_value(Memory r, CheckObjectStatus status) {
return Value(r, check_object_status_name(status));
}
void mark_entity_status(EntityValues& values, CheckObjectStatus status) {
auto r = values.memory_resource();
values.insert_named(check_object_status_field, check_object_status_value(r, status));
}
void clear_entity_status(EntityValues& values) {
(void)values.remove(check_object_status_field);
}
namespace detail {
bool checker_business_field(Memory resource,
const EntityDescriptor& descriptor,
std::string_view field,
std::span<const std::string_view> managed,
std::span<const std::string_view> custom) {
auto name = canonical_field_from_storage(resource, descriptor, field);
auto canonical = name ? std::string_view(name.value()) : field;
return !canonical.starts_with('_') && canonical != "id" && canonical != "version" &&
std::ranges::find(managed, canonical) == managed.end() &&
std::ranges::find(custom, canonical) == custom.end();
}
bool CheckMutationState::has_effective_change(const EntityValues& values,
const EntityDescriptor& descriptor,
std::span<const std::string_view> managed,
std::span<const std::string_view> custom) const {
if (dynamic_changed) {
return true;
}
auto changed = [&](std::string_view field) {
if (!checker_business_field(values.memory_resource(), descriptor, field, managed, custom)) {
return false;
}
if (!original) {
return true;
}
auto prior = original->fields.find(field);
const auto* next = values.get(field);
if (prior == original->fields.end()) {
return next != nullptr;
}
return !next || prior->second != *next;
};
if (dirty) {
return std::ranges::any_of(*dirty, changed);
}
for (const auto& [field, value] : values.named_entries()) {
if (changed(field)) {
return true;
}
}
return false;
}
std::optional<MutationValues>
checker_native_intent(Memory resource, std::optional<AnyRef> carrier, const EntityKey& key) {
if (carrier) {
if (const auto* state = carrier->get<EntityRuntimeState>()) {
auto values = EntityRuntimeAccess::native_mutation_values_for(*state, key);
if (!values || values->memory_resource() == resource) {
return values;
}
return clone_in(resource, values);
}
}
return {};
}
void checker_mapping_failure(const RuntimeError& error,
const ObjectLocation& location,
CheckResults& results) {
results.emplace_back(CheckRule::InvalidType, location);
results.back().with_message(error.to_string(results.get_allocator().resource()));
}
bool missing_or_untyped_null(const Record& values, std::string_view name) {
auto found = values.find(name);
return found == values.end() || std::holds_alternative<std::monostate>(found->second.data);
}
bool missing_or_untyped_null(const MutationValues& values, std::string_view name) {
const auto* found = values.get(name);
return !found || std::holds_alternative<std::monostate>(found->data);
}
std::optional<FieldSet> take_checker_fields(EntityValues& values, std::string_view name) {
auto removed = values.remove(name);
if (!removed) {
return {};
}
std::optional<FieldSet> out;
if (auto* list = std::get_if<Value::List>(&removed->data)) {
out.emplace(values.memory_resource());
for (const auto& field : *list) {
if (auto* text = std::get_if<String>(&field.data)) {
out->emplace(*text);
}
}
}
return out;
}
}
void Checker::required(const EntityValues& values,
std::string_view field,
const ObjectLocation& location,
CheckResults& results) const {
if (detail::missing_or_untyped_null(values.mutation_values(), field)) {
auto r = results.get_allocator().resource();
auto path = detail::clone_in(r, location);
path.member(field);
results.emplace_back(CheckResult::required(r, std::move(path)));
}
}
namespace {
void string_length(const EntityValues& values,
std::string_view field,
std::size_t limit,
const ObjectLocation& location,
CheckResults& results,
bool minimum) {
const auto* found = values.get(field);
if (!found) {
return;
}
auto* text = std::get_if<String>(&found->data);
if (!text) {
return;
}
std::size_t length = 0;
for (unsigned char byte : *text) {
if ((byte & 0xc0) != 0x80) {
++length;
}
}
if (minimum ? length < limit : length > limit) {
auto r = results.get_allocator().resource();
auto path = detail::clone_in(r, location);
path.member(field);
results.emplace_back(minimum ? CheckResult::min_str(r, std::move(path), limit, *text)
: CheckResult::max_str(r, std::move(path), limit, *text));
}
}
}
void Checker::min_string_length(const EntityValues& values,
std::string_view field,
std::size_t limit,
const ObjectLocation& location,
CheckResults& results) const {
string_length(values, field, limit, location, results, true);
}
void Checker::max_string_length(const EntityValues& values,
std::string_view field,
std::size_t limit,
const ObjectLocation& location,
CheckResults& results) const {
string_length(values, field, limit, location, results, false);
}
}