Repository navigation
Expand file tree
/
Copy pathentity_decoder_fixture.hpp
More file actions
95 lines (77 loc) · 3.17 KB
/
Copy pathentity_decoder_fixture.hpp
File metadata and controls
95 lines (77 loc) · 3.17 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
#pragma once
#include <teaql/runtime/entity_graph_decoder.hpp>
#include <teaql/runtime/behavior.hpp>
namespace decoder_fixture {
using namespace teaql;
// Handwritten model: the same model contract is used by the Rust oracle. No
// generator or entity-runtime algorithms are reproduced in this fixture.
template <unsigned Kind = 0, bool DynamicGuard = false>
struct Item : EntityDefaults<Item<Kind, DynamicGuard>> {
using allocator_type = Allocator;
static constexpr std::string_view ENTITY_NAME = "Item";
CompactRow row;
std::optional<EntityRuntimeState> state;
Item(std::allocator_arg_t, Allocator a, CompactRow value)
: row(std::allocator_arg, a, std::move(value)) {}
Item(std::allocator_arg_t, Allocator a, const Item& other)
: row(std::allocator_arg, a, other.row),
state(detail::clone_in(a.resource(), other.state)) {}
Item(std::allocator_arg_t, Allocator a, Item&& other)
: Item(detail::move_to(a.resource(), std::move(other))) {}
Item(const Item&) = delete;
Item(Item&&) noexcept = default;
Memory memory_resource() const {
return row.memory_resource();
}
static EntityDescriptor entity_descriptor(Memory r) {
EntityDescriptor e(std::allocator_arg, Allocator(r), Kind == 2 ? "Alias" : "Item");
e.table(Kind == 1 ? "alternate_items" : "items");
return e;
}
static Result<Item, EntityError> from_compact_row(Memory r, CompactRow row) {
using R = Result<Item, EntityError>;
if (auto* v = row.get("fail"); v && v->try_bool().value_or(false)) {
return R::err(r,
EntityError(std::allocator_arg,
Allocator(r),
ENTITY_NAME,
"handwritten model decode failed"));
}
return R::ok(r, Item(std::allocator_arg, Allocator(r), std::move(row)));
}
// Guard-only fixture: no dynamic provider is installed by this caller.
static constexpr bool supports_dynamic_field_load() {
return DynamicGuard;
}
Value id_value() const {
auto* id = row.get("id");
return id ? Value(memory_resource(), *id) : Value(memory_resource());
}
void on_loaded(AnyRef context) {
if (auto* root = context.get<EntityRuntimeState>()) {
state.emplace(std::allocator_arg, Allocator(memory_resource()), *root);
state->set_original_compact_row(ENTITY_NAME, row);
}
}
MutationValues into_values() && {
MutationValues values(memory_resource());
values.fields = row.into_map(memory_resource());
return values;
}
};
static_assert(IdentifiableEntity<Item<>>);
struct NamedBehavior : EntityDataServiceBehavior {
using allocator_type = Allocator;
String name;
NamedBehavior(std::allocator_arg_t, Allocator a, std::string_view name)
: name(name, a.resource()) {}
Memory memory_resource() const {
return name.get_allocator().resource();
}
Vector<String> relation_loads(Memory r, const UserContext&) const override {
Vector<String> out(r);
out.emplace_back(name);
return out;
}
};
}