Repository navigation
Expand file tree
/
Copy pathchecker.cpp
More file actions
313 lines (301 loc) · 13 KB
/
Copy pathchecker.cpp
File metadata and controls
313 lines (301 loc) · 13 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <teaql/runtime/context.hpp>
#include <teaql/runtime/detail/graph_fix_scope.hpp>
#include <teaql/runtime/detail/checker_intent.hpp>
#include <teaql/runtime/detail/ledger_keys.hpp>
#include <teaql/runtime/detail/checker_rendering.hpp>
#include <algorithm>
namespace teaql {
namespace {
template <class Check>
RuntimeHookResult with_storage_values(const UserContext& context,
std::string_view name,
EntityValues& values,
Check&& check) {
auto result = [&] {
try {
return check();
} catch (...) {
if (const auto* descriptor = context.entity(name)) {
(void)detail::map_checker_values(*descriptor, values, false);
}
throw;
}
}();
if (const auto* descriptor = context.entity(name)) {
auto mapped = detail::map_checker_values(*descriptor, values, false);
if (!mapped) {
return mapped;
}
}
return result;
}
}
EntityDescriptor detail::context_entity_descriptor(const UserContext& user_context,
std::type_index type,
EntityDescriptorFactory factory) {
user_context.check_public();
if (!factory) {
throw std::invalid_argument("A model descriptor factory is required");
}
auto& arena = const_cast<detail::ContextArena&>(user_context.memory_);
detail::CheckerCallScope active(arena);
if (!user_context.model_descriptors_) {
user_context.model_descriptors_ = &arena.make_payload<detail::ModelDescriptorCache>();
}
return user_context.model_descriptors_->get(type, [&] {
return factory(user_context, user_context.memory_resource());
});
}
Arena& detail::checker_reference_origin(const UserContext& context, std::uint64_t id) {
return context.reference_arena(id, std::source_location::current());
}
Arena& detail::checker_call_arena(const UserContext& context) {
return detail::resource_arena(context.memory_resource());
}
bool UserContext::has_checker(std::string_view entity) const {
check_public();
detail::CheckerCallScope active(detail::resource_arena(memory_resource()));
return checker_registry_ &&
checker_registry_->get().checker(memory_resource(), entity).has_value();
}
RuntimeHookResult UserContext::check_and_fix_values(std::string_view entity,
EntityValues& values) const {
return check_and_fix_values_at(entity, values, ObjectLocation::root(memory_resource()));
}
RuntimeHookResult UserContext::check_and_fix_values_at(std::string_view entity,
EntityValues& values,
const ObjectLocation& location) const {
return check_and_fix_values_scoped(entity, values, location, std::nullopt);
}
RuntimeHookResult UserContext::check_and_fix_values_scoped(std::string_view name,
EntityValues& values,
const ObjectLocation& location,
std::optional<bool> changed) const {
check_public();
auto r = memory_resource();
detail::CheckerCallScope active(detail::resource_arena(r));
const auto* descriptor = entity(name);
if (descriptor) {
auto mapped = detail::map_checker_values(*descriptor, values, true);
if (!mapped) {
return mapped;
}
}
auto checker = checker_registry_ ? checker_registry_->get().checker(r, name)
: std::optional<CheckerBorrow>{};
std::optional<detail::CheckerCallScope> checker_owner;
if (checker) {
auto origin = checker->origin_memory_resource();
if (origin != r) {
checker_owner.emplace(detail::resource_arena(origin));
}
}
auto vr = values.memory_resource();
FieldSet automatic(vr);
if (checker) {
for (auto field : checker->get().managed_context_fields()) {
automatic.emplace(field);
}
for (auto field : checker->get().custom_context_fields()) {
automatic.emplace(field);
}
}
Record before(vr);
for (const auto& field : automatic) {
if (auto found = values.fields.find(field); found != values.fields.end()) {
before.emplace(String(field, vr), Value(vr, found->second));
}
}
auto restore = [&] {
for (const auto& field : automatic) {
auto found = before.find(field);
if (found != before.end()) {
values.fields.insert_or_assign(String(field, vr), Value(vr, found->second));
} else {
values.fields.erase(field);
}
}
};
checker_messages::detail::OriginActivation origin(*this, name, &location);
auto run = [&]() -> RuntimeHookResult {
auto status = check_object_status(values);
CheckResults results(r);
if (checker) {
detail::with_fix_time(*this, [&] {
if (changed) {
checker->get().check_and_fix_for_mutation(
*this, values, location, results, *changed);
} else {
checker->get().check_and_fix(*this, values, location, results);
}
});
for (auto& result : results) {
std::string_view owner =
result.entity_type ? std::string_view(*result.entity_type) : std::string_view{};
detail::trim(owner);
if (owner.empty()) {
result.with_entity_type(checker->get().entity());
}
}
}
if (const auto* current = entity(name)) {
auto mapped = detail::map_checker_values(*current, values, true);
if (!mapped) {
return mapped;
}
}
collect_required_property_results(name, values, status, location, results);
collect_boolean_property_results(name, values.mutation_values(), location, results);
collect_numeric_property_results(name, values.mutation_values(), location, results);
if (results.empty()) {
return RuntimeHookResult::ok(r, {});
}
translate_check_results_with_values(results, name, values);
return RuntimeHookResult::err(r, RuntimeError::check(r, std::move(results)));
};
try {
auto result = run();
if (!result) {
restore();
}
return result;
} catch (...) {
restore();
throw;
}
}
RuntimeHookResult
detail::ContextCheckerAccess::check_storage_values(const UserContext& context,
std::string_view name,
EntityValues& values,
const ObjectLocation& location) {
return with_storage_values(context, name, values, [&] {
return context.check_and_fix_values_at(name, values, location);
});
}
RuntimeHookResult
detail::ContextCheckerAccess::check_storage_mutation(const UserContext& context,
std::string_view name,
EntityValues& values,
const ObjectLocation& location,
CheckMutationState intent) {
context.check_public();
auto r = context.memory_resource();
CheckerCallScope active(resource_arena(r));
auto descriptor = context.require_entity(name);
if (!descriptor) {
return RuntimeHookResult::err(r, std::move(descriptor).error());
}
const auto& model = descriptor.value().get();
auto checker = context.checker_registry_ ? context.checker_registry_->get().checker(r, name)
: std::optional<CheckerBorrow>{};
std::optional<CheckerCallScope> checker_owner;
if (checker && checker->origin_memory_resource() != r) {
checker_owner.emplace(resource_arena(checker->origin_memory_resource()));
}
auto managed =
checker ? checker->get().managed_context_fields() : std::span<const std::string_view>{};
auto custom =
checker ? checker->get().custom_context_fields() : std::span<const std::string_view>{};
auto changed = intent.has_effective_change(values, model, managed, custom);
auto vr = values.memory_resource();
FieldSet fields(vr);
for (auto field : managed) {
fields.emplace(field);
}
for (auto field : custom) {
fields.emplace(field);
}
auto automatic = storage_fields_from_ledger(vr, model, fields);
if (!automatic) {
return RuntimeHookResult::err(r, std::move(automatic).error());
}
Record before(vr);
for (const auto& field : automatic.value()) {
if (auto found = values.fields.find(field); found != values.fields.end()) {
before.emplace(String(field, vr), Value(vr, found->second));
}
}
auto create = check_object_status(values) == CheckObjectStatus::Create;
auto result = with_storage_values(context, name, values, [&] {
return context.check_and_fix_values_scoped(name, values, location, changed);
});
// Customer hooks may explicitly destroy or replace a metadata provider.
// Revalidate its checked owner before inspecting the final descriptor.
auto final_descriptor = context.require_entity(name);
if (!final_descriptor) {
return RuntimeHookResult::err(r, std::move(final_descriptor).error());
}
// Fix may repair the requested edit or introduce a different business edit.
// An absent original is insufficient evidence to cancel unknown input.
if (result && !create && intent.original &&
!CheckMutationState{intent.original, nullptr, intent.dynamic_changed}.has_effective_change(
values, final_descriptor.value().get(), managed, custom)) {
for (const auto& field : automatic.value()) {
auto found = before.find(field);
if (found != before.end()) {
values.fields.insert_or_assign(String(field, vr), Value(vr, found->second));
} else {
values.fields.erase(field);
}
}
}
return result;
}
FieldSet detail::ContextCheckerAccess::automatic_fields(const UserContext& context,
std::string_view name) {
context.check_public();
auto resource = context.memory_resource();
CheckerCallScope active(resource_arena(resource));
auto checker = context.checker_registry_
? context.checker_registry_->get().checker(resource, name)
: std::optional<CheckerBorrow>{};
FieldSet fields(resource);
if (checker) {
std::optional<CheckerCallScope> owner;
if (checker->origin_memory_resource() != resource) {
owner.emplace(resource_arena(checker->origin_memory_resource()));
}
for (auto field : checker->get().managed_context_fields()) {
fields.emplace(field);
}
for (auto field : checker->get().custom_context_fields()) {
fields.emplace(field);
}
}
return fields;
}
void UserContext::collect_required_property_results(std::string_view name,
const EntityValues& values,
CheckObjectStatus status,
const ObjectLocation& location,
CheckResults& results) const {
const auto* descriptor = entity(name);
if (!descriptor) {
return;
}
auto r = results.get_allocator().resource();
for (const auto& property : descriptor->properties) {
if (property.nullable || property.is_version) {
continue;
}
auto mapped = detail::canonical_field_from_storage(r, *descriptor, property.name);
auto field = mapped ? std::string_view(mapped.value()) : std::string_view(property.name);
auto found = values.fields.find(field);
if (found == values.fields.end() && field != property.name) {
found = values.fields.find(property.name);
}
bool missing = found == values.fields.end();
bool null = !missing && std::holds_alternative<std::monostate>(found->second.data);
auto path = detail::clone_in(r, location);
path.member(field);
bool reported = std::any_of(results.begin(), results.end(), [&](const CheckResult& result) {
return result.rule == CheckRule::Required && result.location == path;
});
if (((status == CheckObjectStatus::Create && missing) || null) && !reported) {
results.emplace_back(CheckResult::required(r, std::move(path)));
results.back().with_entity_type(name);
}
}
}
}