summaryrefslogtreecommitdiff
path: root/src/mbgl/style/expression/coercion.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/style/expression/coercion.cpp')
-rw-r--r--src/mbgl/style/expression/coercion.cpp30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/mbgl/style/expression/coercion.cpp b/src/mbgl/style/expression/coercion.cpp
index 4a17895991..75a6056081 100644
--- a/src/mbgl/style/expression/coercion.cpp
+++ b/src/mbgl/style/expression/coercion.cpp
@@ -82,6 +82,10 @@ EvaluationResult toColor(const Value& colorValue) {
);
}
+EvaluationResult toFormatted(const Value& formattedValue) {
+ return Formatted(toString(formattedValue).c_str());
+}
+
Coercion::Coercion(type::Type type_, std::vector<std::unique_ptr<Expression>> inputs_) :
Expression(Kind::Coercion, std::move(type_)),
inputs(std::move(inputs_))
@@ -96,11 +100,26 @@ Coercion::Coercion(type::Type type_, std::vector<std::unique_ptr<Expression>> in
coerceSingleValue = toNumber;
} else if (t.is<type::StringType>()) {
coerceSingleValue = [] (const Value& v) -> EvaluationResult { return toString(v); };
+ } else if (t.is<type::FormattedType>()) {
+ coerceSingleValue = toFormatted;
} else {
assert(false);
}
}
+mbgl::Value Coercion::serialize() const {
+ if (getType().is<type::FormattedType>()) {
+ // Since there's no explicit "to-formatted" coercion, the only coercions should be created
+ // by string expressions that get implicitly coerced to "formatted".
+ std::vector<mbgl::Value> serialized{{ std::string("format") }};
+ serialized.push_back(inputs[0]->serialize());
+ serialized.push_back(std::unordered_map<std::string, mbgl::Value>());
+ return serialized;
+ } else {
+ return Expression::serialize();
+ }
+};
+
std::string Coercion::getOperator() const {
return getType().match(
[](const type::BooleanType&) { return "to-boolean"; },
@@ -129,10 +148,16 @@ ParseResult Coercion::parse(const Convertible& value, ParsingContext& ctx) {
auto it = types.find(*toString(arrayMember(value, 0)));
assert(it != types.end());
- if ((it->second == type::Boolean || it->second == type::String) && length != 2) {
+ if ((it->second == type::Boolean || it->second == type::String || it->second == type::Formatted) && length != 2) {
ctx.error("Expected one argument.");
return ParseResult();
}
+
+ /**
+ * Special form for error-coalescing coercion expressions "to-number",
+ * "to-color". Since these coercions can fail at runtime, they accept multiple
+ * arguments, only evaluating one at a time until one succeeds.
+ */
std::vector<std::unique_ptr<Expression>> parsed;
parsed.reserve(length - 1);
@@ -186,6 +211,3 @@ std::vector<optional<Value>> Coercion::possibleOutputs() const {
} // namespace expression
} // namespace style
} // namespace mbgl
-
-
-