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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdint>
#include <memory>
#include <string>
#include <sstream>
#include "Function.hpp"
#include "Logger.hpp"
#include "Number.hpp"
#include "Rtti.hpp"
#include "Variant.hpp"
#include "VariantConverter.hpp"
#include "VariantWriter.hpp"
namespace ousia {
static std::string msgUnexpectedType(const Variant &v,
VariantType requestedType)
{
return std::string("Cannot convert ") + v.getTypeName() + std::string(" (") +
VariantWriter::writeJsonToString(v, false) + std::string(") to ") +
Variant::getTypeName(requestedType);
}
static std::string msgImplicitConversion(VariantType actualType,
VariantType requestedType)
{
return std::string("Implicit conversion from ") +
Variant::getTypeName(actualType) + std::string(" to ") +
Variant::getTypeName(requestedType);
}
bool VariantConverter::toBool(Variant &var, Logger &logger, Mode mode)
{
// Perform safe conversions
const VariantType type = var.getType();
switch (type) {
case VariantType::BOOL:
// No conversion needed if "var" already is a boolean
return true;
default:
break;
}
// Perform potentially dangerous conversions in the "ALL" mode
if (mode == Mode::ALL) {
switch (var.getType()) {
case VariantType::NULLPTR:
var = false;
return true;
case VariantType::INT:
var = var.asInt() != 0;
return true;
case VariantType::DOUBLE:
var = var.asDouble() != 0.0;
return true;
default:
var = true;
return true;
}
}
// No conversion possible, assign default value and log error
logger.error(msgUnexpectedType(var, VariantType::BOOL));
var = false;
return false;
}
bool VariantConverter::toInt(Variant &var, Logger &logger, Mode mode)
{
// Perform safe conversions
const VariantType type = var.getType();
switch (type) {
case VariantType::INT:
// No conversion needed if "var" already is an integer
return true;
default:
break;
}
// Perform all potentially dangerous conversions in the "ALL" mode
if (mode == Mode::ALL) {
switch (type) {
case VariantType::NULLPTR:
var = 0;
return true;
case VariantType::BOOL:
var = var.asBool() ? 1 : 0;
return true;
case VariantType::DOUBLE:
var = (Variant::intType)var.asDouble();
return true;
case VariantType::STRING:
case VariantType::MAGIC: {
Number n;
n.parse(var.asString(), logger);
if (n.isInt()) {
var = (Variant::intType)n.intValue();
return true;
}
break;
}
case VariantType::ARRAY: {
try {
// JavaScript behaviour when converting arrays to doubles
const Variant::arrayType &a = var.asArray();
var = (a.size() == 1) ? a[0].toInt() : 0.0;
return true;
}
catch (LoggableException ex) {
logger.log(ex);
}
}
default:
break;
}
}
// No conversion possible, assign default value and log error
logger.error(msgUnexpectedType(var, VariantType::INT));
var = 0;
return false;
}
bool VariantConverter::toDouble(Variant &var, Logger &logger, Mode mode)
{
// Perform safe conversions
const VariantType type = var.getType();
switch (type) {
case VariantType::DOUBLE:
// No conversion needed if "var" already is a double
return true;
case VariantType::INT:
// Converting integers to doubles is safe
var = (Variant::doubleType)var.asInt();
return true;
default:
break;
}
// Perform all potentially dangerous conversions in the "ALL" mode
if (mode == Mode::ALL) {
switch (type) {
case VariantType::NULLPTR:
var = 0.0;
return true;
case VariantType::BOOL:
var = var.asBool() ? 1.0 : 0.0;
return true;
case VariantType::STRING:
case VariantType::MAGIC: {
Number n;
n.parse(var.asString(), logger);
var = (Variant::doubleType)n.doubleValue();
return true;
}
case VariantType::ARRAY: {
try {
// JavaScript behaviour when converting arrays to doubles
const Variant::arrayType &a = var.asArray();
var = (a.size() == 1) ? a[0].toDouble() : 0.0;
return true;
}
catch (LoggableException ex) {
logger.log(ex);
}
}
default:
break;
}
}
// No conversion possible, assign default value and log error
logger.error(msgUnexpectedType(var, VariantType::DOUBLE));
var = 0.0;
return false;
}
bool VariantConverter::toString(Variant &var, Logger &logger, Mode mode)
{
// Perform safe conversions (all these operations are considered "lossless")
const VariantType type = var.getType();
switch (type) {
case VariantType::NULLPTR:
logger.warning(msgImplicitConversion(type, VariantType::STRING));
var = "null";
return true;
case VariantType::BOOL:
logger.warning(msgImplicitConversion(type, VariantType::STRING));
var = var.asBool() ? "true" : "false";
return true;
case VariantType::INT: {
logger.warning(msgImplicitConversion(type, VariantType::STRING));
std::stringstream ss;
ss << var.asInt();
var = ss.str().c_str();
return true;
}
case VariantType::DOUBLE: {
logger.warning(msgImplicitConversion(type, VariantType::STRING));
std::stringstream ss;
ss << var.asDouble();
var = ss.str().c_str();
return true;
}
case VariantType::MAGIC:
case VariantType::STRING:
// No conversion needed if "var" already is a string (or a magic
// string value)
return true;
default:
break;
}
// Perform lossy conversions
if (mode == Mode::ALL) {
switch (type) {
case VariantType::ARRAY:
case VariantType::MAP: {
std::stringstream ss;
VariantWriter::writeJson(var, ss, false);
var = ss.str().c_str();
return true;
}
case VariantType::OBJECT: {
// Print object address and type
Variant::objectType obj = var.asObject();
std::stringstream ss;
ss << "<object " << obj.get();
if (obj.get() != nullptr) {
ss << " (" << obj->type().name << ")";
}
ss << ">";
var = ss.str().c_str();
return true;
}
case VariantType::FUNCTION: {
// Print function pointer address
Variant::functionType obj = var.asFunction();
std::stringstream ss;
ss << "<function " << obj.get() << ">";
var = ss.str().c_str();
return true;
}
default:
break;
}
}
// No conversion possible, assign default value and log error
logger.error(msgUnexpectedType(var, VariantType::STRING));
var = "";
return false;
}
bool VariantConverter::toArray(Variant &var, const Rtti &innerType,
Logger &logger, Mode mode)
{
// If unsafe conversions are allowed, encapsulate the given variant in an
// array if it is not an array now.
if (!var.isArray() && mode == Mode::ALL) {
var.setArray(Variant::arrayType{var});
}
// Make sure the variant is an array
if (var.isArray()) {
// If no specific inner type is given, conversion is successful at this
// point
if (&innerType == &RttiTypes::None) {
return true;
}
// Convert all entries of the array to the specified inner type, log all
// failures to do so
bool res = true;
for (Variant &v : var.asArray()) {
res = convert(v, innerType, RttiTypes::None, logger, mode) & res;
}
return res;
}
// No conversion possible, assign the default value and log an error
logger.error(msgUnexpectedType(var, VariantType::ARRAY));
var.setArray(Variant::arrayType{});
return false;
}
bool VariantConverter::toMap(Variant &var, const Rtti &innerType,
Logger &logger, Mode mode)
{
// Make sure the variant is a map
if (var.isMap()) {
// If no specific inner type is given, conversion is successful at this
// point
if (&innerType == &RttiTypes::None) {
return true;
}
// Convert the inner type of the map to the specified inner type, log
// all failures to do so
bool res = true;
for (auto &e : var.asMap()) {
res = convert(e.second, innerType, RttiTypes::None, logger, mode) &
res;
}
return res;
}
// No conversion possible, assign the default value and log an error
logger.error(msgUnexpectedType(var, VariantType::MAP));
var.setMap(Variant::mapType{});
return false;
}
bool VariantConverter::toFunction(Variant &var, Logger &logger)
{
if (var.isFunction()) {
return true;
}
// No conversion possible, assign the default value and log an error
logger.error(msgUnexpectedType(var, VariantType::FUNCTION));
var.setFunction(std::shared_ptr<Function>{new FunctionStub()});
return false;
}
bool VariantConverter::convert(Variant &var, const Rtti &type,
const Rtti &innerType, Logger &logger,
Mode mode)
{
// Check for simple Variant types
if (&type == &RttiTypes::None) {
return true; // Everything is fine if no specific type was
// requested
} else if (&type == &RttiTypes::Nullptr) {
// Make sure the variant is set to null
if (!var.isNull()) {
logger.error(msgUnexpectedType(var, VariantType::NULLPTR));
var.setNull();
return false;
}
return true;
} else if (&type == &RttiTypes::Bool) {
return toBool(var, logger, mode);
} else if (&type == &RttiTypes::Int) {
return toInt(var, logger, mode);
} else if (&type == &RttiTypes::Double) {
return toDouble(var, logger, mode);
} else if (&type == &RttiTypes::String) {
return toString(var, logger, mode);
} else if (&type == &RttiTypes::Array) {
return toArray(var, innerType, logger, mode);
} else if (&type == &RttiTypes::Map) {
return toMap(var, innerType, logger, mode);
} else if (&type == &RttiTypes::Function) {
return toFunction(var, logger);
}
// If none of the above primitive types is requested, we were
// obviously asked for a managed object.
if (!var.isObject()) {
logger.error(msgUnexpectedType(var, VariantType::OBJECT));
var.setObject(nullptr);
return false;
}
// Make sure the object type is correct
if (!var.getRtti().isa(type)) {
logger.error(std::string("Expected object of type ") + type.name +
" but got object of type " + var.getRtti().name);
var.setObject(nullptr);
return false;
}
return true;
}
bool VariantConverter::convert(Variant &var, const Rtti &type,
Logger &logger, Mode mode)
{
return convert(var, type, RttiTypes::None, logger, mode);
}
}
|