[core] For string-valued properties, do coercion rather than assertion

This commit is contained in:
John Firebaugh
2018-09-13 15:03:19 -07:00
committed by Chris Loer
parent 89b075f592
commit 2e93a9ddcf
9 changed files with 89 additions and 51 deletions

View File

@@ -52,6 +52,25 @@ public:
} // namespace detail
/*
Controls the annotation behavior of the parser when encountering an expression
whose type is not a subtype of the expected type. The default behavior, used
when optional<TypeAnnotationOption> is a nullopt, is as follows:
When we expect a number, string, boolean, or array but have a value, wrap it in an assertion.
When we expect a color or formatted string, but have a string or value, wrap it in a coercion.
Otherwise, we do static type-checking.
These behaviors are overridable for:
* The "coalesce" operator, which needs to omit type annotations.
* String-valued properties (e.g. `text-field`), where coercion is more convenient than assertion.
*/
enum class TypeAnnotationOption {
coerce,
assert,
omit
};
class ParsingContext {
public:
ParsingContext() : errors(std::make_shared<std::vector<ParsingError>>()) {}
@@ -70,24 +89,18 @@ public:
const std::vector<ParsingError>& getErrors() const { return *errors; }
const std::string getCombinedErrors() const;
enum TypeAnnotationOption {
includeTypeAnnotations,
omitTypeAnnotations
};
/*
Parse the given style-spec JSON value as an expression.
*/
ParseResult parseExpression(const mbgl::style::conversion::Convertible& value,
TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations);
optional<TypeAnnotationOption> = {});
/*
Parse the given style-spec JSON value as an expression intended to be used
in a layout or paint property. This entails checking additional constraints
that exist in that context but not, e.g., for filters.
*/
ParseResult parseLayerPropertyExpression(const mbgl::style::conversion::Convertible& value,
TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations);
ParseResult parseLayerPropertyExpression(const mbgl::style::conversion::Convertible& value);
/*
Parse a child expression. For use by individual Expression::parse() methods.
@@ -95,7 +108,7 @@ public:
ParseResult parse(const mbgl::style::conversion::Convertible&,
std::size_t,
optional<type::Type> = {},
TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations);
optional<TypeAnnotationOption> = {});
/*
Parse a child expression. For use by individual Expression::parse() methods.
@@ -156,7 +169,7 @@ private:
appropriate ParseXxxx::parse(const V&, ParsingContext) method.
*/
ParseResult parse(const mbgl::style::conversion::Convertible& value,
TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations);
optional<TypeAnnotationOption> = {});
std::string key;
optional<type::Type> expected;