-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.cpp
185 lines (160 loc) · 5.28 KB
/
model.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
#include "model.h"
#include <algorithm>
#include <locale>
using namespace std;
string Identifier::qualifiedName() const
{
return (call ? call->name + roleToChar(role) : "") + name;
}
TypeUsage::TypeUsage(const ObjectSchema& schema)
: Identifier(static_cast<const Identifier&>(schema)), baseName(schema.name)
{}
void TypeUsage::assignName(string setName, string setBaseName)
{
if (!name.empty())
throw ModelException("It's not allowed to overwrite used type name if it's already set");
name = std::move(setName);
baseName = setBaseName.empty() ? name : std::move(setBaseName);
}
TypeUsage TypeUsage::specialize(vector<TypeUsage>&& params) const
{
auto tu = *this;
tu.paramTypes = params;
return tu;
}
void toUpper(char& c) { c = toupper(c, locale::classic()); }
string titleCased(string s)
{
string::size_type pos = 0;
while (pos < s.size())
{
toUpper(s[pos]);
pos = s.find_first_of("/_ .-:", pos);
if (pos == string::npos)
break;
// Do not erase '_' at the beginning or the end of an identifier
if (s[pos] != '_' || (pos != 0 && pos != s.size() - 1))
s.erase(pos, 1);
else
++pos;
}
// Remove all remaining non-identifier characters
std::erase_if(s, [] (auto c) { return !isalnum(c) && c != '_'; });
return s;
}
string VarDecl::toString(bool withDefault) const
{
auto result = type.name + " " + name;
if (withDefault && !required)
result += " = " + (defaultValue.empty() ? "(empty)" : defaultValue);
return result;
}
Path::Path(string path)
: string(std::move(path))
{
if (empty())
throw ModelException("Path cannot be empty");
// Working around quirks in the current Matrix CS API definition
// (still applies to any other spec as well)
while (back() == ' ' || back() == '/')
pop_back();
for (size_type i = 0; i < size();)
{
const auto i1 = find('{', i);
if (i1 == npos)
{
parts.emplace_back(i, npos, PartType::Literal);
break;
}
const auto i2 = find('}', i1);
if (i2 == npos)
throw ModelException("Unbalanced braces in the path: " + *this);
parts.emplace_back(i, i1 - i, PartType::Literal);
parts.emplace_back(i1 + 1, i2 - i1 - 1, PartType::Variable);
i = i2 + 1;
}
}
const array<string, 3> Call::ParamGroups {{"path"s, "query"s, "header"s}};
auto getParamsBlockIndex(const string& name)
{
for (Call::params_type::size_type i = 0; i < 4; ++i)
if (Call::ParamGroups[i] == name)
return i;
throw ModelException("Unknown params block name: " + name);
}
Call::params_type& Call::getParamsBlock(const string& blockName)
{
return params[getParamsBlockIndex(blockName)];
}
Call::params_type Call::collateParams() const
{
params_type allCollated;
for (auto c: params)
allCollated.insert(allCollated.end(), c.begin(), c.end());
dispatchVisit(
body,
[&allCollated](const FlatSchema& unpacked) {
allCollated.insert(allCollated.end(), unpacked.fields.begin(),
unpacked.fields.end());
if (unpacked.hasAdditionalProperties())
allCollated.emplace_back(unpacked.additionalProperties);
},
[&allCollated](const VarDecl& packed) {
allCollated.emplace_back(packed);
},
[](monostate) {});
return allCollated;
}
Call& Model::addCall(Path path, string verb, string operationId, bool deprecated, bool needsToken)
{
if (callClasses.empty())
callClasses.emplace_back();
auto& cc = callClasses.back();
cc.calls.emplace_back(std::move(path), std::move(verb),
std::move(operationId), deprecated, needsToken);
return cc.calls.back();
}
void Model::addSchema(ObjectSchema&& schema, const TypeUsage& tu)
{
auto dupIt = find_if(types.begin(), types.end(), [&](const schemaholder_type& s) {
return s.first->call == schema.call && s.first->name == schema.name;
});
if (dupIt != types.end())
return;
addImportsFrom(schema);
types.emplace_back(std::make_unique<const ObjectSchema>(std::move(schema)), tu);
}
void Model::addImportsFrom(const ObjectSchema& s)
{
for (const auto& pt : s.parentTypes)
addImportsFrom(pt);
addImportsFrom(static_cast<const FlatSchema&>(s));
}
void Model::addImportsFrom(const FlatSchema& s)
{
for (const auto& f : s.fields)
addImportsFrom(f.type);
if (!s.additionalProperties.type.empty())
addImportsFrom(s.additionalProperties.type);
}
void Model::addImportsFrom(const TypeUsage& type)
{
const auto renderer = type.importRenderer;
const auto singleTypeImport = type.attributes.find("imports");
if (singleTypeImport != type.attributes.end())
imports.emplace(singleTypeImport->second, renderer);
const auto typeImportsIt = type.lists.find("imports");
if (typeImportsIt != type.lists.end())
for (auto&& import : typeImportsIt->second)
imports.emplace(import, renderer);
for (const auto& paramType : type.paramTypes)
addImportsFrom(paramType);
}
void Model::clear()
{
apiSpec = ApiSpec::Undefined;
imports.clear();
types.clear();
defaultServers.clear();
callClasses.clear();
}