From 8a0b32f1cdc4c63fe605d17c073858f7a44eac7e Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Wed, 10 May 2023 10:51:46 +0200 Subject: [PATCH 1/6] Add simple CLI skeleton Signed-off-by: Cristian Le --- CMakeLists.txt | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cfce8e6..0caa835 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,12 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) GIT_TAG ${JSON_FETCH_VERSION} FIND_PACKAGE_ARGS ) - list(APPEND fetch_packages nlohmann_json) + FetchContent_Declare(CLI11 + GIT_REPOSITORY https://github.com/CLIUtils/CLI11 + GIT_TAG v2.3.2 + FIND_PACKAGE_ARGS + ) + list(APPEND fetch_packages nlohmann_json CLI11) else () # Try to get system installed version find_package(nlohmann_json QUIET) @@ -100,6 +105,15 @@ else () ) list(APPEND fetch_packages nlohmann_json) endif () + find_package(CLI11 QUIET) + if (NOT CLI11_FOUND) + # If failed fetch the desired version + FetchContent_Declare(CLI11 + GIT_REPOSITORY https://github.com/CLIUtils/CLI11 + GIT_TAG v2.3.2 + ) + list(APPEND fetch_packages nlohmann_json CLI11) + endif () endif () # Handle configure flags @@ -140,6 +154,12 @@ set_target_properties(nlohmann_json_schema_validator PROPERTIES OUTPUT_NAME nlohmann_json_validator ) +add_executable(nlohmann_json_schema_validator_cli) +set_target_properties(nlohmann_json_schema_validator_cli PROPERTIES + EXPORT_NAME cli + OUTPUT_NAME json-validator + ) + # Main definitions in here add_subdirectory(src) From 4e4c192c87cfcadd8b71bf1e708e2d93c5bcd5d1 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Wed, 10 May 2023 11:22:55 +0200 Subject: [PATCH 2/6] Add simple CLI skeleton implementation Signed-off-by: Cristian Le --- src/CMakeLists.txt | 6 +++++- src/cli.cpp | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/cli.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f895d66..a3e2dbc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,13 +6,17 @@ target_sources(nlohmann_json_schema_validator PRIVATE json-patch.cpp string-format-check.cpp ) +target_sources(nlohmann_json_schema_validator_cli PRIVATE + cli.cpp) + target_include_directories(nlohmann_json_schema_validator PUBLIC $ $ ) - set_target_properties(nlohmann_json_schema_validator PROPERTIES PUBLIC_HEADER nlohmann/json-schema.hpp) +target_link_libraries(nlohmann_json_schema_validator_cli PRIVATE + nlohmann_json_schema_validator CLI11::CLI11) # TODO: Why would this need to be if guarded? if (JSON_VALIDATOR_SHARED_LIBS) diff --git a/src/cli.cpp b/src/cli.cpp new file mode 100644 index 0000000..94519eb --- /dev/null +++ b/src/cli.cpp @@ -0,0 +1,17 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + CLI::App app{"Json schema validator", "json-validator"}; + // TODO: Move to a generated header file + app.set_version_flag("--version", "2.2.0"); + + try { + app.parse(argc, argv); + } catch (const CLI::ParseError &e) { + return app.exit(e); + } + return 0; +} From 8f2e050c5c52a3da8ff3d13d8cd7c15aa2082e7b Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Wed, 10 May 2023 14:20:51 +0200 Subject: [PATCH 3/6] Implement CLI Signed-off-by: Cristian Le --- src/cli.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/cli.cpp b/src/cli.cpp index 94519eb..e1af222 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -1,17 +1,39 @@ #include #include #include +#include + +#include "nlohmann/json-schema.hpp" + +using namespace nlohmann; +using namespace nlohmann::json_schema; int main(int argc, char *argv[]) { + std::ifstream schema_input; + std::ifstream object_input; + CLI::App app{"Json schema validator", "json-validator"}; // TODO: Move to a generated header file app.set_version_flag("--version", "2.2.0"); + app.add_option("schema", schema_input, "JSON schema of the object") + ->check(CLI::ExistingFile); + app.add_option("object", "JSON object to validate") + ->check(CLI::ExistingFile); try { app.parse(argc, argv); } catch (const CLI::ParseError &e) { return app.exit(e); } + + json schema{}; + json object{}; + if (!schema_input.good()) + throw std::invalid_argument("could not read schema"); + if (!object_input.good()) + throw std::invalid_argument("could not read object"); + schema_input >> schema; + object_input >> object; return 0; } From f543f18d1335d966a5eab7d22fcb17e2e09500f6 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Wed, 10 May 2023 18:08:58 +0200 Subject: [PATCH 4/6] Basic implementation Signed-off-by: Cristian Le --- src/cli.cpp | 57 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/src/cli.cpp b/src/cli.cpp index e1af222..5b11d96 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -8,18 +8,49 @@ using namespace nlohmann; using namespace nlohmann::json_schema; -int main(int argc, char *argv[]) +class main_cli : public CLI::App { std::ifstream schema_input; std::ifstream object_input; + // TODO: Export this as a built-in loader + static void loader(const json_uri &uri, json &schema) + { + std::string filename = "./" + uri.path(); + std::ifstream lf(filename); + if (!lf.good()) + throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename); + try { + lf >> schema; + } catch (const std::exception &e) { + throw e; + } + } + +public: + json schema; + json object; + json_validator validator; + main_cli() + : CLI::App{"Json schema validator", "json-validator"}, + validator{loader, default_string_format_check} + { + // TODO: Move to a generated header file + set_version_flag("--version", "2.2.0"); + add_option("schema", schema_input, "JSON schema of the object") + ->check(CLI::ExistingFile); + add_option("object", object_input, "JSON object to validate") + ->check(CLI::ExistingFile); + } + void validate() + { + validator.set_root_schema(schema); + validator.validate(object); + } +}; - CLI::App app{"Json schema validator", "json-validator"}; - // TODO: Move to a generated header file - app.set_version_flag("--version", "2.2.0"); - app.add_option("schema", schema_input, "JSON schema of the object") - ->check(CLI::ExistingFile); - app.add_option("object", "JSON object to validate") - ->check(CLI::ExistingFile); +int main(int argc, char *argv[]) +{ + main_cli app{}; try { app.parse(argc, argv); @@ -27,13 +58,7 @@ int main(int argc, char *argv[]) return app.exit(e); } - json schema{}; - json object{}; - if (!schema_input.good()) - throw std::invalid_argument("could not read schema"); - if (!object_input.good()) - throw std::invalid_argument("could not read object"); - schema_input >> schema; - object_input >> object; + app.validate(); + return 0; } From 60603f6c8d1cce643d40ba7d71dc68d82009db79 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Wed, 10 May 2023 18:18:13 +0200 Subject: [PATCH 5/6] Use relative path Signed-off-by: Cristian Le --- src/cli.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/cli.cpp b/src/cli.cpp index 5b11d96..9e7b820 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -11,16 +11,16 @@ using namespace nlohmann::json_schema; class main_cli : public CLI::App { std::ifstream schema_input; - std::ifstream object_input; + std::filesystem::path object_path; // TODO: Export this as a built-in loader - static void loader(const json_uri &uri, json &schema) + void loader(const json_uri &uri, json &sch) { - std::string filename = "./" + uri.path(); + std::string filename = object_path.parent_path().append(uri.path()); std::ifstream lf(filename); if (!lf.good()) throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename); try { - lf >> schema; + lf >> sch; } catch (const std::exception &e) { throw e; } @@ -32,13 +32,15 @@ class main_cli : public CLI::App json_validator validator; main_cli() : CLI::App{"Json schema validator", "json-validator"}, - validator{loader, default_string_format_check} + validator{ + [this](const json_uri &u, json &s) { this->loader(u, s); }, + default_string_format_check} { // TODO: Move to a generated header file set_version_flag("--version", "2.2.0"); add_option("schema", schema_input, "JSON schema of the object") ->check(CLI::ExistingFile); - add_option("object", object_input, "JSON object to validate") + add_option("object", object_path, "JSON object to validate") ->check(CLI::ExistingFile); } void validate() From ff35afd5a5d4fa1e67a377983f6a87b5852ac6ba Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Wed, 10 May 2023 18:49:50 +0200 Subject: [PATCH 6/6] Add appropriate project version evaluation Signed-off-by: Cristian Le --- src/CMakeLists.txt | 4 +++- src/cli.cpp | 7 ++++--- src/nlohmann/{json-schema.hpp => json-schema.hpp.in} | 2 ++ 3 files changed, 9 insertions(+), 4 deletions(-) rename src/nlohmann/{json-schema.hpp => json-schema.hpp.in} (98%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a3e2dbc..8387d2d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,13 +8,15 @@ target_sources(nlohmann_json_schema_validator PRIVATE ) target_sources(nlohmann_json_schema_validator_cli PRIVATE cli.cpp) +configure_file(nlohmann/json-schema.hpp.in nlohmann/json-schema.hpp) target_include_directories(nlohmann_json_schema_validator PUBLIC $ $ + $ ) set_target_properties(nlohmann_json_schema_validator PROPERTIES - PUBLIC_HEADER nlohmann/json-schema.hpp) + PUBLIC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/nlohmann/json-schema.hpp) target_link_libraries(nlohmann_json_schema_validator_cli PRIVATE nlohmann_json_schema_validator CLI11::CLI11) diff --git a/src/cli.cpp b/src/cli.cpp index 9e7b820..ac08681 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -10,6 +10,7 @@ using namespace nlohmann::json_schema; class main_cli : public CLI::App { + std::string version; std::ifstream schema_input; std::filesystem::path object_path; // TODO: Export this as a built-in loader @@ -34,10 +35,10 @@ class main_cli : public CLI::App : CLI::App{"Json schema validator", "json-validator"}, validator{ [this](const json_uri &u, json &s) { this->loader(u, s); }, - default_string_format_check} + default_string_format_check}, + version{nlohmann::json_schema::version} { - // TODO: Move to a generated header file - set_version_flag("--version", "2.2.0"); + set_version_flag("--version", version); add_option("schema", schema_input, "JSON schema of the object") ->check(CLI::ExistingFile); add_option("object", object_path, "JSON object to validate") diff --git a/src/nlohmann/json-schema.hpp b/src/nlohmann/json-schema.hpp.in similarity index 98% rename from src/nlohmann/json-schema.hpp rename to src/nlohmann/json-schema.hpp.in index 07befd3..d1695d6 100644 --- a/src/nlohmann/json-schema.hpp +++ b/src/nlohmann/json-schema.hpp.in @@ -128,6 +128,8 @@ class JSON_SCHEMA_VALIDATOR_API json_uri namespace json_schema { +constexpr std::string_view version = "@PROJECT_VERSION@"; + extern json draft7_schema_builtin; typedef std::function schema_loader;