./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
146 / 147
99.3%
Branches:
195 / 207
94.2%
Functions:
18 / 18
100.0%

src/config/json.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "config/json_io.h"
4
5 #include "terraingen/sources/upsampling.h"
6
7 #include <array>
8 #include <cmath>
9 #include <cstdint>
10 #include <filesystem>
11 #include <fstream>
12 #include <limits>
13 #include <sstream>
14 #include <string>
15
16 namespace terraingen {
17
18 namespace config {
19
20
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5762 times.
5767 Reader::Reader(const Json& json, std::string path) : m_json(json), m_path(std::move(path)) {
21
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5762 times.
5767 if (!m_json.is_object()) {
22
3/3
✓ Branch 2 taken 5 times.
✓ Branch 5 taken 5 times.
✓ Branch 8 taken 5 times.
15 throw ConfigError(m_path, std::string("expected an object, not ") + m_json.type_name());
23 }
24 5772 }
25
26 5577 void Reader::Finish() const {
27
5/5
✓ Branch 3 taken 30325 times.
✓ Branch 6 taken 30325 times.
✓ Branch 9 taken 35848 times.
✓ Branch 11 taken 30325 times.
✓ Branch 12 taken 5523 times.
35848 for (const auto& [key, value] : m_json.items()) {
28 60596 if (m_known.count(key) == 0) {
29 54 std::string expected;
30
2/2
✓ Branch 0 taken 678 times.
✓ Branch 1 taken 54 times.
732 for (const std::string& name : m_known) {
31
3/3
✓ Branch 0 taken 624 times.
✓ Branch 1 taken 54 times.
✓ Branch 3 taken 678 times.
2034 expected += (expected.empty() ? "" : ", ") + name;
32 }
33
3/3
✓ Branch 2 taken 54 times.
✓ Branch 5 taken 54 times.
✓ Branch 8 taken 54 times.
108 throw ConfigError(FieldPath(m_path, key), "unknown key; expected one of: " + expected);
34 54 }
35 5631 }
36
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5519 times.
5523 if (!m_missing.empty()) {
37
3/3
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 4 times.
✓ Branch 8 taken 4 times.
8 throw ConfigError(FieldPath(m_path, m_missing.front()), "required");
38 }
39 5519 }
40
41 15323 void Read(const Json& json, const std::string& path, double& value) {
42
2/2
✓ Branch 0 taken 14653 times.
✓ Branch 1 taken 670 times.
15323 if (!json.is_number()) {
43
3/3
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 4 times.
✓ Branch 8 taken 4 times.
12 throw ConfigError(path, std::string("expected a number, not ") + json.type_name());
44 }
45 15319 value = json.get<double>();
46
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15319 times.
15319 if (!std::isfinite(value)) {
47
0/3
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ throw ConfigError(path, "expected a finite number");
48 }
49 15319 }
50
51 336 void Read(const Json& json, const std::string& path, float& value) {
52 336 double number = 0.0;
53 336 Read(json, path, number);
54
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 335 times.
336 if (std::fabs(number) > static_cast<double>(std::numeric_limits<float>::max())) {
55
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 throw ConfigError(path, "number is too large");
56 }
57 335 value = static_cast<float>(number);
58 335 }
59
60 894 void Read(const Json& json, const std::string& path, bool& value) {
61
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 893 times.
894 if (!json.is_boolean()) {
62
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
3 throw ConfigError(path, std::string("expected true or false, not ") + json.type_name());
63 }
64 893 value = json.get<bool>();
65 893 }
66
67 2796 void Read(const Json& json, const std::string& path, std::string& value) {
68
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2795 times.
2796 if (!json.is_string()) {
69
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
3 throw ConfigError(path, std::string("expected a string, not ") + json.type_name());
70 }
71 2795 value = json.get<std::string>();
72 2795 }
73
74 2371 void Read(const Json& json, const std::string& path, int& value) {
75
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2369 times.
2371 if (!json.is_number_integer()) {
76
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
6 throw ConfigError(path, std::string("expected an integer, not ") + json.type_name());
77 }
78
2/2
✓ Branch 0 taken 2368 times.
✓ Branch 1 taken 1 time.
2369 if (json.is_number_unsigned() &&
79
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2367 times.
2368 json.get<std::uint64_t>() > static_cast<std::uint64_t>(std::numeric_limits<int>::max())) {
80
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 throw ConfigError(path, "integer is too large");
81 }
82 2368 const auto number = json.get<std::int64_t>();
83
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2367 times.
2368 if (number < std::numeric_limits<int>::min() || number > std::numeric_limits<int>::max()) {
84
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 throw ConfigError(path, "integer is out of range");
85 }
86 2367 value = static_cast<int>(number);
87 2367 }
88
89 544 void Read(const Json& json, const std::string& path, std::array<double, 2>& value) {
90
4/4
✓ Branch 0 taken 542 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 539 times.
544 if (!json.is_array() || json.size() != 2) {
91
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
10 throw ConfigError(path, std::string("expected a list of two numbers, not ") +
92
7/9
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
✓ Branch 9 taken 2 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
13 (json.is_array() ? "a list of " + std::to_string(json.size())
93
4/4
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
20 : std::string(json.type_name())));
94 }
95 539 std::array<double, 2> pair{};
96
2/2
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 537 times.
1614 for (std::size_t i = 0; i < 2; ++i) {
97
2/2
✓ Branch 2 taken 1077 times.
✓ Branch 5 taken 1075 times.
2152 Read(json[i], ItemPath(path, i), pair[i]);
98 }
99 537 value = pair;
100 537 }
101
102 44 void Read(const Json& json, const std::string& path, UpsamplingSpec& value) {
103 44 UpsamplingSpec spec;
104
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 20 times.
44 if (json.is_string()) {
105
1/1
✓ Branch 1 taken 24 times.
24 spec.type = json.get<std::string>();
106
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 23 times.
24 if (spec.type.empty()) {
107
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 throw ConfigError(path, "must not be empty");
108 }
109 23 value = std::move(spec);
110 23 return;
111 }
112
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if (!json.is_object()) {
113 2 throw ConfigError(
114
1/1
✓ Branch 1 taken 2 times.
4 path, std::string("expected an upsampling name or an object with a \"type\", not ") +
115
2/2
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
8 json.type_name());
116 }
117 18 const auto type = json.find("type");
118
10/12
✓ Branch 2 taken 18 times.
✓ Branch 4 taken 17 times.
✓ Branch 5 taken 1 time.
✓ Branch 7 taken 17 times.
✓ Branch 9 taken 17 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 17 times.
✓ Branch 15 taken 17 times.
✓ Branch 17 taken 17 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 time.
✓ Branch 20 taken 17 times.
53 if (type == json.end() || !type->is_string() || type->get<std::string>().empty()) {
119
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
2 throw ConfigError(FieldPath(path, "type"), "expected an upsampling name");
120 }
121
2/2
✓ Branch 1 taken 17 times.
✓ Branch 4 taken 17 times.
17 spec.type = type->get<std::string>();
122
3/3
✓ Branch 1 taken 17 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 15 times.
17 if (KernelUpsamplerMethod(spec.type)) {
123
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
9 Reader reader(json, path);
124
1/1
✓ Branch 1 taken 2 times.
2 reader.Accept("type");
125
1/1
✓ Branch 1 taken 1 time.
2 reader.Finish();
126
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 4 times.
17 } else if (spec.type == "diamond_square") {
127
1/1
✓ Branch 1 taken 9 times.
11 ReadObject(json, path, spec.diamondSquare, true);
128 } else {
129
1/1
✓ Branch 1 taken 4 times.
4 Json settings = json;
130
1/1
✓ Branch 1 taken 4 times.
4 settings.erase("type");
131
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!settings.empty()) {
132
1/1
✓ Branch 1 taken 4 times.
4 spec.settings = settings.dump();
133 }
134 4 }
135 14 value = std::move(spec);
136 37 }
137
138 4014 Json TypedJson(std::string_view type, const Json& fields) {
139 4014 Json json = Json::object();
140
3/3
✓ Branch 1 taken 4014 times.
✓ Branch 4 taken 4014 times.
✓ Branch 7 taken 4014 times.
8028 json["type"] = std::string(type);
141
3/3
✓ Branch 3 taken 23973 times.
✓ Branch 5 taken 19959 times.
✓ Branch 6 taken 4014 times.
23973 for (const auto& item : fields.items()) {
142
5/5
✓ Branch 1 taken 19959 times.
✓ Branch 4 taken 19959 times.
✓ Branch 7 taken 19959 times.
✓ Branch 10 taken 19959 times.
✓ Branch 13 taken 19959 times.
19959 json[item.key()] = item.value();
143 4014 }
144 4014 return json;
145 }
146
147 190 Json ToJson(const UpsamplingSpec& value, bool compact) {
148
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 164 times.
190 if (value.type == "diamond_square") {
149
1/1
✓ Branch 2 taken 26 times.
26 return TypedJson(value.type, FieldsJson(value.diamondSquare, false, compact));
150 }
151
2/2
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 7 times.
164 if (value.settings.empty()) {
152 157 return value.type;
153 }
154
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 return TypedJson(value.type, Json::parse(value.settings));
155 }
156
157 namespace {
158
159 /// Checks a configuration's "schema_version" before any other key, which a newer version may add.
160 /// @throws ConfigError at "schema_version" when the object `json` has none, or another version
161 /// than this library reads.
162 463 void CheckSchemaVersion(const Json& json) {
163
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 461 times.
463 if (!json.is_object()) {
164 2 return;
165 }
166 461 const std::string supported =
167
1/1
✓ Branch 2 taken 461 times.
461 "this library reads version " + std::to_string(kConfigSchemaVersion);
168 461 const auto found = json.find("schema_version");
169
3/3
✓ Branch 2 taken 461 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 459 times.
922 if (found == json.end()) {
170
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
4 throw ConfigError("schema_version", "required; " + supported);
171 }
172 459 int version = 0;
173
5/5
✓ Branch 1 taken 459 times.
✓ Branch 4 taken 459 times.
✓ Branch 7 taken 456 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 452 times.
924 Read(*found, "schema_version", version);
174
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 452 times.
456 if (version != kConfigSchemaVersion) {
175 4 throw ConfigError("schema_version",
176
4/4
✓ Branch 3 taken 4 times.
✓ Branch 6 taken 4 times.
✓ Branch 9 taken 4 times.
✓ Branch 12 taken 4 times.
12 "version " + std::to_string(version) + " is not supported; " + supported);
177 }
178 452 }
179
180 } // namespace
181
182 } // namespace config
183
184 440 ConfigError::ConfigError(std::string path, const std::string& message)
185
6/8
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 433 times.
✓ Branch 5 taken 433 times.
✓ Branch 8 taken 440 times.
✓ Branch 10 taken 433 times.
✓ Branch 11 taken 7 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
1313 : std::runtime_error(path.empty() ? message : path + ": " + message), m_path(std::move(path)) {}
186
187 465 TerrainGenConfig ParseConfig(std::string_view json) {
188 465 config::Json document;
189 465 try {
190
1/1
✓ Branch 1 taken 463 times.
467 document = config::Json::parse(json);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } catch (const config::Json::exception& error) {
192
3/3
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
6 throw ConfigError("", std::string("invalid JSON: ") + error.what());
193 2 }
194
1/1
✓ Branch 1 taken 454 times.
463 config::CheckSchemaVersion(document);
195 454 ConfigSpec parsed;
196
2/2
✓ Branch 1 taken 454 times.
✓ Branch 4 taken 337 times.
908 config::Read(document, "", parsed);
197
1/1
✓ Branch 2 taken 337 times.
674 return parsed;
198 582 }
199
200 18 TerrainGenConfig LoadConfig(const std::filesystem::path& path) {
201 18 std::ifstream file(path, std::ios::binary);
202
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 15 times.
18 if (!file) {
203
4/4
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 11 taken 3 times.
6 throw ConfigError("", "cannot read " + path.string());
204 }
205
1/1
✓ Branch 1 taken 15 times.
15 std::ostringstream text;
206
1/1
✓ Branch 1 taken 15 times.
15 text << file.rdbuf();
207
3/3
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 14 times.
44 ConfigSpec config = ParseConfig(text.str());
208
1/1
✓ Branch 1 taken 14 times.
14 config.directory = path.parent_path();
209
1/1
✓ Branch 2 taken 14 times.
28 return config;
210 19 }
211
212 107 std::string ConfigToJson(const TerrainGenConfig& terrainConfig, int indent) {
213
1/1
✓ Branch 3 taken 107 times.
214 return config::FieldsJson(terrainConfig.Spec(), true, true).dump(indent) + "\n";
214 }
215
216 74 bool operator==(const TerrainGenConfig& a, const TerrainGenConfig& b) {
217
2/2
✓ Branch 3 taken 74 times.
✓ Branch 6 taken 74 times.
74 return config::FieldsJson(a.Spec()) == config::FieldsJson(b.Spec());
218 }
219
220 2 bool operator!=(const TerrainGenConfig& a, const TerrainGenConfig& b) {
221 2 return !(a == b);
222 }
223
224 } // namespace terraingen
225