[core] Convert token strings to expressions

This commit is contained in:
John Firebaugh
2018-07-13 17:02:30 -07:00
parent a3d988ab85
commit af89318b1d
20 changed files with 166 additions and 101 deletions

View File

@@ -14,7 +14,7 @@ namespace conversion {
template <>
struct Converter<ColorRampPropertyValue> {
optional<ColorRampPropertyValue> operator()(const Convertible& value, Error& error) const {
optional<ColorRampPropertyValue> operator()(const Convertible& value, Error& error, bool /* convertTokens */ = false) const {
using namespace mbgl::style::expression;
if (isUndefined(value)) {
return ColorRampPropertyValue();

View File

@@ -16,7 +16,7 @@ namespace conversion {
template <class T>
struct Converter<DataDrivenPropertyValue<T>> {
optional<DataDrivenPropertyValue<T>> operator()(const Convertible& value, Error& error) const {
optional<DataDrivenPropertyValue<T>> operator()(const Convertible& value, Error& error, bool convertTokens) const {
using namespace mbgl::style::expression;
if (isUndefined(value)) {
@@ -34,13 +34,13 @@ struct Converter<DataDrivenPropertyValue<T>> {
}
expression = PropertyExpression<T>(std::move(*parsed));
} else if (isObject(value)) {
expression = convertFunctionToExpression<T>(value, error);
expression = convertFunctionToExpression<T>(value, error, convertTokens);
} else {
optional<T> constant = convert<T>(value, error);
if (!constant) {
return {};
}
return DataDrivenPropertyValue<T>(*constant);
return convertTokens ? maybeConvertTokens(*constant) : DataDrivenPropertyValue<T>(*constant);
}
if (!expression) {
@@ -56,6 +56,17 @@ struct Converter<DataDrivenPropertyValue<T>> {
return DataDrivenPropertyValue<T>(*constant);
}
}
template <class S>
DataDrivenPropertyValue<T> maybeConvertTokens(const S& t) const {
return DataDrivenPropertyValue<T>(t);
};
DataDrivenPropertyValue<T> maybeConvertTokens(const std::string& t) const {
return hasTokens(t)
? DataDrivenPropertyValue<T>(PropertyExpression<T>(convertTokenStringToExpression(t)))
: DataDrivenPropertyValue<T>(t);
}
};
} // namespace conversion

View File

@@ -10,11 +10,14 @@ namespace mbgl {
namespace style {
namespace conversion {
optional<std::unique_ptr<expression::Expression>> convertFunctionToExpression(expression::type::Type, const Convertible&, Error&);
bool hasTokens(const std::string&);
std::unique_ptr<expression::Expression> convertTokenStringToExpression(const std::string&);
optional<std::unique_ptr<expression::Expression>> convertFunctionToExpression(expression::type::Type, const Convertible&, Error&, bool convertTokens);
template <class T>
optional<PropertyExpression<T>> convertFunctionToExpression(const Convertible& value, Error& error) {
auto expression = convertFunctionToExpression(expression::valueTypeToExpressionType<T>(), value, error);
optional<PropertyExpression<T>> convertFunctionToExpression(const Convertible& value, Error& error, bool convertTokens) {
auto expression = convertFunctionToExpression(expression::valueTypeToExpressionType<T>(), value, error, convertTokens);
if (!expression) {
return {};
}

View File

@@ -16,9 +16,13 @@ namespace conversion {
template <class T>
struct Converter<PropertyValue<T>> {
optional<PropertyValue<T>> operator()(const Convertible& value, Error& error) const {
optional<PropertyValue<T>> operator()(const Convertible& value, Error& error, bool convertTokens = false) const {
using namespace mbgl::style::expression;
// Only icon-image and text-field support tokens, and they are both data-driven.
assert(!convertTokens);
(void)convertTokens;
if (isUndefined(value)) {
return PropertyValue<T>();
}
@@ -34,7 +38,7 @@ struct Converter<PropertyValue<T>> {
}
expression = PropertyExpression<T>(std::move(*parsed));
} else if (isObject(value)) {
expression = convertFunctionToExpression<T>(value, error);
expression = convertFunctionToExpression<T>(value, error, false);
} else {
optional<T> constant = convert<T>(value, error);
if (!constant) {

View File

@@ -52,14 +52,6 @@ public:
);
}
bool isExpression() const {
return value.match(
[] (const Undefined&) { return false; },
[] (const T&) { return false; },
[] (const PropertyExpression<T>& fn) { return fn.isExpression; }
);
}
const T & asConstant() const { return value.template get< T >(); }
const PropertyExpression<T>& asExpression() const { return value.template get<PropertyExpression<T>>(); }

View File

@@ -3,6 +3,7 @@
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/interpolator.hpp>
#include <mbgl/util/ignore.hpp>
#include <memory>
#include <string>
@@ -16,6 +17,13 @@ namespace dsl {
// This convenience API does little to no expression validation or type-checking, and is intended for
// use only by test and other non-production code.
template <class... Args>
std::vector<std::unique_ptr<Expression>> vec(Args... args) {
std::vector<std::unique_ptr<Expression>> result;
util::ignore({ (result.push_back(std::move(args)), 0)... });
return result;
}
std::unique_ptr<Expression> error(std::string);
std::unique_ptr<Expression> literal(const char* value);
@@ -27,8 +35,8 @@ std::unique_ptr<Expression> number(std::unique_ptr<Expression>);
std::unique_ptr<Expression> string(std::unique_ptr<Expression>);
std::unique_ptr<Expression> boolean(std::unique_ptr<Expression>);
std::unique_ptr<Expression> toColor(const char* value);
std::unique_ptr<Expression> toColor(std::unique_ptr<Expression>);
std::unique_ptr<Expression> toString(std::unique_ptr<Expression>);
std::unique_ptr<Expression> get(const char* value);
std::unique_ptr<Expression> get(std::unique_ptr<Expression>);
@@ -68,6 +76,8 @@ std::unique_ptr<Expression> interpolate(Interpolator interpolator,
double input2, std::unique_ptr<Expression> output2,
double input3, std::unique_ptr<Expression> output3);
std::unique_ptr<Expression> concat(std::vector<std::unique_ptr<Expression>> inputs);
} // namespace dsl
} // namespace expression
} // namespace style

View File

@@ -14,16 +14,9 @@ namespace style {
template <class T>
class PropertyExpression {
public:
PropertyExpression(std::unique_ptr<expression::Expression> expression_)
: isExpression(true),
expression(std::move(expression_)),
zoomCurve(expression::findZoomCurveChecked(expression.get())) {
}
// To be used only for conversions from legacy functions.
PropertyExpression(std::unique_ptr<expression::Expression> expression_, optional<T> defaultValue_)
: isExpression(false),
expression(std::move(expression_)),
// Second parameter to be used only for conversions from legacy functions.
PropertyExpression(std::unique_ptr<expression::Expression> expression_, optional<T> defaultValue_ = {})
: expression(std::move(expression_)),
defaultValue(std::move(defaultValue_)),
zoomCurve(expression::findZoomCurveChecked(expression.get())) {
}
@@ -108,7 +101,6 @@ public:
const expression::Expression& getExpression() const { return *expression; }
bool useIntegerZoom = false;
bool isExpression;
friend bool operator==(const PropertyExpression& lhs,
const PropertyExpression& rhs) {