Skip to content

Commit 2e6a3db

Browse files
committed
Use an array of bytes for compile --include
To address compiler string literal maximum interoperability lengths. Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent e4182ea commit 2e6a3db

8 files changed

+59
-35
lines changed

src/command_compile.cc

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,41 @@ auto sourcemeta::jsonschema::compile(const sourcemeta::core::Options &options)
8787
sourcemeta::core::stringify(template_json, json_stream);
8888
const auto json_data{std::move(json_stream).str()};
8989

90-
constexpr auto BYTES_PER_LINE{16};
90+
constexpr auto BYTES_PER_LINE{12};
9191

9292
std::cout << "#ifndef SOURCEMETA_JSONSCHEMA_INCLUDE_" << name << "_H_\n";
9393
std::cout << "#define SOURCEMETA_JSONSCHEMA_INCLUDE_" << name << "_H_\n";
9494
std::cout << "\n";
9595
std::cout << "#ifdef __cplusplus\n";
96+
std::cout << "#include <cstddef>\n";
9697
std::cout << "#include <string_view>\n";
9798
std::cout << "#endif\n";
9899
std::cout << "\n";
99-
std::cout << "static const char " << name << "_DATA[] =";
100+
std::cout << "static const char " << name << "_DATA[] = {";
100101

101102
for (std::size_t index = 0; index < json_data.size(); ++index) {
102103
if (index % BYTES_PER_LINE == 0) {
103-
std::cout << "\n \"";
104+
std::cout << "\n ";
104105
}
105106

106-
std::cout << "\\x" << std::hex << std::setw(2) << std::setfill('0')
107+
std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0')
107108
<< (static_cast<unsigned int>(
108109
static_cast<unsigned char>(json_data[index])));
109110

110-
if ((index + 1) % BYTES_PER_LINE == 0 || index + 1 == json_data.size()) {
111-
std::cout << "\"";
111+
std::cout << ",";
112+
if ((index + 1) % BYTES_PER_LINE != 0) {
113+
std::cout << " ";
112114
}
113115
}
114116

115-
std::cout << ";\n";
117+
if (json_data.size() % BYTES_PER_LINE != 0) {
118+
std::cout << "0x00";
119+
} else {
120+
std::cout << "\n 0x00";
121+
}
122+
123+
std::cout << "\n};\n";
124+
std::cout << "\n";
116125
std::cout << std::dec;
117126
std::cout << "static const unsigned int " << name
118127
<< "_LENGTH = " << json_data.size() << ";\n";

test/ci/fail_bundle_http_non_schema.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cat << 'EOF' > "$TMP/schema.json"
1212
"$schema": "http://json-schema.org/draft-07/schema#",
1313
"title": "Test",
1414
"description": "Test schema",
15-
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema" } ]
15+
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema" } ]
1616
}
1717
EOF
1818

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

2323
cat << EOF > "$TMP/expected.txt"
2424
error: The JSON document is not a valid JSON Schema
25-
at identifier https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
25+
at identifier https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
2626
at file path $(realpath "$TMP")/schema.json
2727
at location "/allOf/0/\$ref"
2828
EOF

test/ci/fail_bundle_http_non_schema_verbose.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cat << 'EOF' > "$TMP/schema.json"
1212
"$schema": "http://json-schema.org/draft-07/schema#",
1313
"title": "Test",
1414
"description": "Test schema",
15-
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema" } ]
15+
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema" } ]
1616
}
1717
EOF
1818

@@ -21,9 +21,9 @@ EOF
2121
test "$CODE" = "1" || exit 1
2222

2323
cat << EOF > "$TMP/expected.txt"
24-
Resolving over HTTP: https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
24+
Resolving over HTTP: https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
2525
error: The JSON document is not a valid JSON Schema
26-
at identifier https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
26+
at identifier https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
2727
at file path $(realpath "$TMP")/schema.json
2828
at location "/allOf/0/\$ref"
2929
EOF

test/ci/fail_validate_http_non_schema.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cat << 'EOF' > "$TMP/schema.json"
1212
"$schema": "http://json-schema.org/draft-07/schema#",
1313
"title": "Test",
1414
"description": "Test schema",
15-
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema" } ]
15+
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema" } ]
1616
}
1717
EOF
1818

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

2727
cat << EOF > "$TMP/expected.txt"
2828
error: The JSON document is not a valid JSON Schema
29-
at identifier https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
29+
at identifier https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
3030
at file path $(realpath "$TMP")/schema.json
3131
at location "/allOf/0/\$ref"
3232
EOF

test/ci/fail_validate_http_non_schema_verbose.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cat << 'EOF' > "$TMP/schema.json"
1212
"$schema": "http://json-schema.org/draft-07/schema#",
1313
"title": "Test",
1414
"description": "Test schema",
15-
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema" } ]
15+
"allOf": [ { "$ref": "https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema" } ]
1616
}
1717
EOF
1818

@@ -25,9 +25,9 @@ EOF
2525
test "$CODE" = "1" || exit 1
2626

2727
cat << EOF > "$TMP/expected.txt"
28-
Resolving over HTTP: https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
28+
Resolving over HTTP: https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
2929
error: The JSON document is not a valid JSON Schema
30-
at identifier https://schemas.sourcemeta.com/self/api/schemas/stats/jsonschema/2020-12/schema
30+
at identifier https://schemas.sourcemeta.com/self/v1/api/schemas/stats/jsonschema/2020-12/schema
3131
at file path $(realpath "$TMP")/schema.json
3232
at location "/allOf/0/\$ref"
3333
EOF

test/compile/pass_include.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@ cat << 'EOF' > "$TMP/expected.h"
2222
#define SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_
2323
2424
#ifdef __cplusplus
25+
#include <cstddef>
2526
#include <string_view>
2627
#endif
2728
28-
static const char TEST_SCHEMA_DATA[] =
29-
"\x5b\x66\x61\x6c\x73\x65\x2c\x74\x72\x75\x65\x2c\x5b\x22\x22\x2c"
30-
"\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\x6c\x65"
31-
"\x2e\x63\x6f\x6d\x22\x5d\x2c\x5b\x5b\x31\x31\x2c\x22\x2f\x74\x79"
32-
"\x70\x65\x22\x2c\x22\x22\x2c\x22\x23\x2f\x74\x79\x70\x65\x22\x2c"
33-
"\x32\x2c\x5b\x38\x2c\x34\x5d\x5d\x5d\x5d";
29+
static const char TEST_SCHEMA_DATA[] = {
30+
0x5b, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x74, 0x72, 0x75, 0x65, 0x2c,
31+
0x5b, 0x22, 0x22, 0x2c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
32+
0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
33+
0x22, 0x5d, 0x2c, 0x5b, 0x5b, 0x31, 0x31, 0x2c, 0x22, 0x2f, 0x74, 0x79,
34+
0x70, 0x65, 0x22, 0x2c, 0x22, 0x22, 0x2c, 0x22, 0x23, 0x2f, 0x74, 0x79,
35+
0x70, 0x65, 0x22, 0x2c, 0x32, 0x2c, 0x5b, 0x38, 0x2c, 0x34, 0x5d, 0x5d,
36+
0x5d, 0x5d, 0x00
37+
};
38+
3439
static const unsigned int TEST_SCHEMA_LENGTH = 74;
3540
3641
#ifdef __cplusplus

test/compile/pass_include_lowercase.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@ cat << 'EOF' > "$TMP/expected.h"
2222
#define SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_
2323
2424
#ifdef __cplusplus
25+
#include <cstddef>
2526
#include <string_view>
2627
#endif
2728
28-
static const char TEST_SCHEMA_DATA[] =
29-
"\x5b\x66\x61\x6c\x73\x65\x2c\x74\x72\x75\x65\x2c\x5b\x22\x22\x2c"
30-
"\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\x6c\x65"
31-
"\x2e\x63\x6f\x6d\x22\x5d\x2c\x5b\x5b\x31\x31\x2c\x22\x2f\x74\x79"
32-
"\x70\x65\x22\x2c\x22\x22\x2c\x22\x23\x2f\x74\x79\x70\x65\x22\x2c"
33-
"\x32\x2c\x5b\x38\x2c\x34\x5d\x5d\x5d\x5d";
29+
static const char TEST_SCHEMA_DATA[] = {
30+
0x5b, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x74, 0x72, 0x75, 0x65, 0x2c,
31+
0x5b, 0x22, 0x22, 0x2c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
32+
0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
33+
0x22, 0x5d, 0x2c, 0x5b, 0x5b, 0x31, 0x31, 0x2c, 0x22, 0x2f, 0x74, 0x79,
34+
0x70, 0x65, 0x22, 0x2c, 0x22, 0x22, 0x2c, 0x22, 0x23, 0x2f, 0x74, 0x79,
35+
0x70, 0x65, 0x22, 0x2c, 0x32, 0x2c, 0x5b, 0x38, 0x2c, 0x34, 0x5d, 0x5d,
36+
0x5d, 0x5d, 0x00
37+
};
38+
3439
static const unsigned int TEST_SCHEMA_LENGTH = 74;
3540
3641
#ifdef __cplusplus

test/compile/pass_include_short.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ cat << 'EOF' > "$TMP/expected.h"
2323
#define SOURCEMETA_JSONSCHEMA_INCLUDE_TEST_SCHEMA_H_
2424
2525
#ifdef __cplusplus
26+
#include <cstddef>
2627
#include <string_view>
2728
#endif
2829
29-
static const char TEST_SCHEMA_DATA[] =
30-
"\x5b\x66\x61\x6c\x73\x65\x2c\x74\x72\x75\x65\x2c\x5b\x22\x22\x2c"
31-
"\x22\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\x6c\x65"
32-
"\x2e\x63\x6f\x6d\x22\x5d\x2c\x5b\x5b\x31\x31\x2c\x22\x2f\x74\x79"
33-
"\x70\x65\x22\x2c\x22\x22\x2c\x22\x23\x2f\x74\x79\x70\x65\x22\x2c"
34-
"\x32\x2c\x5b\x38\x2c\x34\x5d\x5d\x5d\x5d";
30+
static const char TEST_SCHEMA_DATA[] = {
31+
0x5b, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x74, 0x72, 0x75, 0x65, 0x2c,
32+
0x5b, 0x22, 0x22, 0x2c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
33+
0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
34+
0x22, 0x5d, 0x2c, 0x5b, 0x5b, 0x31, 0x31, 0x2c, 0x22, 0x2f, 0x74, 0x79,
35+
0x70, 0x65, 0x22, 0x2c, 0x22, 0x22, 0x2c, 0x22, 0x23, 0x2f, 0x74, 0x79,
36+
0x70, 0x65, 0x22, 0x2c, 0x32, 0x2c, 0x5b, 0x38, 0x2c, 0x34, 0x5d, 0x5d,
37+
0x5d, 0x5d, 0x00
38+
};
39+
3540
static const unsigned int TEST_SCHEMA_LENGTH = 74;
3641
3742
#ifdef __cplusplus

0 commit comments

Comments
 (0)