-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcommand_fmt.cc
More file actions
325 lines (293 loc) · 12.8 KB
/
Copy pathcommand_fmt.cc
File metadata and controls
325 lines (293 loc) · 12.8 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <sourcemeta/blaze/format.h>
#include <sourcemeta/blaze/foundation.h>
#include <sourcemeta/core/diff.h>
#include <sourcemeta/core/io.h>
#include <sourcemeta/core/json.h>
#include <iostream> // std::cerr, std::cout
#include <sstream> // std::ostringstream
#include <string> // std::string
#include <string_view> // std::string_view
#include <utility> // std::move, std::unreachable
#include "command.h"
#include "error.h"
#include "input.h"
#include "logger.h"
#include "resolver.h"
#include "utils.h"
namespace {
auto diff_operation_name(const sourcemeta::core::Diff::Operation::Type type)
-> std::string_view {
switch (type) {
case sourcemeta::core::Diff::Operation::Type::Equal:
return "equal";
case sourcemeta::core::Diff::Operation::Type::Delete:
return "delete";
case sourcemeta::core::Diff::Operation::Type::Insert:
return "insert";
default:
std::unreachable();
}
}
auto to_diff_json(const sourcemeta::core::Diff &difference)
-> sourcemeta::core::JSON {
auto operations{sourcemeta::core::JSON::make_array()};
for (const auto &operation : difference.operations) {
// An insertion is the only operation that reads the modified input, as
// the lines it introduces are not present in the original one
const auto insertion{operation.type ==
sourcemeta::core::Diff::Operation::Type::Insert};
const auto &tokens{insertion ? difference.modified : difference.original};
const auto start{insertion ? operation.modified_start
: operation.original_start};
const auto end{insertion ? operation.modified_end : operation.original_end};
auto lines{sourcemeta::core::JSON::make_array()};
for (auto index{start}; index < end; ++index) {
lines.push_back(sourcemeta::core::JSON{tokens[index]});
}
auto entry{sourcemeta::core::JSON::make_object()};
entry.assign("type",
sourcemeta::core::JSON{diff_operation_name(operation.type)});
entry.assign("lines", std::move(lines));
operations.push_back(std::move(entry));
}
return operations;
}
auto report_check_failure(const std::string ¤t,
const std::string &expected, const std::string &label,
const bool output_json,
sourcemeta::core::JSON &errors) -> void {
const auto difference{sourcemeta::core::diff(
current, expected, sourcemeta::core::Diff::Mode::Line,
sourcemeta::core::Diff::Algorithm::Myers)};
if (output_json) {
auto entry{sourcemeta::core::JSON::make_object()};
entry.assign("path", sourcemeta::core::JSON{label});
entry.assign("diff", to_diff_json(difference));
errors.push_back(std::move(entry));
} else {
std::cerr << "fail: " << label << "\n";
sourcemeta::core::stringify(
difference, std::cerr, sourcemeta::core::Diff::Format::Unified,
{.original_label = "current", .modified_label = "expected"});
}
}
} // namespace
auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
-> void {
validate_http_headers(options);
const bool output_json{options.contains("json")};
bool result{true};
auto errors{sourcemeta::core::JSON::make_array()};
const auto indentation{parse_indentation(options)};
const auto handle_stdin = [&]() {
const auto current_path{std::filesystem::current_path()};
const auto configuration_path{find_configuration(current_path)};
const auto &configuration{
read_configuration(options, configuration_path, current_path)};
const auto display_path{stdin_path()};
std::string raw_stdin;
const auto parsed{read_from_stdin(&raw_stdin)};
if (parsed.yaml) {
throw YAMLInputError{"This command does not support YAML input files yet",
display_path};
}
const auto &document{parsed.document};
const auto dialect{default_dialect(options, configuration)};
const auto is_test_document =
dialect.empty() && looks_like_test_document(document);
const auto effective_dialect =
is_test_document ? TEST_DOCUMENT_DEFAULT_DIALECT : dialect;
if (is_test_document) {
std::cerr << "Interpreting as a test file: "
<< display_path.generic_string() << "\n";
}
const auto &custom_resolver{resolver(options, options.contains("http"),
effective_dialect, configuration)};
const auto stdin_label{display_path.generic_string()};
try {
if (options.contains("check")) {
std::ostringstream expected;
if (options.contains("keep-ordering")) {
sourcemeta::core::prettify(document, expected, indentation);
} else {
auto copy = document;
sourcemeta::blaze::format(copy, sourcemeta::blaze::schema_walker,
custom_resolver, effective_dialect);
sourcemeta::core::prettify(copy, expected, indentation);
}
expected << "\n";
if (raw_stdin == expected.str()) {
LOG_VERBOSE(options) << "ok: " << stdin_label << "\n";
} else {
report_check_failure(raw_stdin, expected.str(), stdin_label,
output_json, errors);
result = false;
}
} else {
if (options.contains("keep-ordering")) {
sourcemeta::core::prettify(document, std::cout, indentation);
} else {
auto copy = document;
sourcemeta::blaze::format(copy, sourcemeta::blaze::schema_walker,
custom_resolver, effective_dialect);
sourcemeta::core::prettify(copy, std::cout, indentation);
}
std::cout << "\n";
}
} catch (const sourcemeta::blaze::SchemaKeywordError &error) {
throw sourcemeta::core::FileError<sourcemeta::blaze::SchemaKeywordError>(
display_path, error);
} catch (const sourcemeta::blaze::SchemaFrameError &error) {
throw sourcemeta::core::FileError<sourcemeta::blaze::SchemaFrameError>(
display_path, error);
} catch (const sourcemeta::blaze::SchemaAnchorCollisionError &error) {
const auto position{parsed.positions.get(error.location())};
if (position.has_value()) {
throw PositionError<sourcemeta::core::FileError<
sourcemeta::blaze::SchemaAnchorCollisionError>>(
std::get<0>(position.value()), std::get<1>(position.value()),
display_path, error);
}
throw sourcemeta::core::FileError<
sourcemeta::blaze::SchemaAnchorCollisionError>(display_path, error);
} catch (const sourcemeta::blaze::SchemaRelativeMetaschemaResolutionError
&error) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::SchemaRelativeMetaschemaResolutionError>(
display_path, error);
} catch (const sourcemeta::blaze::SchemaResolutionError &error) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::SchemaResolutionError>(display_path, error);
} catch (const sourcemeta::blaze::SchemaUnknownBaseDialectError &) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::SchemaUnknownBaseDialectError>(display_path);
} catch (const sourcemeta::blaze::SchemaError &error) {
throw sourcemeta::core::FileError<sourcemeta::blaze::SchemaError>(
display_path, error.what());
}
};
const auto handle_file_entry = [&](const InputJSON &entry) {
if (entry.yaml) {
throw YAMLInputError{"This command does not support YAML input files yet",
entry.resolution_base};
}
if (options.contains("check")) {
LOG_VERBOSE(options) << "Checking: " << entry.first << "\n";
} else {
LOG_VERBOSE(options) << "Formatting: " << entry.first << "\n";
}
try {
const auto configuration_path{find_configuration(entry.resolution_base)};
const auto &configuration{read_configuration(options, configuration_path,
entry.resolution_base)};
const auto dialect{default_dialect(options, configuration)};
const auto is_test_document =
dialect.empty() && looks_like_test_document(entry.second);
const auto effective_dialect =
is_test_document ? TEST_DOCUMENT_DEFAULT_DIALECT : dialect;
if (is_test_document) {
std::cerr << "Interpreting as a test file: " << entry.first << "\n";
}
const auto &custom_resolver{resolver(options, options.contains("http"),
effective_dialect, configuration)};
std::ostringstream expected;
if (options.contains("keep-ordering")) {
sourcemeta::core::prettify(entry.second, expected, indentation);
} else {
auto copy = entry.second;
sourcemeta::blaze::format(copy, sourcemeta::blaze::schema_walker,
custom_resolver, effective_dialect);
sourcemeta::core::prettify(copy, expected, indentation);
}
expected << "\n";
const auto current{
sourcemeta::core::read_file_to_string(entry.resolution_base)};
if (options.contains("check")) {
if (current == expected.str()) {
LOG_VERBOSE(options) << "ok: " << entry.first << "\n";
} else {
report_check_failure(current, expected.str(), entry.first,
output_json, errors);
result = false;
}
} else {
if (current != expected.str()) {
sourcemeta::core::atomic_write_file(entry.resolution_base,
expected.str());
}
}
} catch (const sourcemeta::blaze::SchemaKeywordError &error) {
throw sourcemeta::core::FileError<sourcemeta::blaze::SchemaKeywordError>(
entry.resolution_base, error);
} catch (const sourcemeta::blaze::SchemaFrameError &error) {
throw sourcemeta::core::FileError<sourcemeta::blaze::SchemaFrameError>(
entry.resolution_base, error);
} catch (const sourcemeta::blaze::SchemaAnchorCollisionError &error) {
const auto position{entry.positions.get(error.location())};
if (position.has_value()) {
throw PositionError<sourcemeta::core::FileError<
sourcemeta::blaze::SchemaAnchorCollisionError>>(
std::get<0>(position.value()), std::get<1>(position.value()),
entry.resolution_base, error);
}
throw sourcemeta::core::FileError<
sourcemeta::blaze::SchemaAnchorCollisionError>(entry.resolution_base,
error);
} catch (const sourcemeta::blaze::SchemaRelativeMetaschemaResolutionError
&error) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::SchemaRelativeMetaschemaResolutionError>(
entry.resolution_base, error);
} catch (const sourcemeta::blaze::SchemaResolutionError &error) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::SchemaResolutionError>(entry.resolution_base,
error);
} catch (const sourcemeta::blaze::SchemaUnknownBaseDialectError &) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::SchemaUnknownBaseDialectError>(
entry.resolution_base);
} catch (const sourcemeta::blaze::SchemaUnknownDialectError &) {
throw sourcemeta::core::FileError<
sourcemeta::blaze::SchemaUnknownDialectError>(entry.resolution_base);
} catch (const sourcemeta::blaze::SchemaError &error) {
throw sourcemeta::core::FileError<sourcemeta::blaze::SchemaError>(
entry.resolution_base, error.what());
}
};
// Process arguments in order to preserve argument ordering semantics.
// When no positional arguments are given, default to for_each_json(options)
// which scans the current directory.
if (options.positional().empty()) {
for (const auto &entry : for_each_json(options)) {
handle_file_entry(entry);
}
} else {
check_no_duplicate_stdin(options.positional());
for (const auto &arg : options.positional()) {
if (arg == "-") {
handle_stdin();
} else {
for (const auto &entry : for_each_json({arg}, options)) {
handle_file_entry(entry);
}
}
}
}
if (options.contains("check") && output_json) {
auto output_json_object{sourcemeta::core::JSON::make_object()};
output_json_object.assign("valid", sourcemeta::core::JSON{result});
if (!result) {
output_json_object.assign("errors", std::move(errors));
}
sourcemeta::core::prettify(output_json_object, std::cout, indentation);
std::cout << "\n";
}
if (!result) {
if (!output_json) {
std::cerr << "\nRun the `fmt` command without `--check/-c` to fix the "
"formatting"
<< "\n";
}
throw Fail{EXIT_EXPECTED_FAILURE};
}
}