Skip to content

Work on various CI build errors #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 47 additions & 26 deletions include/json2cpp/json2cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ template<typename CharType> struct data_variant
// cppcheck-suppress noExplicitConstructor
constexpr data_variant(std::basic_string_view<CharType> s) : value{ s }, selected{ selected_type::string } {}

[[nodiscard]] constexpr bool is_boolean() const noexcept { return selected == selected_type::boolean; }

[[nodiscard]] constexpr const bool *get_if_boolean() const noexcept
{
if (selected == selected_type::boolean) {
Expand All @@ -135,51 +137,68 @@ template<typename CharType> struct data_variant
}
}

[[nodiscard]] constexpr bool is_array() const noexcept { return selected == selected_type::array; }

[[nodiscard]] constexpr const basic_array_t<CharType> *get_if_array() const noexcept
{
if (selected == selected_type::array) {
if (is_array()) {
return &value.array_;
} else {
return nullptr;
}
}

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

[[nodiscard]] constexpr const basic_object_t<CharType> *get_if_object() const noexcept
{
if (selected == selected_type::object) {
if (is_object()) {
return &value.object_;
} else {
return nullptr;
}
}

[[nodiscard]] constexpr bool is_integer() const noexcept { return selected == selected_type::integer; }

[[nodiscard]] constexpr const std::int64_t *get_if_integer() const noexcept
{
if (selected == selected_type::integer) {
if (is_integer()) {
return &value.int64_t_;
} else {
return nullptr;
}
}

[[nodiscard]] constexpr bool is_uinteger() const noexcept { return selected == selected_type::uinteger; }

[[nodiscard]] constexpr const std::uint64_t *get_if_uinteger() const noexcept
{
if (selected == selected_type::uinteger) {
if (is_uinteger()) {
return &value.uint64_t_;
} else {
return nullptr;
}
}


[[nodiscard]] constexpr bool is_floating_point() const noexcept { return selected == selected_type::floating_point; }


[[nodiscard]] constexpr const double *get_if_floating_point() const noexcept
{
if (selected == selected_type::floating_point) {
if (is_floating_point()) {
return &value.double_;
} else {
return nullptr;
}
}

[[nodiscard]] constexpr bool is_string() const noexcept { return selected == selected_type::string; }

[[nodiscard]] constexpr const std::basic_string_view<CharType> *get_if_string() const noexcept
{
if (selected == selected_type::string) {
if (is_string()) {
return &value.string_view_;
} else {
return nullptr;
Expand All @@ -199,7 +218,7 @@ template<typename CharType> struct basic_json
: parent_value_(&value), index_{ index }
{}

constexpr const basic_json &operator*() const noexcept
constexpr const basic_json &operator*() const
{
if (parent_value_->is_array()) {
return (*parent_value_)[index_];
Expand Down Expand Up @@ -340,7 +359,7 @@ template<typename CharType> struct basic_json
}
}

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

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

constexpr const auto &array_data() const
{
if (const auto *result = data.get_if_array(); result != nullptr) {
return *result;
if (data.is_array()) {
return *data.get_if_array();
} else {
throw std::runtime_error("value is not an array type");
}
}

constexpr const auto &object_data() const
{
if (const auto *result = data.get_if_object(); result != nullptr) {
return *result;
if (data.is_object()) {
return *data.get_if_object();
} else {
throw std::runtime_error("value is not an object type");
}
Expand All @@ -388,35 +407,37 @@ template<typename CharType> struct basic_json
constexpr static basic_json object() { return basic_json{ data_t{ basic_object_t<CharType>{} } }; }
constexpr static basic_json array() { return basic_json{ data_t{ basic_array_t<CharType>{} } }; }

template<typename Type>[[nodiscard]] constexpr auto get() const
template<typename Type> [[nodiscard]] constexpr auto get() const
{
// I don't like this level of implicit conversions in the `get()` function,
// but it's necessary for API compatibility with nlohmann::json
if constexpr (std::is_same_v<Type, std::uint64_t> || std::is_same_v<Type, std::int64_t> || std::is_same_v<Type, double>) {
if (const auto *uint_value = data.get_if_uinteger(); uint_value != nullptr) {
return Type(*uint_value);
} else if (const auto *value = data.get_if_integer(); value != nullptr) {
return Type(*value);
} else if (const auto *fpvalue = data.get_if_floating_point(); fpvalue != nullptr) {
return Type(*fpvalue);
if constexpr (std::is_same_v<Type,
std::uint64_t> || std::is_same_v<Type, std::int64_t> || std::is_same_v<Type, double>) {
if (data.is_uinteger()) {
return Type(*data.get_if_uinteger());
} else if (data.is_integer()) {
return Type(*data.get_if_integer());
} else if (data.is_floating_point()) {
return Type(*data.get_if_floating_point());
} else {
throw std::runtime_error("Unexpected type: number requested");// + ss.str() );
}
} else if constexpr (std::is_same_v<Type,
std::basic_string_view<CharType>> || std::is_same_v<Type, std::basic_string<CharType>>) {
if (const auto *value = data.get_if_string(); value != nullptr) { return *value; }
else {
if (data.is_string()) {
return *data.get_if_string();
} else {
throw std::runtime_error("Unexpected type: string-like requested");
}
} else if constexpr (std::is_same_v<Type, bool>) {
if (const auto *value = data.get_if_boolean(); value != nullptr) { return *value; }
else {
if (data.is_boolean()) {
return *data.get_if_boolean();
} else {
throw std::runtime_error("Unexpected type: bool requested");
}
} else {
throw std::runtime_error("Unexpected type for get()");
}

}

[[nodiscard]] constexpr bool is_object() const noexcept { return data.selected == data_t::selected_type::object; }
Expand Down
12 changes: 6 additions & 6 deletions include/json2cpp/json2cpp_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,23 +494,23 @@ namespace adapters {

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

// cppcheck-suppress functionConst
// cppcheck-suppress functionConst
const json2cppJsonArrayValueIterator &operator++()
{
++m_itr;

return *this;
}

// cppcheck-suppress functionConst
// cppcheck-suppress functionConst
json2cppJsonArrayValueIterator operator++(int)
{
json2cppJsonArrayValueIterator iterator_pre(m_itr);
++(*this);
return iterator_pre;
}

// cppcheck-suppress functionConst
// cppcheck-suppress functionConst
const json2cppJsonArrayValueIterator &operator--()
{
--m_itr;
Expand Down Expand Up @@ -575,23 +575,23 @@ namespace adapters {

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

// cppcheck-suppress functionConst
// cppcheck-suppress functionConst
const json2cppJsonObjectMemberIterator &operator++()
{
++m_itr;

return *this;
}

// cppcheck-suppress functionConst
// cppcheck-suppress functionConst
json2cppJsonObjectMemberIterator operator++(int)
{
json2cppJsonObjectMemberIterator iterator_pre(m_itr);
++(*this);
return iterator_pre;
}

// cppcheck-suppress functionConst
// cppcheck-suppress functionConst
const json2cppJsonObjectMemberIterator &operator--()
{
--m_itr;
Expand Down
29 changes: 15 additions & 14 deletions src/json2cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ SOFTWARE.
*/



#include "json2cpp.hpp"
#include <fstream>

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

lines.push_back(fmt::format("inline constexpr std::array<value_pair_t, {}> object_data_{} = {{",
pairs.size(),
current_object_number));
lines.push_back(fmt::format(
"inline constexpr std::array<value_pair_t, {}> object_data_{} = {{", pairs.size(), current_object_number));

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


lines.push_back(fmt::format("inline constexpr std::array<json, {}> object_data_{} = {{{{",
entries.size(),
current_object_number));
lines.push_back(fmt::format(
"inline constexpr std::array<json, {}> object_data_{} = {{{{", entries.size(), current_object_number));

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

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

results.impl.push_back(
fmt::format(R"(
results.impl.push_back(fmt::format(R"(
namespace compiled_json::{}::impl {{

using json = json2cpp::basic_json<char>;
Expand All @@ -123,7 +119,8 @@ using array_t=json2cpp::basic_array_t<char>;
using object_t=json2cpp::basic_object_t<char>;
using value_pair_t=json2cpp::basic_value_pair_t<char>;

)", document_name));
)",
document_name));


const auto last_obj_name = compile(json, obj_count, results.impl);
Expand All @@ -136,7 +133,8 @@ inline constexpr auto document = json{{{{{}}}}};

#endif

)", last_obj_name));
)",
last_obj_name));


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

std::ofstream cpp(cpp_name);
cpp << fmt::format("#include \"{}\"\n", impl_name.filename().string());
cpp << fmt::format("namespace compiled_json::{} {{\nconst json2cpp::json &get() {{ return compiled_json::{}::impl::document; }}\n}}\n", document_name, document_name);
cpp << fmt::format(
"namespace compiled_json::{} {{\nconst json2cpp::json &get() {{ return compiled_json::{}::impl::document; }}\n}}\n",
document_name,
document_name);
}

void compile_to(const std::string_view document_name,
Expand Down
15 changes: 4 additions & 11 deletions src/schema_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ void walk_internal(std::int64_t &int_sum,
}
}

template<typename JSON>
void walk(const JSON &objects)
template<typename JSON> void walk(const JSON &objects)
{
std::int64_t int_sum{};
double double_sum{};
Expand All @@ -174,13 +173,7 @@ void walk(const JSON &objects)

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

walk_internal(int_sum,
double_sum,
string_sizes,
array_count,
object_count,
objects
);
walk_internal(int_sum, double_sum, string_sizes, array_count, object_count, objects);

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

if (args.at("--walk").asBool()) {
if (args.at("--walk").asBool()) {
if (args.at("--internal").asBool()) {
walk(compiled_json::energyplus_schema::get());
walk(compiled_json::energyplus_schema::get());
} else {
std::filesystem::path schema_file_name = args.at("<schema_file>").asString();
spdlog::info("Creating nlohmann::json object");
Expand Down
9 changes: 3 additions & 6 deletions test/valijson_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ TEST_CASE("Can load a valijson schema")
// Parse JSON schema content using valijson
Schema mySchema;
SchemaParser parser;
json2cppJsonAdapter mySchemaAdapter(
compiled_json::allof_integers_and_numbers_schema::get());
json2cppJsonAdapter mySchemaAdapter(compiled_json::allof_integers_and_numbers_schema::get());
CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema));
}

Expand All @@ -36,8 +35,7 @@ TEST_CASE("Validation fails where expected")
// Parse JSON schema content using valijson
Schema mySchema;
SchemaParser parser;
json2cppJsonAdapter mySchemaAdapter(
compiled_json::allof_integers_and_numbers_schema::get());
json2cppJsonAdapter mySchemaAdapter(compiled_json::allof_integers_and_numbers_schema::get());
CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema));

Validator validator;
Expand All @@ -58,8 +56,7 @@ TEST_CASE("Can validate a document")
// Parse JSON schema content using valijson
Schema mySchema;
SchemaParser parser;
json2cppJsonAdapter mySchemaAdapter(
compiled_json::allof_integers_and_numbers_schema::get());
json2cppJsonAdapter mySchemaAdapter(compiled_json::allof_integers_and_numbers_schema::get());
CHECK_NOTHROW(parser.populateSchema(mySchemaAdapter, mySchema));

Validator validator;
Expand Down