Repository navigation
Expand file tree
/
Copy pathreference_model_probe.cpp
More file actions
176 lines (169 loc) · 7.65 KB
/
Copy pathreference_model_probe.cpp
File metadata and controls
176 lines (169 loc) · 7.65 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
#include <teaql/runtime/reference_model_debug.hpp>
#include <teaql/runtime/context.hpp>
#include "fixture_value.hpp"
#include <teaql/core/detail/model_json.hpp>
#include "debug_modes.hpp"
#include <iostream>
static UtcDateTime time_value(const Json& input) {
const auto& values = std::get<Json::Array>(input.data);
return UtcDateTime::from_timestamp(
number<std::int64_t>(values[0]),
detail::model_integer<std::uint32_t>(values[1], "nanoseconds"))
.value();
}
static ReferenceIdentity identity(Memory owner, const Json& input) {
return ReferenceIdentity(std::allocator_arg,
Allocator(owner),
text(field(input, "entity")),
number<std::uint64_t>(field(input, "id")),
number<std::int64_t>(field(input, "version")));
}
static ReferenceMode mode(const Json& input) {
return std::get<bool>(field(input, "raw").data) ? ReferenceMode::Raw : ReferenceMode::Governed;
}
template <class T>
static Json snapshot(Memory owner, const T& value) {
auto copy = detail::clone_in(owner, value);
Json::Object output(owner);
output.emplace("debug", formatted_debug(owner, value));
output.emplace("clone_debug", formatted_debug(owner, copy));
if constexpr (requires { value == copy; }) {
output.emplace("equal", Json(owner, value == copy));
}
return Json(owner, std::move(output));
}
template <class T>
static Json error_snapshot(Memory owner, const T& value) {
auto output = snapshot(owner, value);
auto& fields = std::get<Json::Object>(output.data);
fields.emplace("display", Json(owner, value.code()));
fields.emplace("source_none", Json(owner, value.source() == nullptr));
return output;
}
static Json evaluate(UserContext& user, const Json& input) {
auto owner = user.memory_resource();
auto kind = text(field(input, "kind"));
if (kind == "claims") {
auto value = EntityReferenceClaims(
std::allocator_arg,
Allocator(owner),
text(field(input, "entity")),
number<std::uint64_t>(field(input, "id")),
number<std::int64_t>(field(input, "version")),
time_value(field(input, "issued")),
time_value(field(input, "expires")),
text(field(input, "purpose")),
detail::model_integer<std::uint32_t>(field(input, "key_version"), "key_version"));
return snapshot(owner, value);
}
if (kind == "principal") {
auto value = TrustedReferencePrincipal(std::allocator_arg,
Allocator(owner),
text(field(input, "realm")),
text(field(input, "subject")),
text(field(input, "root")),
number<std::uint64_t>(field(input, "id")));
return snapshot(owner, value);
}
if (kind == "document") {
auto value = ReferenceDocumentScope(std::allocator_arg,
Allocator(owner),
text(field(input, "document")),
text(field(input, "purpose")),
text(field(input, "aggregate")),
number<std::uint64_t>(field(input, "id")),
number<std::int64_t>(field(input, "version")));
return snapshot(owner, value);
}
if (kind == "identity") {
return snapshot(owner, identity(owner, input));
}
if (kind == "external") {
if (mode(input) == ReferenceMode::Raw) {
return snapshot(owner,
ExternalEntityReference(
std::allocator_arg,
Allocator(owner),
RawEntityReference{number<std::uint64_t>(field(input, "id")),
number<std::int64_t>(field(input, "version"))}));
}
return snapshot(owner,
ExternalEntityReference(
std::allocator_arg, Allocator(owner), text(field(input, "token"))));
}
if (kind == "resolved") {
std::optional<std::string_view> key;
std::optional<UtcDateTime> issued, expires;
if (auto* value = std::get_if<String>(&field(input, "key").data)) {
key = *value;
}
if (!std::holds_alternative<std::monostate>(field(input, "issued").data)) {
issued = time_value(field(input, "issued"));
}
if (!std::holds_alternative<std::monostate>(field(input, "expires").data)) {
expires = time_value(field(input, "expires"));
}
return snapshot(owner,
ResolvedEntityReference(std::allocator_arg,
Allocator(owner),
identity(owner, input),
mode(input),
key,
issued,
expires));
}
if (kind == "profile") {
return snapshot(owner,
static_cast<DeploymentProfile>(
detail::model_integer<unsigned>(field(input, "variant"), "variant")));
}
if (kind == "mode") {
return snapshot(owner, mode(input));
}
if (kind == "wire") {
return snapshot(owner, ReferenceWireCodec{});
}
if (kind == "key") {
detail::CryptoKey bytes{};
bytes.fill(detail::model_integer<std::uint8_t>(field(input, "byte"), "byte"));
return snapshot(
owner,
ReferenceKey(std::allocator_arg, Allocator(owner), text(field(input, "key")), bytes));
}
if (kind == "notice") {
ReferenceStartupNotice value{
text(field(input, "level")),
text(field(input, "message")),
text(field(input, "profile")),
text(field(input, "telemetry_key")),
text(field(input, "telemetry_value")),
{{{"TeaQL-Reference-Mode", "raw-internal-id"}, {"Cache-Control", "no-store"}}}};
return snapshot(owner, value);
}
if (kind == "token_error") {
return error_snapshot(owner,
detail::model_integer<unsigned>(field(input, "variant"), "variant") ==
0
? EntityReferenceTokenError::invalid()
: EntityReferenceTokenError::codec_required());
}
const auto errors = std::array{RoundTripReferenceError::invalid(),
RoundTripReferenceError::expired(),
RoundTripReferenceError::scope_mismatch(),
RoundTripReferenceError::authorization_required(),
RoundTripReferenceError::configuration()};
return error_snapshot(
owner, errors.at(detail::model_integer<unsigned>(field(input, "variant"), "variant")));
}
int main() {
GlobalContext global;
std::string line;
while (std::getline(std::cin, line)) {
auto user = global.make_user_context();
auto input = Json::parse(user->memory_resource(), line);
if (!input) {
throw std::invalid_argument("Invalid reference model fixture");
}
std::cout << evaluate(*user, *input).dump(user->memory_resource()) << '\n';
}
}