Commit 6a5992763677b55620e823e0859c166209dad7f8
1 parent
eda40d87
In JSON::checkSchemaInternal refactor: Modernize loop structures and error handling
Replaced manual key-value access with structured bindings in loops for better readability and modern C++ practices. Simplified error handling using `emplace_back` instead of `push_back`. Improved consistency and clarity in schema validation logic.
Showing
1 changed file
with
13 additions
and
16 deletions
libqpdf/JSON.cc
| ... | ... | @@ -490,24 +490,22 @@ JSON::checkSchemaInternal( |
| 490 | 490 | } |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | - if (sch_dict && (!pattern_key.empty())) { | |
| 493 | + if (sch_dict && !pattern_key.empty()) { | |
| 494 | 494 | auto pattern_schema = sch_dict->members[pattern_key].m->value.get(); |
| 495 | - for (auto const& iter: this_dict->members) { | |
| 496 | - std::string const& key = iter.first; | |
| 495 | + for (auto const& [key, val]: this_dict->members) { | |
| 497 | 496 | checkSchemaInternal( |
| 498 | - this_dict->members[key].m->value.get(), | |
| 497 | + val.m->value.get(), | |
| 499 | 498 | pattern_schema, |
| 500 | 499 | flags, |
| 501 | 500 | errors, |
| 502 | 501 | prefix + "." + key); |
| 503 | 502 | } |
| 504 | 503 | } else if (sch_dict) { |
| 505 | - for (auto& iter: sch_dict->members) { | |
| 506 | - std::string const& key = iter.first; | |
| 507 | - if (this_dict->members.count(key)) { | |
| 504 | + for (auto& [key, val]: sch_dict->members) { | |
| 505 | + if (this_dict->members.contains(key)) { | |
| 508 | 506 | checkSchemaInternal( |
| 509 | 507 | this_dict->members[key].m->value.get(), |
| 510 | - iter.second.m->value.get(), | |
| 508 | + val.m->value.get(), | |
| 511 | 509 | flags, |
| 512 | 510 | errors, |
| 513 | 511 | prefix + "." + key); |
| ... | ... | @@ -516,17 +514,16 @@ JSON::checkSchemaInternal( |
| 516 | 514 | QTC::TC("libtests", "JSON optional key"); |
| 517 | 515 | } else { |
| 518 | 516 | QTC::TC("libtests", "JSON key missing in object"); |
| 519 | - errors.push_back( | |
| 517 | + errors.emplace_back( | |
| 520 | 518 | err_prefix + ": key \"" + key + |
| 521 | 519 | "\" is present in schema but missing in object"); |
| 522 | 520 | } |
| 523 | 521 | } |
| 524 | 522 | } |
| 525 | - for (auto const& iter: this_dict->members) { | |
| 526 | - std::string const& key = iter.first; | |
| 527 | - if (sch_dict->members.count(key) == 0) { | |
| 523 | + for (auto const& [key, val]: this_dict->members) { | |
| 524 | + if (!sch_dict->members.contains(key)) { | |
| 528 | 525 | QTC::TC("libtests", "JSON key extra in object"); |
| 529 | - errors.push_back( | |
| 526 | + errors.emplace_back( | |
| 530 | 527 | err_prefix + ": key \"" + key + |
| 531 | 528 | "\" is not present in schema but appears in object"); |
| 532 | 529 | } |
| ... | ... | @@ -554,9 +551,9 @@ JSON::checkSchemaInternal( |
| 554 | 551 | checkSchemaInternal( |
| 555 | 552 | this_v, sch_arr->elements.at(0).m->value.get(), flags, errors, prefix); |
| 556 | 553 | } |
| 557 | - } else if (!this_arr || (this_arr->elements.size() != n_elements)) { | |
| 554 | + } else if (!this_arr || this_arr->elements.size() != n_elements) { | |
| 558 | 555 | QTC::TC("libtests", "JSON schema array length mismatch"); |
| 559 | - errors.push_back( | |
| 556 | + errors.emplace_back( | |
| 560 | 557 | err_prefix + " is supposed to be an array of length " + std::to_string(n_elements)); |
| 561 | 558 | return false; |
| 562 | 559 | } else { |
| ... | ... | @@ -576,7 +573,7 @@ JSON::checkSchemaInternal( |
| 576 | 573 | } |
| 577 | 574 | } else if (!sch_str) { |
| 578 | 575 | QTC::TC("libtests", "JSON schema other type"); |
| 579 | - errors.push_back(err_prefix + " schema value is not dictionary, array, or string"); | |
| 576 | + errors.emplace_back(err_prefix + " schema value is not dictionary, array, or string"); | |
| 580 | 577 | return false; |
| 581 | 578 | } |
| 582 | 579 | ... | ... |