-
Notifications
You must be signed in to change notification settings - Fork 2
/
translator.cpp
372 lines (342 loc) · 15.5 KB
/
translator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/******************************************************************************
* Copyright (C) 2016 Kitsune Ral <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "translator.h"
#include "printer.h"
#include "yaml.h"
#include <regex>
using namespace std;
void addTypeAttributes(TypeUsage& typeUsage, const YamlMap<>& attributesMap)
{
for (const auto& [attrName, attrData] : attributesMap) {
if (attrName == "type")
continue;
switch (attrData.Type()) {
case YAML::NodeType::Null:
typeUsage.attributes.emplace(std::move(attrName), string {});
break;
case YAML::NodeType::Scalar:
typeUsage.attributes.emplace(std::move(attrName), attrData.as<string>());
break;
case YAML::NodeType::Sequence:
if (const auto& seq = attrData.as<YamlSequence<string>>(); seq.size() > 0)
typeUsage.lists.emplace(std::move(attrName),
vector<string>{seq.begin(), seq.end()});
break;
default:
throw YamlException(attrData, "Malformed attribute");
}
}
}
TypeUsage parseTargetType(const YamlNode& yamlTypeNode)
{
using namespace YAML::NodeType;
if (yamlTypeNode.Type() == Null)
return {};
if (yamlTypeNode.Type() == Scalar)
return TypeUsage(yamlTypeNode.as<string>());
const auto yamlTypeMap = yamlTypeNode.as<YamlMap<>>();
TypeUsage typeUsage{yamlTypeMap.get<string>("type", {})};
addTypeAttributes(typeUsage, yamlTypeMap);
return typeUsage;
}
TypeUsage parseTargetType(const YamlNode& yamlTypeNode,
const YamlMap<>& commonAttributesYaml)
{
auto tu = parseTargetType(yamlTypeNode);
addTypeAttributes(tu, commonAttributesYaml);
return tu;
}
void parseEntries(const YamlSequence<YamlMap<>>& entriesYaml,
const std::invocable<string, YamlNode, YamlMap<>> auto& inserter,
const YamlMap<>& commonAttributesYaml = {})
{
for (const auto& typesBlockYaml: entriesYaml) // clazy:exclude=range-loop
{
switch (typesBlockYaml.size())
{
case 0:
throw YamlException(typesBlockYaml, "Empty type entry");
case 1:
{
const auto& [name, details] = typesBlockYaml.front();
if (name == "+set" || name == "+on")
throw YamlException(
typesBlockYaml,
"+set and +on block should always be used together in the same object - "
"did you accidentally put them in separate list items?");
inserter(name, details, commonAttributesYaml);
break;
}
case 2: {
const auto setYaml = typesBlockYaml.maybeGet<YamlMap<>>("+set");
const auto onYaml = typesBlockYaml.maybeGet<YamlSequence<YamlMap<>>>("+on");
if (setYaml && onYaml)
{
// #56: merge into the outer common attributes
// YamlMap constructors shallow-copy but we need a
// one-level-deep copy here to revert to the previous set
// after returning from recursion
YamlMap<> newCommonAttributesYaml;
// Instead of specialising struct convert<>, simply cast
// to YAML::Node that already has a specialisation
for (const auto& [k, v] : commonAttributesYaml)
newCommonAttributesYaml.force_insert<string, YAML::Node>(k,v);
for (const auto& [k, v] : *setYaml)
newCommonAttributesYaml.force_insert<string, YAML::Node>(k, v);
parseEntries(*onYaml, inserter, newCommonAttributesYaml);
break;
}
if (bool(setYaml) != bool(onYaml)) [[unlikely]]
throw YamlException(typesBlockYaml,
"+set and +on block should always be used together, "
"one can not occur without another");
[[fallthrough]];
}
default:
throw YamlException(typesBlockYaml,
"Too many entries in the map, check indentation");
}
}
}
pair_vector_t<TypeUsage> parseTypeEntry(const YamlNode& targetTypeYaml,
const YamlMap<>& commonAttributesYaml = {})
{
switch (targetTypeYaml.Type())
{
case YAML::NodeType::Scalar: // Use a type with no regard to format
case YAML::NodeType::Map: // Same, with attributes for the target type
{
return { { {},
parseTargetType(targetTypeYaml, commonAttributesYaml) } };
}
case YAML::NodeType::Sequence: // A list of formats for the type
{
pair_vector_t<TypeUsage> targetTypes;
parseEntries(
targetTypeYaml.as<YamlSequence<YamlMap<>>>(),
[&targetTypes](string formatName, const YamlNode& typeYaml,
const YamlMap<>& commonAttrsYaml) {
if (formatName.empty())
formatName = "/"; // Empty format means all formats
else if (formatName.size() > 1 &&
formatName.front() == '/' &&
formatName.back() == '/')
{
formatName.pop_back();
}
targetTypes.emplace_back(std::move(formatName),
parseTargetType(typeYaml, commonAttrsYaml));
},
commonAttributesYaml);
return targetTypes;
}
default:
throw YamlException(targetTypeYaml, "Malformed type entry");
}
}
subst_list_t loadStringMap(const YamlMap<>& yaml, string_view key)
{
subst_list_t stringMap;
for (const auto& [patternNode, subst] : yaml.maybeGet<YamlGenericMap>(key)) {
auto pattern = patternNode.as<string>();
if (pattern.empty()) [[unlikely]]
clog << patternNode.location()
<< ": warning: empty pattern in substitutions, skipping\n";
else if (pattern.size() > 1 && pattern.front() != '/' && pattern.back() == '/') [[unlikely]]
clog << patternNode.location() << ": warning: invalid regular expression, skipping\n"
"(use a regex with \\/ to match strings beginning with /)\n";
else {
if (pattern.front() == '/' && pattern.back() == '/')
pattern.pop_back();
if (!subst.IsMap())
stringMap.emplace_back(pattern, subst.as<string>());
else if (subst.size() != 0) { // empty() would also check IsDefined() - again
clog << subst.location()
<< ": warning: non-empty maps have no meaning in substitutions\n"
<< "(put literal {} to indicate entry removal)\n";
} else
stringMap.emplace_back(pattern, nullopt);
}
}
return stringMap;
}
Translator::Translator(const path& configFilePath, path outputDirPath,
Verbosity verbosity)
: _verbosity(verbosity), _outputDirPath(std::move(outputDirPath))
{
cout << "Using config file at " << configFilePath << endl;
const auto configY = YamlNode::fromFile(configFilePath).as<YamlMap<YamlMap<>>>();
if (const auto& analyzerYaml = configY["analyzer"]) {
_substitutions = loadStringMap(*analyzerYaml, "subst");
_identifiers = loadStringMap(*analyzerYaml, "identifiers");
parseEntries(
analyzerYaml->get<YamlSequence<YamlMap<>>>("types", {}),
[this](const string& name, const YamlNode& typeYaml, const YamlMap<>& commonAttrsYaml) {
_typesMap.emplace_back(name, parseTypeEntry(typeYaml, commonAttrsYaml));
});
if (const auto& referencesYaml = analyzerYaml->maybeGet<YamlMap<>>("references")) {
referencesYaml->maybeLoad("importRenderer", &_importRenderer);
ranges::copy(referencesYaml->maybeGet<YamlSequence<string>>("inline"),
back_inserter(_inlinedRefs));
parseEntries(referencesYaml->get<YamlSequence<YamlMap<>>>("replace", {}),
[this](string_view name, const YamlNode& typeYaml,
const YamlMap<>& commonAttrsYaml) {
if (name.size() > 1 && name.front() == '/' && name.back() == '/')
name.remove_suffix(1);
_refReplacements.emplace_back(
name, parseTargetType(typeYaml, commonAttrsYaml));
});
}
if (_verbosity == Verbosity::Debug) {
// TODO: dump identifier substitutions?
for (const auto& t : _typesMap) {
clog << "Type " << t.first << ":\n";
for (const auto& f : t.second) {
clog << " Format " << (f.first.empty() ? "(none)" : f.first) << ":\n"
<< " mapped to " << (!f.second.name.empty() ? f.second.name : "(none)")
<< '\n';
if (!f.second.attributes.empty()) {
clog << " attributes:\n";
for (const auto& a : f.second.attributes)
clog << " " << a.first << " -> " << a.second << '\n';
} else
clog << " no attributes\n";
if (!f.second.lists.empty()) {
clog << " lists:\n";
for (const auto& l : f.second.lists)
clog << " " << l.first << " (entries: " << l.second.size()
<< ")\n";
} else
clog << " no lists\n";
}
}
clog.flush();
// TODO: dump reference substitutions
}
}
Printer::context_type env;
using namespace kainjow::mustache;
const auto& mustacheYaml = configY.get("mustache");
const auto& delimiter = mustacheYaml.get<string>("delimiter", {});
for (const auto& [cName, cValue] : mustacheYaml.maybeGet<YamlMap<string>>("constants"))
env.emplace(cName, cValue);
for (const auto& [pName, pValue] : mustacheYaml.maybeGet<YamlMap<string>>("partials"))
env.emplace(pName, makePartial(pValue, delimiter));
const auto& templatesYaml = mustacheYaml.maybeGet<YamlMap<YamlMap<string>>>("templates");
templatesYaml->maybeLoad("data", &_dataTemplates);
templatesYaml->maybeLoad("api", &_apiTemplates);
_printer = make_unique<Printer>(std::move(env), configFilePath.parent_path(),
mustacheYaml.get<string>("outFilesList", {}),
delimiter, *this);
}
Translator::~Translator() = default;
Translator::output_config_t Translator::outputConfig(const path& fileStem,
const Model& model) const
{
const auto& srcConfig =
model.apiSpec == ApiSpec::JSONSchema ? _dataTemplates : _apiTemplates;
output_config_t result;
for (const auto& [fExtension, fTemplate]: srcConfig)
result.emplace_back(path(fileStem) += fExtension, fTemplate);
return result;
}
TypeUsage Translator::mapType(string_view swaggerType, string_view swaggerFormat,
string_view baseName) const
{
TypeUsage tu;
for (const auto& [swType, swFormats]: _typesMap)
if (swType == swaggerType)
for (const auto& [swFormat, mappedType]: swFormats)
{
if (swFormat == swaggerFormat
|| (!swFormat.empty() && swFormat.front() == '/'
&& regex_search(string(swaggerFormat),
regex(++swFormat.begin(), swFormat.end()))))
{
// FIXME (#22): a source of great inefficiency.
// TypeUsage should become a handle to an instance of
// a newly-made TypeDefinition type that would own all
// the stuff TypeUsage now has, except paramTypes
tu = mappedType;
goto conclusion;
}
}
conclusion:
// Fallback chain: baseName, swaggerFormat, swaggerType
tu.baseName = baseName.empty()
? swaggerFormat.empty() ? swaggerType : swaggerFormat
: baseName;
return tu;
}
string Translator::mapIdentifier(string_view baseName, const Identifier* scope, bool required) const
{
auto scopedName = scope ? scope->qualifiedName() : string();
scopedName.append(1, '/').append(baseName);
string newName{baseName};
for (const auto& [pattn, subst]: _identifiers)
{
if (!pattn.empty() && pattn.front() == '/') {
const regex re(++pattn.begin(), pattn.end());
if (!subst && regex_search(scopedName, re)) {
if (_verbosity == Verbosity::Debug)
cout << "Regex erasure: " << scopedName << "\n";
newName = {};
break;
}
if (auto&& replaced = regex_replace(scopedName, re, *subst);
replaced != scopedName)
{
if (_verbosity == Verbosity::Debug)
cout << "Regex replace: " << scopedName << " -> " << replaced << '\n';
newName = replaced;
break;
}
} else if (pattn == baseName || pattn == scopedName) {
newName = subst.value_or(""s);
break;
}
}
if (newName.empty() && required)
throw Exception("Attempt to skip the required variable '"s.append(baseName).append(
"' - check 'identifiers' block in your gtad.yaml"));
return newName;
}
TypeUsage Translator::mapReference(string_view fullRefPath) const
{
TypeUsage tu;
for (const auto& [pattn, mappedType] : _refReplacements)
if (pattn == fullRefPath
|| (!pattn.empty() && pattn.front() == '/'
&& regex_search(string(fullRefPath), regex(++pattn.begin(), pattn.end()))))
{
tu = mappedType;
break;
}
if (tu.empty())
tu.importRenderer = _importRenderer;
tu.baseName = fullRefPath;
return tu;
}
bool Translator::isRefInlined(string_view fullRefPath) const
{
return ranges::any_of(_inlinedRefs, [fullRefPath](string_view pattn) {
return pattn == fullRefPath
|| (!pattn.empty() && pattn.front() == '/'
&& regex_search(string(fullRefPath), regex(pattn.begin() + 1, pattn.end() - 1)));
});
}