@@ -46,11 +46,13 @@ static constexpr auto USAGE =
46
46
Usage:
47
47
schema_validator <schema_file> <document_to_validate> [--internal]
48
48
schema_validator (-h | --help)
49
+ schema_validator <schema_file> --walk [--internal]
49
50
schema_validator --version
50
51
Options:
51
52
-h --help Show this screen.
52
53
--version Show version.
53
54
--internal Use internal schema
55
+ --walk Just walk the schema and count objects (perf test)
54
56
)" ;
55
57
56
58
@@ -134,6 +136,55 @@ bool validate_internal(const std::filesystem::path &file_to_validate)
134
136
return result;
135
137
}
136
138
139
+ template <typename JSON>
140
+ void walk_internal (std::int64_t &int_sum,
141
+ double &double_sum,
142
+ std::size_t &string_sizes,
143
+ int &array_count,
144
+ int &object_count,
145
+ const JSON &obj)
146
+ {
147
+ if (obj.is_number_integer ()) {
148
+ int_sum += obj.template get <std::int64_t >();
149
+ } else if (obj.is_number_float ()) {
150
+ double_sum += obj.template get <double >();
151
+ } else if (obj.is_string ()) {
152
+ string_sizes += obj.template get <std::string_view>().size ();
153
+ } else if (obj.is_array ()) {
154
+ ++array_count;
155
+ for (const auto &child : obj) {
156
+ walk_internal (int_sum, double_sum, string_sizes, array_count, object_count, child);
157
+ }
158
+ } else if (obj.is_object ()) {
159
+ ++object_count;
160
+ for (const auto &child : obj) {
161
+ walk_internal (int_sum, double_sum, string_sizes, array_count, object_count, child);
162
+ }
163
+ }
164
+ }
165
+
166
+ template <typename JSON>
167
+ void walk (const JSON &objects)
168
+ {
169
+ std::int64_t int_sum{};
170
+ double double_sum{};
171
+ std::size_t string_sizes{};
172
+ int array_count{};
173
+ int object_count{};
174
+
175
+ spdlog::info (" Starting tree walk" );
176
+
177
+ walk_internal (int_sum,
178
+ double_sum,
179
+ string_sizes,
180
+ array_count,
181
+ object_count,
182
+ objects
183
+ );
184
+
185
+ spdlog::info (" {} {} {} {} {}" , int_sum, double_sum, string_sizes, array_count, object_count);
186
+ }
187
+
137
188
int main (int argc, const char **argv)
138
189
{
139
190
try {
@@ -142,9 +193,27 @@ int main(int argc, const char **argv)
142
193
true ,// show help if requested
143
194
" schema_validator 0.0.1 Copyright 2022 Jason Turner" );// version string
144
195
196
+ if (args.at (" --walk" ).asBool ()) {
197
+ if (args.at (" --internal" ).asBool ()) {
198
+ walk (compiled_json::energyplus_schema::get_energyplus_schema ());
199
+ } else {
200
+ std::filesystem::path schema_file_name = args.at (" <schema_file>" ).asString ();
201
+ spdlog::info (" Creating nlohmann::json object" );
202
+ nlohmann::json schema;
203
+ spdlog::info (" Opening json file" );
204
+ std::ifstream schema_file (schema_file_name);
205
+ spdlog::info (" Loading json file" );
206
+ schema_file >> schema;
207
+ walk (schema);
208
+ }
209
+ return EXIT_SUCCESS;
210
+ }
211
+
212
+
145
213
std::filesystem::path schema = args.at (" <schema_file>" ).asString ();
146
214
std::filesystem::path doc = args.at (" <document_to_validate>" ).asString ();
147
215
216
+
148
217
if (args.at (" --internal" ).asBool ()) {
149
218
validate_internal (doc);
150
219
} else {
0 commit comments