Repository navigation
Expand file tree
/
Copy pathbowtie_jsoncons.cpp
More file actions
165 lines (139 loc) · 5.76 KB
/
Copy pathbowtie_jsoncons.cpp
File metadata and controls
165 lines (139 loc) · 5.76 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
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <optional>
#include <string>
#include <utility>
using namespace jsoncons;
int main() {
bool started{false};
jsonschema::evaluation_options options{};
std::string line;
while (std::getline(std::cin, line)) {
const json message{json::parse(line)};
assert(message.contains("cmd"));
assert(message.at("cmd").is_string());
const std::string command{message.at("cmd").as<std::string>()};
if (command == "start") {
// Validate protocol version
started = true;
assert(message.contains("version") && message.at("version").is_integer());
assert(message.at("version").as<int64_t>() == 1);
// Respond with implementation details
json response;
response["version"] = 1;
json implementation;
implementation["language"] = "c++";
implementation["version"] = std::to_string(JSONCONS_VERSION_MAJOR) + "." +
std::to_string(JSONCONS_VERSION_MINOR) + "." +
std::to_string(JSONCONS_VERSION_PATCH);
implementation["name"] = "jsoncons";
implementation["homepage"] = "https://danielaparker.github.io/jsoncons/";
implementation["issues"] =
"https://github.com/danielaparker/jsoncons/issues";
implementation["source"] = "https://github.com/danielaparker/jsoncons";
json dialects(json_array_arg,
{"https://json-schema.org/draft/2020-12/schema",
"https://json-schema.org/draft/2019-09/schema",
"http://json-schema.org/draft-07/schema#",
"http://json-schema.org/draft-06/schema#",
"http://json-schema.org/draft-04/schema#"});
implementation["dialects"] = std::move(dialects);
response["implementation"] = std::move(implementation);
std::cout << print(response) << std::endl;
} else if (command == "dialect") {
// Set the dialect and respond
assert(started);
assert(message.contains("dialect") && message.at("dialect").is_string());
options.default_version(message.at("dialect").as<std::string>());
json response;
response["ok"] = true;
std::cout << print(response) << std::endl;
} else if (command == "run") {
assert(started);
assert(message.contains("seq"));
assert(message.contains("case") && message.at("case").is_object());
assert(message.at("case").contains("schema"));
assert(message.at("case").contains("tests") &&
message.at("case").at("tests").is_array());
if (message.contains("output") && message.at("output").is_string() &&
message.at("output").as<std::string>() == "annotations") {
json response;
response["seq"] = message.at("seq");
response["results"] = json(json_array_arg);
for (const auto &test : message.at("case").at("tests").array_range()) {
(void)test;
json test_result;
test_result["skipped"] = true;
test_result["message"] =
"jsoncons does not support annotation collection";
response["results"].push_back(std::move(test_result));
}
std::cout << print(response) << std::endl;
} else {
const auto schema = message.at("case").at("schema");
try {
// Compile the schema and validate test cases
std::unordered_map<std::string, jsoncons::json> schema_registry;
if (message.at("case").contains("registry")) {
for (const auto &[key, value] :
message.at("case").at("registry").object_range()) {
std::error_code ec;
auto uri = jsoncons::uri::parse(key, ec);
if (!ec) {
schema_registry.emplace(uri.path(), value);
}
}
}
auto resolver = [&](const jsoncons::uri &uri) {
auto it = schema_registry.find(uri.path());
if (it != schema_registry.end()) {
return it->second;
}
return jsoncons::json::null();
};
const auto compiled =
jsonschema::make_json_schema(schema, resolver, options);
json response;
response["seq"] = message.at("seq");
response["results"] = json(json_array_arg);
for (const auto &test : message.at("case").at("tests").array_range()) {
assert(test.contains("instance"));
// Validate this test case and skip error messages
auto valid = true;
auto reporter =
[&valid](const jsonschema::validation_message &message)
-> jsonschema::walk_result {
valid = false;
// TODO Capture error messages
return jsonschema::walk_result::abort;
};
compiled.validate(test.at("instance"), reporter);
json test_result;
test_result["valid"] = std::move(valid);
response["results"].push_back(std::move(test_result));
}
std::cout << print(response) << std::endl;
} catch (const std::exception &error) {
// Report any errors during compilation or validation
json response;
response["seq"] = message.at("seq");
response["errored"] = true;
json context;
context["message"] = error.what();
response["context"] = std::move(context);
std::cout << print(response) << std::endl;
}
}
} else if (command == "stop") {
assert(started);
return EXIT_SUCCESS;
} else {
std::cerr << "Unknown command: " << command << "\n";
return EXIT_FAILURE;
}
}
return EXIT_FAILURE;
}