-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeywords.cc
70 lines (60 loc) · 2.25 KB
/
keywords.cc
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
#include <iostream>
#include <iomanip>
#include <filesystem>
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
#include <sourcemeta/core/yaml.h>
#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonschema.h>
using Summary = std::map<std::pair<std::string, std::string>, std::uint64_t>;
auto process_schema(const sourcemeta::core::JSON &schema, Summary &summary) -> void {
for (const auto &entry :
sourcemeta::core::SchemaIterator{
schema, sourcemeta::core::schema_official_walker,
sourcemeta::core::schema_official_resolver}) {
if (!entry.subschema.get().is_object()) {
continue;
}
for (const auto &property : entry.subschema.get().as_object()) {
const auto walker_result{sourcemeta::core::schema_official_walker(property.first, entry.vocabularies)};
const auto vocabulary{walker_result.vocabulary.value_or("none")};
const std::pair<std::string, std::string> key{vocabulary, property.first};
summary[key] += 1;
}
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <schemas...>" << std::endl;
return 1;
}
Summary summary;
for (int i = 1; i < argc; i++) {
std::filesystem::path file_path{argv[i]};
if (file_path.extension() == ".json") {
std::cerr << "Processing as JSON: " << file_path.string() << '\n';
process_schema(sourcemeta::core::read_json(file_path), summary);
} else if (file_path.extension() == ".yaml" || file_path.extension() == ".yml") {
std::cerr << "Processing as YAML: " << file_path.string() << '\n';
process_schema(sourcemeta::core::read_yaml(file_path), summary);
} else {
std::cerr << "File is not a schema: " << file_path.string() << '\n';
return 1;
}
}
std::vector<std::pair<std::pair<std::string, std::string>, std::uint64_t>>
vec(summary.begin(), summary.end());
std::sort(vec.begin(), vec.end(),
[](const auto& a, const auto& b) { return a.second > b.second; });
for (const auto &entry : vec) {
std::cout << std::setw(5)
<< entry.second
<< " - "
<< entry.first.second
<< " (" << entry.first.first << ")\n";
}
return 0;
}