Skip to content

Commit 9145ad9

Browse files
authoredApr 9, 2022
Merge pull request #9 from lefticus/fix_build_errors
Work on various CI build errors

File tree

5 files changed

+75
-63
lines changed

5 files changed

+75
-63
lines changed
 

‎include/json2cpp/json2cpp.hpp

+47-26
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ template<typename CharType> struct data_variant
126126
// cppcheck-suppress noExplicitConstructor
127127
constexpr data_variant(std::basic_string_view<CharType> s) : value{ s }, selected{ selected_type::string } {}
128128

129+
[[nodiscard]] constexpr bool is_boolean() const noexcept { return selected == selected_type::boolean; }
130+
129131
[[nodiscard]] constexpr const bool *get_if_boolean() const noexcept
130132
{
131133
if (selected == selected_type::boolean) {
@@ -135,51 +137,68 @@ template<typename CharType> struct data_variant
135137
}
136138
}
137139

140+
[[nodiscard]] constexpr bool is_array() const noexcept { return selected == selected_type::array; }
141+
138142
[[nodiscard]] constexpr const basic_array_t<CharType> *get_if_array() const noexcept
139143
{
140-
if (selected == selected_type::array) {
144+
if (is_array()) {
141145
return &value.array_;
142146
} else {
143147
return nullptr;
144148
}
145149
}
150+
151+
[[nodiscard]] constexpr bool is_object() const noexcept { return selected == selected_type::object; }
152+
146153
[[nodiscard]] constexpr const basic_object_t<CharType> *get_if_object() const noexcept
147154
{
148-
if (selected == selected_type::object) {
155+
if (is_object()) {
149156
return &value.object_;
150157
} else {
151158
return nullptr;
152159
}
153160
}
161+
162+
[[nodiscard]] constexpr bool is_integer() const noexcept { return selected == selected_type::integer; }
163+
154164
[[nodiscard]] constexpr const std::int64_t *get_if_integer() const noexcept
155165
{
156-
if (selected == selected_type::integer) {
166+
if (is_integer()) {
157167
return &value.int64_t_;
158168
} else {
159169
return nullptr;
160170
}
161171
}
172+
173+
[[nodiscard]] constexpr bool is_uinteger() const noexcept { return selected == selected_type::uinteger; }
174+
162175
[[nodiscard]] constexpr const std::uint64_t *get_if_uinteger() const noexcept
163176
{
164-
if (selected == selected_type::uinteger) {
177+
if (is_uinteger()) {
165178
return &value.uint64_t_;
166179
} else {
167180
return nullptr;
168181
}
169182
}
170183

184+
185+
[[nodiscard]] constexpr bool is_floating_point() const noexcept { return selected == selected_type::floating_point; }
186+
187+
171188
[[nodiscard]] constexpr const double *get_if_floating_point() const noexcept
172189
{
173-
if (selected == selected_type::floating_point) {
190+
if (is_floating_point()) {
174191
return &value.double_;
175192
} else {
176193
return nullptr;
177194
}
178195
}
179196

197+
[[nodiscard]] constexpr bool is_string() const noexcept { return selected == selected_type::string; }
198+
180199
[[nodiscard]] constexpr const std::basic_string_view<CharType> *get_if_string() const noexcept
181200
{
182-
if (selected == selected_type::string) {
201+
if (is_string()) {
183202
return &value.string_view_;
184203
} else {
185204
return nullptr;
@@ -199,7 +218,7 @@ template<typename CharType> struct basic_json
199218
: parent_value_(&value), index_{ index }
200219
{}
201220

202-
constexpr const basic_json &operator*() const noexcept
221+
constexpr const basic_json &operator*() const
203222
{
204223
if (parent_value_->is_array()) {
205224
return (*parent_value_)[index_];
@@ -340,7 +359,7 @@ template<typename CharType> struct basic_json
340359
}
341360
}
342361

343-
template<typename Key>[[nodiscard]] constexpr std::size_t count(const Key &key) const noexcept
362+
template<typename Key> [[nodiscard]] constexpr std::size_t count(const Key &key) const
344363
{
345364
if (is_object()) {
346365
const auto found = find(key);
@@ -353,7 +372,7 @@ template<typename CharType> struct basic_json
353372
return 0;
354373
}
355374

356-
[[nodiscard]] constexpr iterator find(const std::basic_string_view<CharType> key) const noexcept
375+
[[nodiscard]] constexpr iterator find(const std::basic_string_view<CharType> key) const
357376
{
358377
for (auto itr = begin(); itr != end(); ++itr) {
359378
if (itr.key() == key) { return itr; }
@@ -369,17 +388,17 @@ template<typename CharType> struct basic_json
369388

370389
constexpr const auto &array_data() const
371390
{
372-
if (const auto *result = data.get_if_array(); result != nullptr) {
373-
return *result;
391+
if (data.is_array()) {
392+
return *data.get_if_array();
374393
} else {
375394
throw std::runtime_error("value is not an array type");
376395
}
377396
}
378397

379398
constexpr const auto &object_data() const
380399
{
381-
if (const auto *result = data.get_if_object(); result != nullptr) {
382-
return *result;
400+
if (data.is_object()) {
401+
return *data.get_if_object();
383402
} else {
384403
throw std::runtime_error("value is not an object type");
385404
}
@@ -388,35 +407,37 @@ template<typename CharType> struct basic_json
388407
constexpr static basic_json object() { return basic_json{ data_t{ basic_object_t<CharType>{} } }; }
389408
constexpr static basic_json array() { return basic_json{ data_t{ basic_array_t<CharType>{} } }; }
390409

391-
template<typename Type>[[nodiscard]] constexpr auto get() const
410+
template<typename Type> [[nodiscard]] constexpr auto get() const
392411
{
393412
// I don't like this level of implicit conversions in the `get()` function,
394413
// but it's necessary for API compatibility with nlohmann::json
395-
if constexpr (std::is_same_v<Type, std::uint64_t> || std::is_same_v<Type, std::int64_t> || std::is_same_v<Type, double>) {
396-
if (const auto *uint_value = data.get_if_uinteger(); uint_value != nullptr) {
397-
return Type(*uint_value);
398-
} else if (const auto *value = data.get_if_integer(); value != nullptr) {
399-
return Type(*value);
400-
} else if (const auto *fpvalue = data.get_if_floating_point(); fpvalue != nullptr) {
401-
return Type(*fpvalue);
414+
if constexpr (std::is_same_v<Type,
415+
std::uint64_t> || std::is_same_v<Type, std::int64_t> || std::is_same_v<Type, double>) {
416+
if (data.is_uinteger()) {
417+
return Type(*data.get_if_uinteger());
418+
} else if (data.is_integer()) {
419+
return Type(*data.get_if_integer());
420+
} else if (data.is_floating_point()) {
421+
return Type(*data.get_if_floating_point());
402422
} else {
403423
throw std::runtime_error("Unexpected type: number requested");// + ss.str() );
404424
}
405425
} else if constexpr (std::is_same_v<Type,
406426
std::basic_string_view<CharType>> || std::is_same_v<Type, std::basic_string<CharType>>) {
407-
if (const auto *value = data.get_if_string(); value != nullptr) { return *value; }
408-
else {
427+
if (data.is_string()) {
428+
return *data.get_if_string();
429+
} else {
409430
throw std::runtime_error("Unexpected type: string-like requested");
410431
}
411432
} else if constexpr (std::is_same_v<Type, bool>) {
412-
if (const auto *value = data.get_if_boolean(); value != nullptr) { return *value; }
413-
else {
433+
if (data.is_boolean()) {
434+
return *data.get_if_boolean();
435+
} else {
414436
throw std::runtime_error("Unexpected type: bool requested");
415437
}
416438
} else {
417439
throw std::runtime_error("Unexpected type for get()");
418440
}
419-
420441
}
421442

422443
[[nodiscard]] constexpr bool is_object() const noexcept { return data.selected == data_t::selected_type::object; }

‎include/json2cpp/json2cpp_adapter.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -494,23 +494,23 @@ namespace adapters {
494494

495495
bool operator!=(const json2cppJsonArrayValueIterator &other) const { return !(m_itr == other.m_itr); }
496496

497-
// cppcheck-suppress functionConst
497+
// cppcheck-suppress functionConst
498498
const json2cppJsonArrayValueIterator &operator++()
499499
{
500500
++m_itr;
501501

502502
return *this;
503503
}
504504

505-
// cppcheck-suppress functionConst
505+
// cppcheck-suppress functionConst
506506
json2cppJsonArrayValueIterator operator++(int)
507507
{
508508
json2cppJsonArrayValueIterator iterator_pre(m_itr);
509509
++(*this);
510510
return iterator_pre;
511511
}
512512

513-
// cppcheck-suppress functionConst
513+
// cppcheck-suppress functionConst
514514
const json2cppJsonArrayValueIterator &operator--()
515515
{
516516
--m_itr;
@@ -575,23 +575,23 @@ namespace adapters {
575575

576576
bool operator!=(const json2cppJsonObjectMemberIterator &other) const { return !(m_itr == other.m_itr); }
577577

578-
// cppcheck-suppress functionConst
578+
// cppcheck-suppress functionConst
579579
const json2cppJsonObjectMemberIterator &operator++()
580580
{
581581
++m_itr;
582582

583583
return *this;
584584
}
585585

586-
// cppcheck-suppress functionConst
586+
// cppcheck-suppress functionConst
587587
json2cppJsonObjectMemberIterator operator++(int)
588588
{
589589
json2cppJsonObjectMemberIterator iterator_pre(m_itr);
590590
++(*this);
591591
return iterator_pre;
592592
}
593593

594-
// cppcheck-suppress functionConst
594+
// cppcheck-suppress functionConst
595595
const json2cppJsonObjectMemberIterator &operator--()
596596
{
597597
--m_itr;

‎src/json2cpp.cpp

+15-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ SOFTWARE.
2323
*/
2424

2525

26-
2726
#include "json2cpp.hpp"
2827
#include <fstream>
2928

@@ -36,13 +35,12 @@ std::string compile(const nlohmann::json &value, std::size_t &obj_count, std::ve
3635
if (value.is_object()) {
3736
std::vector<std::string> pairs;
3837
for (auto itr = value.begin(); itr != value.end(); ++itr) {
39-
pairs.push_back(fmt::format(
40-
"value_pair_t{{{}, {{{}}}}},", json_string(itr.key()), compile(*itr, obj_count, lines)));
38+
pairs.push_back(
39+
fmt::format("value_pair_t{{{}, {{{}}}}},", json_string(itr.key()), compile(*itr, obj_count, lines)));
4140
}
4241

43-
lines.push_back(fmt::format("inline constexpr std::array<value_pair_t, {}> object_data_{} = {{",
44-
pairs.size(),
45-
current_object_number));
42+
lines.push_back(fmt::format(
43+
"inline constexpr std::array<value_pair_t, {}> object_data_{} = {{", pairs.size(), current_object_number));
4644

4745
std::transform(pairs.begin(), pairs.end(), std::back_inserter(lines), [](const auto &pair) {
4846
return fmt::format(" {}", pair);
@@ -58,9 +56,8 @@ std::string compile(const nlohmann::json &value, std::size_t &obj_count, std::ve
5856
});
5957

6058

61-
lines.push_back(fmt::format("inline constexpr std::array<json, {}> object_data_{} = {{{{",
62-
entries.size(),
63-
current_object_number));
59+
lines.push_back(fmt::format(
60+
"inline constexpr std::array<json, {}> object_data_{} = {{{{", entries.size(), current_object_number));
6461

6562
std::transform(entries.begin(), entries.end(), std::back_inserter(lines), [](const auto &entry) {
6663
return fmt::format(" {}", entry);
@@ -112,8 +109,7 @@ compile_results compile(const std::string_view document_name, const nlohmann::js
112109

113110
results.impl.emplace_back("#include <json2cpp/json2cpp.hpp>");
114111

115-
results.impl.push_back(
116-
fmt::format(R"(
112+
results.impl.push_back(fmt::format(R"(
117113
namespace compiled_json::{}::impl {{
118114
119115
using json = json2cpp::basic_json<char>;
@@ -123,7 +119,8 @@ using array_t=json2cpp::basic_array_t<char>;
123119
using object_t=json2cpp::basic_object_t<char>;
124120
using value_pair_t=json2cpp::basic_value_pair_t<char>;
125121
126-
)", document_name));
122+
)",
123+
document_name));
127124

128125

129126
const auto last_obj_name = compile(json, obj_count, results.impl);
@@ -136,7 +133,8 @@ inline constexpr auto document = json{{{{{}}}}};
136133
137134
#endif
138135
139-
)", last_obj_name));
136+
)",
137+
last_obj_name));
140138

141139

142140
spdlog::info("{} JSON objects processed.", obj_count);
@@ -178,7 +176,10 @@ void write_compilation([[maybe_unused]] std::string_view document_name,
178176

179177
std::ofstream cpp(cpp_name);
180178
cpp << fmt::format("#include \"{}\"\n", impl_name.filename().string());
181-
cpp << fmt::format("namespace compiled_json::{} {{\nconst json2cpp::json &get() {{ return compiled_json::{}::impl::document; }}\n}}\n", document_name, document_name);
179+
cpp << fmt::format(
180+
"namespace compiled_json::{} {{\nconst json2cpp::json &get() {{ return compiled_json::{}::impl::document; }}\n}}\n",
181+
document_name,
182+
document_name);
182183
}
183184

184185
void compile_to(const std::string_view document_name,

‎src/schema_validator.cpp

+4-11
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ void walk_internal(std::int64_t &int_sum,
163163
}
164164
}
165165

166-
template<typename JSON>
167-
void walk(const JSON &objects)
166+
template<typename JSON> void walk(const JSON &objects)
168167
{
169168
std::int64_t int_sum{};
170169
double double_sum{};
@@ -174,13 +173,7 @@ void walk(const JSON &objects)
174173

175174
spdlog::info("Starting tree walk");
176175

177-
walk_internal(int_sum,
178-
double_sum,
179-
string_sizes,
180-
array_count,
181-
object_count,
182-
objects
183-
);
176+
walk_internal(int_sum, double_sum, string_sizes, array_count, object_count, objects);
184177

185178
spdlog::info("{} {} {} {} {}", int_sum, double_sum, string_sizes, array_count, object_count);
186179
}
@@ -193,9 +186,9 @@ int main(int argc, const char **argv)
193186
true,// show help if requested
194187
"schema_validator 0.0.1 Copyright 2022 Jason Turner");// version string
195188

196-
if (args.at("--walk").asBool()) {
189+
if (args.at("--walk").asBool()) {
197190
if (args.at("--internal").asBool()) {
198-
walk(compiled_json::energyplus_schema::get());
191+
walk(compiled_json::energyplus_schema::get());
199192
} else {
200193
std::filesystem::path schema_file_name = args.at("<schema_file>").asString();
201194
spdlog::info("Creating nlohmann::json object");

‎test/valijson_tests.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ TEST_CASE("Can load a valijson schema")
1919
// Parse JSON schema content using valijson
2020
Schema mySchema;
2121
SchemaParser parser;
22-
json2cppJsonAdapter mySchemaAdapter(
23-
compiled_json::allof_integers_and_numbers_schema::get());
22+
json2cppJsonAdapter mySchemaAdapter(compiled_json::allof_integers_and_numbers_schema::get());
2423
CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema));
2524
}
2625

@@ -36,8 +35,7 @@ TEST_CASE("Validation fails where expected")
3635
// Parse JSON schema content using valijson
3736
Schema mySchema;
3837
SchemaParser parser;
39-
json2cppJsonAdapter mySchemaAdapter(
40-
compiled_json::allof_integers_and_numbers_schema::get());
38+
json2cppJsonAdapter mySchemaAdapter(compiled_json::allof_integers_and_numbers_schema::get());
4139
CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema));
4240

4341
Validator validator;
@@ -58,8 +56,7 @@ TEST_CASE("Can validate a document")
5856
// Parse JSON schema content using valijson
5957
Schema mySchema;
6058
SchemaParser parser;
61-
json2cppJsonAdapter mySchemaAdapter(
62-
compiled_json::allof_integers_and_numbers_schema::get());
59+
json2cppJsonAdapter mySchemaAdapter(compiled_json::allof_integers_and_numbers_schema::get());
6360
CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema));
6461

6562
Validator validator;

0 commit comments

Comments
 (0)
Please sign in to comment.