summaryrefslogtreecommitdiff
path: root/include/mbgl/style/conversion.hpp
blob: 18326c8dd140bf5081ea4ad74844d2cbb8168153 (plain)
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
#pragma once

#include <mbgl/style/types.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/filter.hpp>

#include <mbgl/util/variant.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>

#include <array>
#include <string>
#include <vector>

namespace mbgl {
namespace style {
namespace conversion {

/*
   This namespace defines a generic conversion from a templated type `V` representing a JSON object conforming
   to a style property from the Mapbox Style Specification, to `PropertyValue<T>`:

       template <class T, class V>
       Result<PropertyValue<T>> convertPropertyValue(const V& value);

   This is used concretely for conversions from RapidJSON types in mbgl core, and from v8 types in
   the node bindings.

   It also defines a convertion from `V` to `Filter`, representing a JSON object conforming to a Style
   Specification filter object:

       template <class V>
       Result<Filter> convertFilter(const V& value);

   The requirements are that the following are legal expressions for a value `v` of type `const V&`:

      * `isArray(v)` -- returns a boolean indicating whether `v` represents a JSON array
      * `arrayLength(v)` -- called only if `isArray(v)`; returns a size_t length
      * `arrayMember(v)` -- called only if `isArray(v)`; returns `V` or `V&`

      * `isObject(v)` -- returns a boolean indicating whether `v` represents a JSON object
      * `objectMember(v, name)` -- called only if `isObject(v)`; `name` is `const char *`; return value:
         * is true when evaluated in a boolean context iff the named member exists
         * is convertable to a `V` or `V&` when dereferenced

      * `toBool(v)` -- returns `optional<bool>`, absence indicating `v` is not a JSON boolean
      * `toNumber(v)` -- returns `optional<float>`, absence indicating `v` is not a JSON number
      * `toString(v)` -- returns `optional<std::string>`, absence indicating `v` is not a JSON string
      * `toValue(v)` -- returns `optional<mbgl::Value>`, a variant type, for generic conversion,
        absence indicating `v` is not a boolean, number, or string. Numbers should be converted to
        unsigned integer, signed integer, or floating point, in descending preference.

   If for any reason the conversion fails, the result of `convertPropertyValue` will be the `Error` variant,
   which includes explanatory text.
*/

struct Error { const char * message; };

template <class V>
using Result = variant<V, Error>;

template <class V, class T, class Enable = void>
struct ConstantConverter {};

template <class V>
struct ConstantConverter<V, bool> {
    Result<bool> operator()(const V& value) const {
        optional<bool> converted = toBool(value);
        if (!converted) {
            return Error { "value must be a boolean" };
        }
        return *converted;
    }
};

template <class V>
struct ConstantConverter<V, float> {
    Result<float> operator()(const V& value) const {
        optional<float> converted = toNumber(value);
        if (!converted) {
            return Error { "value must be a number" };
        }
        return *converted;
    }
};

template <class V>
struct ConstantConverter<V, std::string> {
    Result<std::string> operator()(const V& value) const {
        optional<std::string> converted = toString(value);
        if (!converted) {
            return Error { "value must be a string" };
        }
        return *converted;
    }
};

template <class V, class T>
struct ConstantConverter<V, T, typename std::enable_if_t<std::is_enum<T>::value>> {
    Result<T> operator()(const V& value) const {
        optional<std::string> string = toString(value);
        if (!string) {
            return Error { "value must be a string" };
        }

        const auto result = Enum<T>::toEnum(*string);
        if (!result) {
            return Error { "value must be a valid enumeration value" };
        }

        return *result;
    }
};

template <class V>
struct ConstantConverter<V, Color> {
    Result<Color> operator()(const V& value) const {
        optional<std::string> string = toString(value);
        if (!string) {
            return Error { "value must be a string" };
        }

        optional<Color> color = Color::parse(*string);
        if (!color) {
            return Error { "value must be a valid color" };
        }

        return *color;
    }
};

template <class V>
struct ConstantConverter<V, std::array<float, 2>> {
    Result<std::array<float, 2>> operator()(const V& value) const {
        if (!isArray(value) || arrayLength(value) != 2) {
            return Error { "value must be an array of two numbers" };
        }

        optional<float> first = toNumber(arrayMember(value, 0));
        optional<float> second = toNumber(arrayMember(value, 1));
        if (!first || !second) {
            return Error { "value must be an array of two numbers" };
        }

        return std::array<float, 2> {{ *first, *second }};
    }
};

template <class V>
struct ConstantConverter<V, std::array<float, 4>> {
    Result<std::array<float, 4>> operator()(const V& value) const {
        if (!isArray(value) || arrayLength(value) != 4) {
            return Error { "value must be an array of four numbers" };
        }

        optional<float> first = toNumber(arrayMember(value, 0));
        optional<float> second = toNumber(arrayMember(value, 1));
        optional<float> third = toNumber(arrayMember(value, 2));
        optional<float> fourth = toNumber(arrayMember(value, 3));
        if (!first || !second) {
            return Error { "value must be an array of four numbers" };
        }

        return std::array<float, 4> {{ *first, *second, *third, *fourth }};
    }
};

template <class V>
struct ConstantConverter<V, std::vector<float>> {
    Result<std::vector<float>> operator()(const V& value) const {
        if (!isArray(value)) {
            return Error { "value must be an array" };
        }

        std::vector<float> result;
        result.reserve(arrayLength(value));

        for (std::size_t i = 0; i < arrayLength(value); ++i) {
            optional<float> number = toNumber(arrayMember(value, i));
            if (!number) {
                return Error { "value must be an array of numbers" };
            }
            result.push_back(*number);
        }

        return result;
    }
};

template <class V>
struct ConstantConverter<V, std::vector<std::string>> {
    Result<std::vector<std::string>> operator()(const V& value) const {
        if (!isArray(value)) {
            return Error { "value must be an array" };
        }

        std::vector<std::string> result;
        result.reserve(arrayLength(value));

        for (std::size_t i = 0; i < arrayLength(value); ++i) {
            optional<std::string> string = toString(arrayMember(value, i));
            if (!string) {
                return Error { "value must be an array of strings" };
            }
            result.push_back(*string);
        }

        return result;
    }
};

template <class T, class V>
Result<PropertyValue<T>> convertPropertyValue(const V& value) {
    if (!isObject(value)) {
        Result<T> constant = ConstantConverter<V, T>()(value);
        if (constant.template is<Error>()) {
            return constant.template get<Error>();
        }
        return constant.template get<T>();
    }

    auto stopsValue = objectMember(value, "stops");
    if (!stopsValue) {
        return Error { "function value must specify stops" };
    }

    if (!isArray(*stopsValue)) {
        return Error { "function stops must be an array" };
    }

    std::vector<std::pair<float, T>> stops;
    for (std::size_t i = 0; i < arrayLength(*stopsValue); ++i) {
        const auto& stopValue = arrayMember(*stopsValue, i);

        if (!isArray(stopValue)) {
            return Error { "function stop must be an array" };
        }

        if (arrayLength(stopValue) != 2) {
            return Error { "function stop must have two elements" };
        }

        optional<float> z = toNumber(arrayMember(stopValue, 0));
        if (!z) {
            return Error { "function stop zoom level must be a number" };
        }

        Result<T> v = ConstantConverter<V, T>()(arrayMember(stopValue, 1));
        if (v.template is<Error>()) {
            return v.template get<Error>();
        }

        stops.emplace_back(*z, v.template get<T>());
    }

    auto baseValue = objectMember(value, "base");
    if (!baseValue) {
        return Function<T>(stops, 1.0f);
    }

    optional<float> base = toNumber(*baseValue);
    if (!base) {
        return Error { "function base must be a number"};
    }

    return Function<T>(stops, *base);
}

Result<Value> normalizeFilterValue(const std::string& key, const optional<Value>&);

template <class V>
Result<Filter> convertFilter(const V& value);

template <class FilterType, class V>
Result<Filter> parseUnaryFilter(const V& value) {
    if (arrayLength(value) < 2) {
        return Error { "filter expression must have 2 elements" };
    }

    optional<std::string> key = toString(arrayMember(value, 1));
    if (!key) {
        return Error { "filter expression key must be a string" };
    }

    return FilterType { *key };
}

template <class FilterType, class V>
Result<Filter> parseBinaryFilter(const V& value) {
    if (arrayLength(value) < 3) {
        return Error { "filter expression must have 3 elements" };
    }

    optional<std::string> key = toString(arrayMember(value, 1));
    if (!key) {
        return Error { "filter expression key must be a string" };
    }

    Result<Value> filterValue = normalizeFilterValue(*key, toValue(arrayMember(value, 2)));
    if (filterValue.is<Error>()) {
        return filterValue.get<Error>();
    }

    return FilterType { *key, filterValue.get<Value>() };
}

template <class FilterType, class V>
Result<Filter> parseSetFilter(const V& value) {
    if (arrayLength(value) < 2) {
        return Error { "filter expression must at least 2 elements" };
    }

    optional<std::string> key = toString(arrayMember(value, 1));
    if (!key) {
        return Error { "filter expression key must be a string" };
    }

    std::vector<Value> values;
    for (std::size_t i = 2; i < arrayLength(value); ++i) {
        Result<Value> filterValue = normalizeFilterValue(*key, toValue(arrayMember(value, i)));
        if (filterValue.is<Error>()) {
            return filterValue.get<Error>();
        }
        values.push_back(filterValue.get<Value>());
    }

    return FilterType { *key, std::move(values) };
}

template <class FilterType, class V>
Result<Filter> parseCompoundFilter(const V& value) {
    std::vector<Filter> filters;
    for (std::size_t i = 1; i < arrayLength(value); ++i) {
        Result<Filter> element = convertFilter(arrayMember(value, i));
        if (element.is<Error>()) {
            return element;
        }
        filters.push_back(element.get<Filter>());
    }

    return FilterType { std::move(filters) };
}

template <class V>
Result<Filter> convertFilter(const V& value) {
    if (!isArray(value)) {
        return Error { "filter expression must be an array" };
    }

    if (arrayLength(value) < 1) {
        return Error { "filter expression must have at least 1 element" };
    }

    optional<std::string> op = toString(arrayMember(value, 0));
    if (!op) {
        return Error { "filter operator must be a string" };
    }

    if (*op == "==") {
        return parseBinaryFilter<EqualsFilter>(value);
    } else if (*op == "!=") {
        return parseBinaryFilter<NotEqualsFilter>(value);
    } else if (*op == ">") {
        return parseBinaryFilter<GreaterThanFilter>(value);
    } else if (*op == ">=") {
        return parseBinaryFilter<GreaterThanEqualsFilter>(value);
    } else if (*op == "<") {
        return parseBinaryFilter<LessThanFilter>(value);
    } else if (*op == "<=") {
        return parseBinaryFilter<LessThanEqualsFilter>(value);
    } else if (*op == "in") {
        return parseSetFilter<InFilter>(value);
    } else if (*op == "!in") {
        return parseSetFilter<NotInFilter>(value);
    } else if (*op == "all") {
        return parseCompoundFilter<AllFilter>(value);
    } else if (*op == "any") {
        return parseCompoundFilter<AnyFilter>(value);
    } else if (*op == "none") {
        return parseCompoundFilter<NoneFilter>(value);
    } else if (*op == "has") {
        return parseUnaryFilter<HasFilter>(value);
    } else if (*op == "!has") {
       return parseUnaryFilter<NotHasFilter>(value);
    }

    return Error { "filter operator must be one of \"==\", \"!=\", \">\", \">=\", \"<\", \"<=\", \"in\", \"!in\", \"all\", \"any\", \"none\", \"has\", or \"!has\"" };
}

} // namespace conversion
} // namespace style
} // namespace mbgl