Repository navigation
Expand file tree
/
Copy pathentity_key.cpp
More file actions
149 lines (144 loc) · 5.92 KB
/
Copy pathentity_key.cpp
File metadata and controls
149 lines (144 loc) · 5.92 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
#include <teaql/runtime/entity_key.hpp>
#include <teaql/runtime/entity_runtime_debug.hpp>
#include <bit>
#include <charconv>
#include <teaql/core/detail/debug.hpp>
namespace teaql {
EntityKey::EntityKey(std::allocator_arg_t, Allocator a, std::string_view name, const Value& value)
: entity(name, a.resource()), id(a.resource(), value), kind_(Kind::Null),
identity_(a.resource()) {
auto r = a.resource();
if (value.is_null()) {
return;
}
std::visit(
[&](const auto& v) {
using T = std::decay_t<decltype(v)>;
if constexpr (std::same_as<T, bool>) {
kind_ = Kind::Bool;
identity_ = Value(r, v);
} else if constexpr (std::same_as<T, std::int64_t>) {
if (v < 0) {
kind_ = Kind::I64;
identity_ = Value(r, v);
} else {
kind_ = Kind::U64;
identity_ = Value(r, std::uint64_t(v));
}
} else if constexpr (std::same_as<T, std::uint64_t>) {
kind_ = Kind::U64;
identity_ = Value(r, v);
} else if constexpr (std::same_as<T, double>) {
kind_ = Kind::F64;
identity_ = Value(r, std::bit_cast<std::uint64_t>(v));
} else if constexpr (std::same_as<T, Decimal>) {
kind_ = Kind::Decimal;
identity_ = Value(r, v);
} else if constexpr (std::same_as<T, String>) {
kind_ = Kind::Text;
identity_ = Value(r, v);
} else if constexpr (std::same_as<T, Date>) {
kind_ = Kind::Date;
identity_ = Value(r, v);
} else if constexpr (std::same_as<T, Timestamp>) {
kind_ = Kind::Timestamp;
identity_ = Value(r, v);
} else if constexpr (std::same_as<T, Json>) {
kind_ = Kind::Other;
String text("json:", r);
text += v.dump(r);
identity_ = Value(r, text);
} else if constexpr (std::same_as<T, Value::Object>) {
kind_ = Kind::Other;
identity_ = Value(r, "object");
} else if constexpr (std::same_as<T, Value::List>) {
kind_ = Kind::Other;
identity_ = Value(r, "list");
}
},
value.data);
}
EntityKey::EntityKey(std::allocator_arg_t, Allocator a, const EntityKey& other)
: entity(other.entity, a.resource()), id(a.resource(), other.id), kind_(other.kind_),
identity_(a.resource(), other.identity_) {}
std::strong_ordering EntityKey::operator<=>(const EntityKey& other) const {
if (auto order = entity <=> other.entity; order != 0) {
return order;
}
if (auto order = kind_ <=> other.kind_; order != 0) {
return order;
}
switch (kind_) {
case Kind::Null:
return std::strong_ordering::equal;
case Kind::Bool:
return std::get<bool>(identity_.data) <=> std::get<bool>(other.identity_.data);
case Kind::I64:
return std::get<std::int64_t>(identity_.data) <=>
std::get<std::int64_t>(other.identity_.data);
case Kind::U64:
case Kind::F64:
return std::get<std::uint64_t>(identity_.data) <=>
std::get<std::uint64_t>(other.identity_.data);
case Kind::Decimal:
return std::get<Decimal>(identity_.data) <=> std::get<Decimal>(other.identity_.data);
case Kind::Text:
case Kind::Other:
return std::get<String>(identity_.data) <=> std::get<String>(other.identity_.data);
case Kind::Date:
return std::get<Date>(identity_.data).epoch_days() <=>
std::get<Date>(other.identity_.data).epoch_days();
case Kind::Timestamp:
return std::get<Timestamp>(identity_.data).as_millis() <=>
std::get<Timestamp>(other.identity_.data).as_millis();
}
std::terminate();
}
}
namespace teaql {
void write_debug(DebugWriter& writer, const EntityKey& key) {
writer.structure("EntityKey", [&](auto& fields) {
fields.field("entity", [&](auto& out) {
out.text(key.entity);
});
fields.field("id", [&](auto& out) {
write_debug(out, key.id);
});
fields.field("id_key", [&](auto& out) {
if (key.kind_ == EntityKey::Kind::Null) {
out.raw("Null");
return;
}
constexpr std::string_view names[]{"Null",
"Bool",
"I64",
"U64",
"F64",
"Decimal",
"Text",
"Date",
"Timestamp",
"Other"};
out.tuple(names[static_cast<unsigned>(key.kind_)], [&](auto& inner) {
std::visit(
[&](const auto& value) {
using T = std::decay_t<decltype(value)>;
if constexpr (std::same_as<T, Timestamp>) {
inner.integer(value.as_millis());
} else if constexpr (std::same_as<T, bool> || std::integral<T> ||
std::same_as<T, String> || std::same_as<T, Decimal> ||
std::same_as<T, Date>) {
write_debug(inner, value);
} else {
throw std::logic_error("Invalid cached entity identity diagnostic");
}
},
key.identity_.data);
});
});
});
}
String EntityKey::debug(Memory r) const {
return debug_string(r, *this);
}
}