Add script to bundle library into a single header

This commit is contained in:
Tristan Penman 2022-04-09 13:45:59 +10:00
parent 21322b2d82
commit 1ff3625482
6 changed files with 10779 additions and 0 deletions

View File

@ -237,6 +237,10 @@ if(valijson_BUILD_EXAMPLES)
examples/remote_resolution_local_file.cpp
)
add_executable(valijson_nlohmann_bundled_test
examples/valijson_nlohmann_bundled_test.cpp
)
if(curlpp_FOUND)
include_directories(${curlpp_INCLUDE_DIR})

View File

@ -193,6 +193,23 @@ find_package(valijson REQUIRED)
add_executable(executable main.cpp)
target_link_libraries(executable valijson)
```
## Bundled Headers ##
An alternative way to include Valijson in your project is to generate a bundled header file, containing support for just one parser/adapter.
You can generate a header file using the `bundle.sh` script:
./bundle.sh nlohmann_json > valijson_nlohmann_bundled.hpp
This can then be used in your project with a single `#include`:
#include "valijson_nlohmann_bundled.hpp"
An example can be found in [examples/valijson_nlohmann_bundled_test.cpp](examples/valijson_nlohmann_bundled_test.cpp).
Note: the bundled version of Valijson always embeds a compatibility header in place of `std::optional`.
## Examples ##
Building the Valijson Test Suite, using the instructions above, will also compile two example applications: `custom_schema` and `external_schema`.

105
bundle.sh Executable file
View File

@ -0,0 +1,105 @@
#!/usr/bin/env bash
#
# Exit codes:
#
# 0 - success
# 64 - usage
# 65 - invalid adapter
#
set -euo pipefail
adapter_path=include/valijson/adapters
utils_path=include/valijson/utils
# find all available adapters
pushd "${adapter_path}" > /dev/null
adapters=($(ls *.hpp))
popd > /dev/null
# remove _adapter.hpp suffix
adapters=("${adapters[@]/_adapter.hpp/}")
usage() {
echo 'Generates a single header file for a particular Valijson adapter'
echo
echo 'This makes it easier to embed Valijson in smaller projects, where integrating a'
echo 'third-party dependency is inconvenient or undesirable.'
echo
echo 'Output is written to STDOUT.'
echo
echo 'Usage:'
echo
echo ' ./bundle.sh <adapter-prefix>'
echo
echo 'Example usage:'
echo
echo ' ./bundle.sh nlohmann_json > valijson_nlohmann_bundled.hpp'
echo
echo 'Available adapters:'
echo
for adapter in "${adapters[@]}"; do
echo " - $(basename ${adapter} _adapter.hpp)"
done
echo
exit 64
}
if [ $# -ne 1 ]; then
usage
fi
adapter_header=
for adapter in "${adapters[@]}"; do
if [ "${adapter}" == "$1" ]; then
adapter_header="${adapter_path}/${adapter}_adapter.hpp"
break
fi
done
if [ -z "${adapter_header}" ]; then
echo "Error: Adapter name is not valid."
exit 65
fi
common_headers=(
include/valijson/exceptions.hpp
include/compat/optional.hpp
include/valijson/internal/optional_bundled.hpp
include/valijson/internal/adapter.hpp
include/valijson/internal/basic_adapter.hpp
include/valijson/internal/custom_allocator.hpp
include/valijson/internal/debug.hpp
include/valijson/internal/frozen_value.hpp
include/valijson/internal/json_pointer.hpp
include/valijson/internal/json_reference.hpp
include/valijson/internal/uri.hpp
include/valijson/utils/file_utils.hpp
include/valijson/utils/utf8_utils.hpp
include/valijson/constraints/constraint.hpp
include/valijson/subschema.hpp
include/valijson/schema.hpp
include/valijson/constraints/constraint_visitor.hpp
include/valijson/constraints/basic_constraint.hpp
include/valijson/constraints/concrete_constraints.hpp
include/valijson/constraint_builder.hpp
include/valijson/schema_parser.hpp
include/valijson/adapters/std_string_adapter.hpp
include/valijson/validation_results.hpp
include/valijson/validation_visitor.hpp
include/valijson/validator.hpp)
# remove internal #includes
grep --no-filename -v "include <valijson/" ${common_headers[@]}
# std_string_adapter is always included
if [ "${adapter}" != "std_string" ]; then
grep --no-filename -v "include <valijson/" "${adapter_header}"
fi
# include file utils if available
utils_header="${utils_path}/${adapter}_utils.hpp"
if [ -f "${utils_header}" ]; then
grep --no-filename -v "include <valijson/" "${utils_header}"
fi

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,66 @@
#include <nlohmann/json.hpp>
#include "valijson_nlohmann_bundled.hpp"
using namespace std;
using namespace valijson;
using namespace valijson::adapters;
int main(int argc, char *argv[])
{
if (argc != 3) {
cerr << "Usage: " << argv[0] << " <schema document> <test/target document>" << endl;
return 1;
}
// Load the document containing the schema
nlohmann::json schemaDocument;
if (!valijson::utils::loadDocument(argv[1], schemaDocument)) {
cerr << "Failed to load schema document." << endl;
return 1;
}
// Load the document that is to be validated
nlohmann::json targetDocument;
if (!valijson::utils::loadDocument(argv[2], targetDocument)) {
cerr << "Failed to load target document." << endl;
return 1;
}
// Parse the json schema into an internal schema format
Schema schema;
SchemaParser parser;
NlohmannJsonAdapter schemaDocumentAdapter(schemaDocument);
try {
parser.populateSchema(schemaDocumentAdapter, schema);
} catch (std::exception &e) {
cerr << "Failed to parse schema: " << e.what() << endl;
return 1;
}
// Perform validation
Validator validator(Validator::kStrongTypes);
ValidationResults results;
NlohmannJsonAdapter targetDocumentAdapter(targetDocument);
if (!validator.validate(schema, targetDocumentAdapter, &results)) {
std::cerr << "Validation failed." << endl;
ValidationResults::Error error;
unsigned int errorNum = 1;
while (results.popError(error)) {
std::string context;
std::vector<std::string>::iterator itr = error.context.begin();
for (; itr != error.context.end(); itr++) {
context += *itr;
}
cerr << "Error #" << errorNum << std::endl
<< " context: " << context << endl
<< " desc: " << error.description << endl;
++errorNum;
}
return 1;
}
return 0;
}

View File

@ -0,0 +1,3 @@
#pragma once
namespace opt = std::experimental;