From f31386385952f42e0468c7d7a0ab0a066f0bf803 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Fri, 30 Nov 2018 14:31:37 +0200 Subject: [core] Remove optional from GeometryTileFeature::getValue() --- .../default/mbgl/tile/default_tile_feature.hpp | 8 ++++-- src/mbgl/annotation/annotation_tile.cpp | 7 ++---- src/mbgl/annotation/annotation_tile.hpp | 2 +- src/mbgl/layout/symbol_feature.hpp | 2 +- src/mbgl/style/expression/compound_expression.cpp | 29 ++++++++++++---------- src/mbgl/style/expression/expression.cpp | 10 +++----- src/mbgl/tile/geojson_tile_data.hpp | 8 ++---- src/mbgl/tile/geometry_tile_data.hpp | 4 +-- src/mbgl/tile/vector_tile_data.cpp | 5 ++-- src/mbgl/tile/vector_tile_data.hpp | 2 +- test/tile/vector_tile.test.cpp | 6 ++--- 11 files changed, 40 insertions(+), 43 deletions(-) diff --git a/platform/default/mbgl/tile/default_tile_feature.hpp b/platform/default/mbgl/tile/default_tile_feature.hpp index 9183fd583a..417bb8c637 100644 --- a/platform/default/mbgl/tile/default_tile_feature.hpp +++ b/platform/default/mbgl/tile/default_tile_feature.hpp @@ -29,13 +29,17 @@ public: return id; } - optional getValue(const std::string& key) const override { - return properties.count(key) ? properties.at(key) : optional(); + Value getValue(const std::string& key) const override { + return properties.count(key) ? properties.at(key) : NullValue(); } GeometryCollection getGeometries() const override { return geometry; } + + const PropertyMap& getProperties() const override { + return properties; + } }; } // namespace mbgl diff --git a/src/mbgl/annotation/annotation_tile.cpp b/src/mbgl/annotation/annotation_tile.cpp index 74a7dad682..d22e1ca9b3 100644 --- a/src/mbgl/annotation/annotation_tile.cpp +++ b/src/mbgl/annotation/annotation_tile.cpp @@ -47,12 +47,9 @@ FeatureType AnnotationTileFeature::getType() const { return data->type; } -optional AnnotationTileFeature::getValue(const std::string& key) const { +Value AnnotationTileFeature::getValue(const std::string& key) const { auto it = data->properties.find(key); - if (it != data->properties.end()) { - return optional(it->second); - } - return optional(); + return it != data->properties.cend() ? it->second : NullValue(); } FeatureIdentifier AnnotationTileFeature::getID() const { diff --git a/src/mbgl/annotation/annotation_tile.hpp b/src/mbgl/annotation/annotation_tile.hpp index 7757f0b0ce..cc5a3dfb52 100644 --- a/src/mbgl/annotation/annotation_tile.hpp +++ b/src/mbgl/annotation/annotation_tile.hpp @@ -26,7 +26,7 @@ public: ~AnnotationTileFeature() override; FeatureType getType() const override; - optional getValue(const std::string&) const override; + Value getValue(const std::string&) const override; FeatureIdentifier getID() const override; const PropertyMap& getProperties() const override; GeometryCollection getGeometries() const override; diff --git a/src/mbgl/layout/symbol_feature.hpp b/src/mbgl/layout/symbol_feature.hpp index 0773c48d39..617e8c9ba2 100644 --- a/src/mbgl/layout/symbol_feature.hpp +++ b/src/mbgl/layout/symbol_feature.hpp @@ -17,7 +17,7 @@ public: {} FeatureType getType() const override { return feature->getType(); } - optional getValue(const std::string& key) const override { return feature->getValue(key); }; + Value getValue(const std::string& key) const override { return feature->getValue(key); }; const PropertyMap& getProperties() const override { return feature->getProperties(); }; FeatureIdentifier getID() const override { return feature->getID(); }; GeometryCollection getGeometries() const override { return geometry; }; diff --git a/src/mbgl/style/expression/compound_expression.cpp b/src/mbgl/style/expression/compound_expression.cpp index cc1d58025b..53512ce89e 100644 --- a/src/mbgl/style/expression/compound_expression.cpp +++ b/src/mbgl/style/expression/compound_expression.cpp @@ -234,8 +234,12 @@ Value featureIdAsExpressionValue(EvaluationContext params) { optional featurePropertyAsExpressionValue(EvaluationContext params, const std::string& key) { assert(params.feature); - auto property = params.feature->getValue(key); - return property ? toExpressionValue(*property) : optional(); + const PropertyMap& properties = params.feature->getProperties(); + const auto it = properties.find(key); + if (it == properties.cend()) { + return optional(); + } + return toExpressionValue(it->second); }; optional featureTypeAsString(FeatureType type) { @@ -255,9 +259,7 @@ optional featureTypeAsString(FeatureType type) { optional featurePropertyAsDouble(EvaluationContext params, const std::string& key) { assert(params.feature); - auto property = params.feature->getValue(key); - if (!property) return {}; - return property->match( + return params.feature->getValue(key).match( [](double value) { return value; }, [](uint64_t value) { return optional(static_cast(value)); }, [](int64_t value) { return optional(static_cast(value)); }, @@ -267,9 +269,7 @@ optional featurePropertyAsDouble(EvaluationContext params, const std::st optional featurePropertyAsString(EvaluationContext params, const std::string& key) { assert(params.feature); - auto property = params.feature->getValue(key); - if (!property) return {}; - return property->match( + return params.feature->getValue(key).match( [](std::string value) { return value; }, [](auto) { return optional(); } ); @@ -376,7 +376,8 @@ const auto& hasContextCompoundExpression() { }; } - return params.feature->getValue(key) ? true : false; + const PropertyMap& properties = params.feature->getProperties(); + return properties.find(key) != properties.cend(); }); return signature; } @@ -396,11 +397,12 @@ const auto& getContextCompoundExpression() { }; } - auto propertyValue = params.feature->getValue(key); - if (!propertyValue) { + const PropertyMap& properties = params.feature->getProperties(); + const auto it = properties.find(key); + if (it == properties.cend()) { return Null; } - return Value(toExpressionValue(*propertyValue)); + return toExpressionValue(it->second); }); return signature; } @@ -817,7 +819,8 @@ const auto& filterIdGreaterOrEqualThanStringCompoundExpression() { const auto& filterHasCompoundExpression() { static auto signature = detail::makeSignature("filter-has", [](const EvaluationContext& params, const std::string& key) -> Result { assert(params.feature); - return bool(params.feature->getValue(key)); + const PropertyMap& properties = params.feature->getProperties(); + return properties.find(key) != properties.cend(); }); return signature; } diff --git a/src/mbgl/style/expression/expression.cpp b/src/mbgl/style/expression/expression.cpp index bb03bcc2e5..8b61aef7ca 100644 --- a/src/mbgl/style/expression/expression.cpp +++ b/src/mbgl/style/expression/expression.cpp @@ -2,6 +2,8 @@ #include #include +#include + namespace mbgl { namespace style { namespace expression { @@ -18,12 +20,8 @@ public: const PropertyMap& getProperties() const override { return feature.properties; } FeatureIdentifier getID() const override { return feature.id; } GeometryCollection getGeometries() const override { return {}; } - optional getValue(const std::string& key) const override { - auto it = feature.properties.find(key); - if (it != feature.properties.end()) { - return optional(it->second); - } - return optional(); + mbgl::Value getValue(const std::string& key) const override { + return feature.properties.count(key) ? feature.properties.at(key) : NullValue(); } }; diff --git a/src/mbgl/tile/geojson_tile_data.hpp b/src/mbgl/tile/geojson_tile_data.hpp index 70a985db47..9325f1bb16 100644 --- a/src/mbgl/tile/geojson_tile_data.hpp +++ b/src/mbgl/tile/geojson_tile_data.hpp @@ -36,12 +36,8 @@ public: return geometry; } - optional getValue(const std::string& key) const override { - auto it = feature.properties.find(key); - if (it != feature.properties.end()) { - return optional(it->second); - } - return optional(); + Value getValue(const std::string& key) const override { + return feature.properties.count(key) ? feature.properties.at(key) : NullValue(); } }; diff --git a/src/mbgl/tile/geometry_tile_data.hpp b/src/mbgl/tile/geometry_tile_data.hpp index 3913615465..8200e51ad8 100644 --- a/src/mbgl/tile/geometry_tile_data.hpp +++ b/src/mbgl/tile/geometry_tile_data.hpp @@ -41,8 +41,8 @@ class GeometryTileFeature { public: virtual ~GeometryTileFeature() = default; virtual FeatureType getType() const = 0; - virtual optional getValue(const std::string& key) const = 0; - virtual const PropertyMap& getProperties() const { static PropertyMap empty; return empty; } + virtual Value getValue(const std::string& key) const = 0; + virtual const PropertyMap& getProperties() const = 0; virtual FeatureIdentifier getID() const { return NullValue {}; } virtual GeometryCollection getGeometries() const = 0; }; diff --git a/src/mbgl/tile/vector_tile_data.cpp b/src/mbgl/tile/vector_tile_data.cpp index 305f8e7dcf..be66dc0709 100644 --- a/src/mbgl/tile/vector_tile_data.cpp +++ b/src/mbgl/tile/vector_tile_data.cpp @@ -21,9 +21,8 @@ FeatureType VectorTileFeature::getType() const { } } -optional VectorTileFeature::getValue(const std::string& key) const { - const optional value(feature.getValue(key)); - return value->is() ? nullopt : std::move(value); +Value VectorTileFeature::getValue(const std::string& key) const { + return feature.getValue(key); } const PropertyMap& VectorTileFeature::getProperties() const { diff --git a/src/mbgl/tile/vector_tile_data.hpp b/src/mbgl/tile/vector_tile_data.hpp index 7a169ac44c..ace20bcfc4 100644 --- a/src/mbgl/tile/vector_tile_data.hpp +++ b/src/mbgl/tile/vector_tile_data.hpp @@ -14,7 +14,7 @@ public: VectorTileFeature(const mapbox::vector_tile::layer&, const protozero::data_view&); FeatureType getType() const override; - optional getValue(const std::string& key) const override; + Value getValue(const std::string& key) const override; const PropertyMap& getProperties() const override; FeatureIdentifier getID() const override; GeometryCollection getGeometries() const override; diff --git a/test/tile/vector_tile.test.cpp b/test/tile/vector_tile.test.cpp index ed3eda7863..07110fdd7f 100644 --- a/test/tile/vector_tile.test.cpp +++ b/test/tile/vector_tile.test.cpp @@ -103,7 +103,7 @@ TEST(VectorTileData, ParseResults) { std::unordered_map properties = feature->getProperties(); ASSERT_EQ(properties.size(), 3u); - ASSERT_EQ(properties.at("disputed"), *feature->getValue("disputed")); + ASSERT_EQ(properties.at("disputed"), feature->getValue("disputed")); - ASSERT_EQ(feature->getValue("invalid"), nullopt); -} \ No newline at end of file + ASSERT_EQ(feature->getValue("invalid"), NullValue()); +} -- cgit v1.2.1