From 2e93a9ddcf1f97188250335d991fbd7a2cc283af Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 13 Sep 2018 15:03:19 -0700 Subject: [core] For string-valued properties, do coercion rather than assertion --- include/mbgl/style/expression/parsing_context.hpp | 33 ++++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'include/mbgl/style') diff --git a/include/mbgl/style/expression/parsing_context.hpp b/include/mbgl/style/expression/parsing_context.hpp index 66014e33d4..dde907ec2c 100644 --- a/include/mbgl/style/expression/parsing_context.hpp +++ b/include/mbgl/style/expression/parsing_context.hpp @@ -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 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>()) {} @@ -70,24 +89,18 @@ public: const std::vector& 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 = {}); /* 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 = {}, - TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations); + optional = {}); /* 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 = {}); std::string key; optional expected; -- cgit v1.2.1