@@ -46,11 +46,13 @@ static constexpr auto USAGE =
4646 Usage:
4747 schema_validator <schema_file> <document_to_validate> [--internal]
4848 schema_validator (-h | --help)
49+ schema_validator <schema_file> --walk [--internal]
4950 schema_validator --version
5051 Options:
5152 -h --help Show this screen.
5253 --version Show version.
5354 --internal Use internal schema
55+ --walk Just walk the schema and count objects (perf test)
5456)" ;
5557
5658
@@ -134,6 +136,55 @@ bool validate_internal(const std::filesystem::path &file_to_validate)
134136 return result;
135137}
136138
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+
137188int main (int argc, const char **argv)
138189{
139190 try {
@@ -142,9 +193,27 @@ int main(int argc, const char **argv)
142193 true ,// show help if requested
143194 " schema_validator 0.0.1 Copyright 2022 Jason Turner" );// version string
144195
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+
145213 std::filesystem::path schema = args.at (" <schema_file>" ).asString ();
146214 std::filesystem::path doc = args.at (" <document_to_validate>" ).asString ();
147215
216+
148217 if (args.at (" --internal" ).asBool ()) {
149218 validate_internal (doc);
150219 } else {
0 commit comments