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
|
#pragma once
#include <vector>
#include <memory>
#include <mbgl/util/optional.hpp>
#include <mbgl/style/expression/literal.hpp>
#include <mbgl/style/expression/type.hpp>
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/conversion.hpp>
namespace mbgl {
namespace style {
namespace expression {
struct ParseLiteral {
static ParseResult parse(const mbgl::style::conversion::Value& value, ParsingContext ctx) {
const optional<Value> parsedValue = parseValue(value, ctx);
if (!parsedValue) {
return ParseResult();
}
// special case: infer the item type if possible for zero-length arrays
if (
ctx.expected &&
ctx.expected->template is<type::Array>() &&
parsedValue->template is<std::vector<Value>>()
) {
auto type = typeOf(*parsedValue).template get<type::Array>();
auto expected = ctx.expected->template get<type::Array>();
if (
type.N && (*type.N == 0) &&
(!expected.N || (*expected.N == 0))
) {
return ParseResult(std::make_unique<Literal>(expected, parsedValue->template get<std::vector<Value>>()));
}
}
return ParseResult(std::make_unique<Literal>(*parsedValue));
}
static optional<Value> parseValue(const mbgl::style::conversion::Value& value, ParsingContext ctx) {
using namespace mbgl::style::conversion;
if (isUndefined(value)) return {Null};
if (isObject(value)) {
std::unordered_map<std::string, Value> result;
bool error = false;
eachMember(value, [&] (const std::string& k, const mbgl::style::conversion::Value& v) -> optional<conversion::Error> {
if (!error) {
optional<Value> memberValue = parseValue(v, ctx);
if (memberValue) {
result.emplace(k, *memberValue);
} else {
error = true;
}
}
return {};
});
return error ? optional<Value>() : optional<Value>(result);
}
if (isArray(value)) {
std::vector<Value> result;
const auto length = arrayLength(value);
for(std::size_t i = 0; i < length; i++) {
optional<Value> item = parseValue(arrayMember(value, i), ctx);
if (item) {
result.emplace_back(*item);
} else {
return optional<Value>();
}
}
return optional<Value>(result);
}
optional<mbgl::Value> v = toValue(value);
assert(v);
return v->match(
[&](uint64_t n) { return checkNumber(n); },
[&](int64_t n) { return checkNumber(n); },
[&](double n) { return checkNumber(n); },
[&](const auto&) {
return optional<Value>(toExpressionValue(*v));
}
);
}
template <typename T>
static optional<Value> checkNumber(T n) {
if (n > std::numeric_limits<double>::max()) {
return {std::numeric_limits<double>::infinity()};
} else {
return {static_cast<double>(n)};
}
}
};
} // namespace expression
} // namespace style
} // namespace mbgl
|