Skip to content
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
23 changes: 16 additions & 7 deletions src/command_compile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,41 @@ auto sourcemeta::jsonschema::compile(const sourcemeta::core::Options &options)
sourcemeta::core::stringify(template_json, json_stream);
const auto json_data{std::move(json_stream).str()};

constexpr auto BYTES_PER_LINE{16};
constexpr auto BYTES_PER_LINE{12};

std::cout << "#ifndef SOURCEMETA_JSONSCHEMA_INCLUDE_" << name << "_H_\n";
std::cout << "#define SOURCEMETA_JSONSCHEMA_INCLUDE_" << name << "_H_\n";
std::cout << "\n";
std::cout << "#ifdef __cplusplus\n";
std::cout << "#include <cstddef>\n";
std::cout << "#include <string_view>\n";
std::cout << "#endif\n";
std::cout << "\n";
std::cout << "static const char " << name << "_DATA[] =";
std::cout << "static const char " << name << "_DATA[] = {";

for (std::size_t index = 0; index < json_data.size(); ++index) {
if (index % BYTES_PER_LINE == 0) {
std::cout << "\n \"";
std::cout << "\n ";
}

std::cout << "\\x" << std::hex << std::setw(2) << std::setfill('0')
std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0')
<< (static_cast<unsigned int>(
static_cast<unsigned char>(json_data[index])));

if ((index + 1) % BYTES_PER_LINE == 0 || index + 1 == json_data.size()) {
std::cout << "\"";
std::cout << ",";
if ((index + 1) % BYTES_PER_LINE != 0) {
std::cout << " ";
}
}

std::cout << ";\n";
if (json_data.size() % BYTES_PER_LINE != 0) {
std::cout << "0x00";
} else {
std::cout << "\n 0x00";
}

std::cout << "\n};\n";
std::cout << "\n";
std::cout << std::dec;
std::cout << "static const unsigned int " << name
<< "_LENGTH = " << json_data.size() << ";\n";
Expand Down
4 changes: 2 additions & 2 deletions test/ci/fail_bundle_http_non_schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cat << 'EOF' > "$TMP/schema.json"
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test",
"description": "Test schema",
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema" } ]
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema" } ]
}
EOF

Expand All @@ -22,7 +22,7 @@ test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
error: The JSON document is not a valid JSON Schema
at identifier https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
at identifier https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
at file path $(realpath "$TMP")/schema.json
at location "/allOf/0/\$ref"
EOF
Expand Down
6 changes: 3 additions & 3 deletions test/ci/fail_bundle_http_non_schema_verbose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cat << 'EOF' > "$TMP/schema.json"
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test",
"description": "Test schema",
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema" } ]
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema" } ]
}
EOF

Expand All @@ -21,9 +21,9 @@ EOF
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Resolving over HTTP: https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
Resolving over HTTP: https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
error: The JSON document is not a valid JSON Schema
at identifier https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
at identifier https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
at file path $(realpath "$TMP")/schema.json
at location "/allOf/0/\$ref"
EOF
Expand Down
4 changes: 2 additions & 2 deletions test/ci/fail_validate_http_non_schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cat << 'EOF' > "$TMP/schema.json"
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test",
"description": "Test schema",
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema" } ]
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema" } ]
}
EOF

Expand All @@ -26,7 +26,7 @@ test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
error: The JSON document is not a valid JSON Schema
at identifier https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
at identifier https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
at file path $(realpath "$TMP")/schema.json
at location "/allOf/0/\$ref"
EOF
Expand Down
6 changes: 3 additions & 3 deletions test/ci/fail_validate_http_non_schema_verbose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cat << 'EOF' > "$TMP/schema.json"
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test",
"description": "Test schema",
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema" } ]
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema" } ]
}
EOF

Expand All @@ -25,9 +25,9 @@ EOF
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
Resolving over HTTP: https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
Resolving over HTTP: https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
error: The JSON document is not a valid JSON Schema
at identifier https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
at identifier https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
at file path $(realpath "$TMP")/schema.json
at location "/allOf/0/\$ref"
EOF
Expand Down
17 changes: 11 additions & 6 deletions test/compile/pass_include.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ cat << 'EOF' > "$TMP/expected.h"
#define SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_

#ifdef __cplusplus
#include <cstddef>
#include <string_view>
#endif

static const char TEST_SCHEMA_DATA[] =
"\x5b\x66\x61\x6c\x73\x65\x2c\x74\x72\x75\x65\x2c\x5b\x22\x22\x2c"
"\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\x6c\x65"
"\x2e\x63\x6f\x6d\x22\x5d\x2c\x5b\x5b\x31\x31\x2c\x22\x2f\x74\x79"
"\x70\x65\x22\x2c\x22\x22\x2c\x22\x23\x2f\x74\x79\x70\x65\x22\x2c"
"\x32\x2c\x5b\x38\x2c\x34\x5d\x5d\x5d\x5d";
static const char TEST_SCHEMA_DATA[] = {
0x5b, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x74, 0x72, 0x75, 0x65, 0x2c,
0x5b, 0x22, 0x22, 0x2c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
0x22, 0x5d, 0x2c, 0x5b, 0x5b, 0x31, 0x31, 0x2c, 0x22, 0x2f, 0x74, 0x79,
0x70, 0x65, 0x22, 0x2c, 0x22, 0x22, 0x2c, 0x22, 0x23, 0x2f, 0x74, 0x79,
0x70, 0x65, 0x22, 0x2c, 0x32, 0x2c, 0x5b, 0x38, 0x2c, 0x34, 0x5d, 0x5d,
0x5d, 0x5d, 0x00
};

static const unsigned int TEST_SCHEMA_LENGTH = 74;

#ifdef __cplusplus
Expand Down
17 changes: 11 additions & 6 deletions test/compile/pass_include_lowercase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ cat << 'EOF' > "$TMP/expected.h"
#define SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_

#ifdef __cplusplus
#include <cstddef>
#include <string_view>
#endif

static const char TEST_SCHEMA_DATA[] =
"\x5b\x66\x61\x6c\x73\x65\x2c\x74\x72\x75\x65\x2c\x5b\x22\x22\x2c"
"\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\x6c\x65"
"\x2e\x63\x6f\x6d\x22\x5d\x2c\x5b\x5b\x31\x31\x2c\x22\x2f\x74\x79"
"\x70\x65\x22\x2c\x22\x22\x2c\x22\x23\x2f\x74\x79\x70\x65\x22\x2c"
"\x32\x2c\x5b\x38\x2c\x34\x5d\x5d\x5d\x5d";
static const char TEST_SCHEMA_DATA[] = {
0x5b, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x74, 0x72, 0x75, 0x65, 0x2c,
0x5b, 0x22, 0x22, 0x2c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
0x22, 0x5d, 0x2c, 0x5b, 0x5b, 0x31, 0x31, 0x2c, 0x22, 0x2f, 0x74, 0x79,
0x70, 0x65, 0x22, 0x2c, 0x22, 0x22, 0x2c, 0x22, 0x23, 0x2f, 0x74, 0x79,
0x70, 0x65, 0x22, 0x2c, 0x32, 0x2c, 0x5b, 0x38, 0x2c, 0x34, 0x5d, 0x5d,
0x5d, 0x5d, 0x00
};

static const unsigned int TEST_SCHEMA_LENGTH = 74;

#ifdef __cplusplus
Expand Down
17 changes: 11 additions & 6 deletions test/compile/pass_include_short.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ cat << 'EOF' > "$TMP/expected.h"
#define SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_

#ifdef __cplusplus
#include <cstddef>
#include <string_view>
#endif

static const char TEST_SCHEMA_DATA[] =
"\x5b\x66\x61\x6c\x73\x65\x2c\x74\x72\x75\x65\x2c\x5b\x22\x22\x2c"
"\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\x6c\x65"
"\x2e\x63\x6f\x6d\x22\x5d\x2c\x5b\x5b\x31\x31\x2c\x22\x2f\x74\x79"
"\x70\x65\x22\x2c\x22\x22\x2c\x22\x23\x2f\x74\x79\x70\x65\x22\x2c"
"\x32\x2c\x5b\x38\x2c\x34\x5d\x5d\x5d\x5d";
static const char TEST_SCHEMA_DATA[] = {
0x5b, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x74, 0x72, 0x75, 0x65, 0x2c,
0x5b, 0x22, 0x22, 0x2c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
0x22, 0x5d, 0x2c, 0x5b, 0x5b, 0x31, 0x31, 0x2c, 0x22, 0x2f, 0x74, 0x79,
0x70, 0x65, 0x22, 0x2c, 0x22, 0x22, 0x2c, 0x22, 0x23, 0x2f, 0x74, 0x79,
0x70, 0x65, 0x22, 0x2c, 0x32, 0x2c, 0x5b, 0x38, 0x2c, 0x34, 0x5d, 0x5d,
0x5d, 0x5d, 0x00
};

static const unsigned int TEST_SCHEMA_LENGTH = 74;

#ifdef __cplusplus
Expand Down