Repository navigation
Expand file tree
/
Copy pathcheck_result.cpp
More file actions
140 lines (132 loc) · 4.99 KB
/
Copy pathcheck_result.cpp
File metadata and controls
140 lines (132 loc) · 4.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
#include <teaql/runtime/check_result.hpp>
#include <teaql/core/detail/model_json.hpp>
namespace teaql {
namespace {
template <class T>
void nullable(Json& out, std::string_view key, const std::optional<T>& value) {
if (value) {
detail::model_put(out, key, *value);
} else {
detail::model_put(out, key, Json(out.memory_resource()));
}
}
}
std::string_view wire_id(CheckRule rule) {
switch (rule) {
case CheckRule::Required:
return "required";
case CheckRule::InvalidType:
return "invalid_type";
case CheckRule::Min:
return "min";
case CheckRule::Max:
return "max";
case CheckRule::MinStringLength:
return "min_string_length";
case CheckRule::MaxStringLength:
return "max_string_length";
case CheckRule::ContextRootMissing:
return "context_root_missing";
case CheckRule::ContextRootMismatch:
return "context_root_mismatch";
case CheckRule::InvalidDeletionState:
return "invalid_deletion_state";
case CheckRule::InvalidVersionState:
return "invalid_version_state";
case CheckRule::VersionExhausted:
return "version_exhausted";
}
throw std::logic_error("Invalid check rule");
}
std::string_view debug_name(CheckRule rule) {
switch (rule) {
case CheckRule::Required:
return "Required";
case CheckRule::InvalidType:
return "InvalidType";
case CheckRule::Min:
return "Min";
case CheckRule::Max:
return "Max";
case CheckRule::MinStringLength:
return "MinStringLength";
case CheckRule::MaxStringLength:
return "MaxStringLength";
case CheckRule::ContextRootMissing:
return "ContextRootMissing";
case CheckRule::ContextRootMismatch:
return "ContextRootMismatch";
case CheckRule::InvalidDeletionState:
return "InvalidDeletionState";
case CheckRule::InvalidVersionState:
return "InvalidVersionState";
case CheckRule::VersionExhausted:
return "VersionExhausted";
}
throw std::logic_error("Invalid check rule");
}
CheckResult::CheckResult(std::allocator_arg_t, Allocator a, const CheckResult& other)
: rule(other.rule), location(std::allocator_arg, a, other.location),
input_value(detail::clone_in(a.resource(), other.input_value)),
system_value(detail::clone_in(a.resource(), other.system_value)),
message(detail::clone_in(a.resource(), other.message)),
entity_type(detail::clone_in(a.resource(), other.entity_type)),
source_instance_path(detail::clone_in(a.resource(), other.source_instance_path)),
presentation_(
checker_messages::detail::clone_rendered_state(a.resource(), other.presentation_)) {}
String CheckResult::to_string(Memory r) const {
if (message) {
return String(*message, r);
}
auto out = location.to_string(r);
out += ": ";
out += debug_name(rule);
return out;
}
WireCheckResult CheckResult::to_wire(Memory r, JsonFieldNamingProfile profile) const {
WireCheckResult out(std::allocator_arg, Allocator(r));
out.rule_id = wire_id(rule);
out.entity_type = detail::clone_in(r, entity_type);
out.source_instance_path = detail::clone_in(r, source_instance_path);
out.message = detail::clone_in(r, message);
for (const auto& segment : location.segments()) {
if (auto* name = std::get_if<String>(&segment.data)) {
out.location.emplace_back(std::string_view(*name));
} else {
out.location.emplace_back(std::get<std::size_t>(segment.data));
}
}
out.instance_path = location.instance_path_with(r, profile);
if (input_value) {
out.input_value = input_value->to_json_value(r);
}
if (system_value) {
out.system_value = system_value->to_json_value(r);
}
return out;
}
WireCheckResult::WireCheckResult(std::allocator_arg_t, Allocator a, const WireCheckResult& other)
: rule_id(other.rule_id, a.resource()),
entity_type(detail::clone_in(a.resource(), other.entity_type)),
location(other.location, a.resource()), instance_path(other.instance_path, a.resource()),
source_instance_path(detail::clone_in(a.resource(), other.source_instance_path)),
input_value(detail::clone_in(a.resource(), other.input_value)),
system_value(detail::clone_in(a.resource(), other.system_value)),
message(detail::clone_in(a.resource(), other.message)) {}
Json WireCheckResult::to_json(Memory r) const {
auto out = detail::model_json(r);
detail::model_put(out, "ruleId", rule_id);
nullable(out, "entityType", entity_type);
Json::Array segments(r);
for (const auto& segment : location) {
segments.emplace_back(segment.to_json(r));
}
detail::model_put(out, "location", segments);
detail::model_put(out, "instancePath", instance_path);
nullable(out, "sourceInstancePath", source_instance_path);
nullable(out, "inputValue", input_value);
nullable(out, "systemValue", system_value);
nullable(out, "message", message);
return out;
}
}