diff options
author | Chris Loer <chris.loer@gmail.com> | 2018-10-02 17:03:50 -0700 |
---|---|---|
committer | Chris Loer <chris.loer@mapbox.com> | 2018-10-15 13:15:46 -0700 |
commit | ce76bde13d0f4381ee861f81daf636defaff0bc5 (patch) | |
tree | e37d93d14fc64620069bac5488bae871af2fa431 /src/mbgl/style/expression/coercion.cpp | |
parent | bc718257748f1ad87658e85f8c31b574afca57a9 (diff) | |
download | qtlocation-mapboxgl-ce76bde13d0f4381ee861f81daf636defaff0bc5.tar.gz |
[core] Initial implementation of 'format' expression
Diffstat (limited to 'src/mbgl/style/expression/coercion.cpp')
-rw-r--r-- | src/mbgl/style/expression/coercion.cpp | 30 |
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 - - - |