diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2020-05-18 14:43:46 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2020-05-26 20:35:05 +0300 |
commit | 5b3f7b3c9e7d6cb378fbaea24aac0c3ca2a60032 (patch) | |
tree | 92833f909f7d4fa8c006c4756a2e8021f734c16c | |
parent | 4345bf23fbeee2b450e93a72c910233d31c8edbe (diff) | |
download | qtlocation-mapboxgl-5b3f7b3c9e7d6cb378fbaea24aac0c3ca2a60032.tar.gz |
[core] Static default property values API for sources
Introduce
`Value SourceManager::getPropertyDefaultValue(const std::string& type, const std::string& property)`
27 files changed, 97 insertions, 76 deletions
diff --git a/include/mbgl/sourcemanager/annotation_source_factory.hpp b/include/mbgl/sourcemanager/annotation_source_factory.hpp index 0ea98bab99..4237d0c9d4 100644 --- a/include/mbgl/sourcemanager/annotation_source_factory.hpp +++ b/include/mbgl/sourcemanager/annotation_source_factory.hpp @@ -11,6 +11,7 @@ protected: const style::conversion::Convertible& value, style::conversion::Error&) noexcept final; std::unique_ptr<RenderSource> createRenderSource(Immutable<style::Source::Impl>) noexcept final; + Value getPropertyDefaultValue(const std::string& property) noexcept final; }; } // namespace mbgl diff --git a/include/mbgl/sourcemanager/custom_geometry_source_factory.hpp b/include/mbgl/sourcemanager/custom_geometry_source_factory.hpp index 12d8234182..9f1e1809be 100644 --- a/include/mbgl/sourcemanager/custom_geometry_source_factory.hpp +++ b/include/mbgl/sourcemanager/custom_geometry_source_factory.hpp @@ -11,6 +11,7 @@ protected: const style::conversion::Convertible& value, style::conversion::Error&) noexcept final; std::unique_ptr<RenderSource> createRenderSource(Immutable<style::Source::Impl>) noexcept final; + Value getPropertyDefaultValue(const std::string& property) noexcept final; }; } // namespace mbgl diff --git a/include/mbgl/sourcemanager/geojson_source_factory.hpp b/include/mbgl/sourcemanager/geojson_source_factory.hpp index be6e64a6bc..9d325136fe 100644 --- a/include/mbgl/sourcemanager/geojson_source_factory.hpp +++ b/include/mbgl/sourcemanager/geojson_source_factory.hpp @@ -11,6 +11,7 @@ protected: const style::conversion::Convertible& value, style::conversion::Error&) noexcept final; std::unique_ptr<RenderSource> createRenderSource(Immutable<style::Source::Impl>) noexcept final; + Value getPropertyDefaultValue(const std::string& property) noexcept final; }; } // namespace mbgl diff --git a/include/mbgl/sourcemanager/image_source_factory.hpp b/include/mbgl/sourcemanager/image_source_factory.hpp index c560cb112d..c484126525 100644 --- a/include/mbgl/sourcemanager/image_source_factory.hpp +++ b/include/mbgl/sourcemanager/image_source_factory.hpp @@ -11,6 +11,7 @@ protected: const style::conversion::Convertible& value, style::conversion::Error&) noexcept final; std::unique_ptr<RenderSource> createRenderSource(Immutable<style::Source::Impl>) noexcept final; + Value getPropertyDefaultValue(const std::string& property) noexcept final; }; } // namespace mbgl diff --git a/include/mbgl/sourcemanager/raster_dem_source_factory.hpp b/include/mbgl/sourcemanager/raster_dem_source_factory.hpp index a01b26eb0d..eb7c9630e9 100644 --- a/include/mbgl/sourcemanager/raster_dem_source_factory.hpp +++ b/include/mbgl/sourcemanager/raster_dem_source_factory.hpp @@ -11,6 +11,7 @@ protected: const style::conversion::Convertible& value, style::conversion::Error&) noexcept final; std::unique_ptr<RenderSource> createRenderSource(Immutable<style::Source::Impl>) noexcept final; + Value getPropertyDefaultValue(const std::string& property) noexcept final; }; } // namespace mbgl diff --git a/include/mbgl/sourcemanager/raster_source_factory.hpp b/include/mbgl/sourcemanager/raster_source_factory.hpp index d5f88e65d9..fb7fa1e09b 100644 --- a/include/mbgl/sourcemanager/raster_source_factory.hpp +++ b/include/mbgl/sourcemanager/raster_source_factory.hpp @@ -11,6 +11,7 @@ protected: const style::conversion::Convertible& value, style::conversion::Error&) noexcept final; std::unique_ptr<RenderSource> createRenderSource(Immutable<style::Source::Impl>) noexcept final; + Value getPropertyDefaultValue(const std::string& property) noexcept final; }; } // namespace mbgl diff --git a/include/mbgl/sourcemanager/source_factory.hpp b/include/mbgl/sourcemanager/source_factory.hpp index 31d392163b..202768b946 100644 --- a/include/mbgl/sourcemanager/source_factory.hpp +++ b/include/mbgl/sourcemanager/source_factory.hpp @@ -32,6 +32,9 @@ public: /// Returns a new RenderSource instance. virtual std::unique_ptr<RenderSource> createRenderSource(Immutable<style::Source::Impl>) noexcept = 0; + + /// Returns the default value for the given source property + virtual Value getPropertyDefaultValue(const std::string& property) noexcept = 0; }; } // namespace mbgl diff --git a/include/mbgl/sourcemanager/source_manager.hpp b/include/mbgl/sourcemanager/source_manager.hpp index 4e4dea06ff..afeadc1355 100644 --- a/include/mbgl/sourcemanager/source_manager.hpp +++ b/include/mbgl/sourcemanager/source_manager.hpp @@ -40,6 +40,9 @@ public: /// Returns a new RenderSource instance on success call; returns `nullptr` otherwise. std::unique_ptr<RenderSource> createRenderSource(Immutable<style::Source::Impl>) noexcept; + /// Returns the default value for the given source type and property name. + Value getPropertyDefaultValue(const std::string& type, const std::string& property); + protected: virtual ~SourceManager() = default; virtual SourceFactory* getFactory(const std::string& type) noexcept = 0; diff --git a/include/mbgl/sourcemanager/vector_source_factory.hpp b/include/mbgl/sourcemanager/vector_source_factory.hpp index 05cae49af9..ec7f743bc8 100644 --- a/include/mbgl/sourcemanager/vector_source_factory.hpp +++ b/include/mbgl/sourcemanager/vector_source_factory.hpp @@ -11,6 +11,7 @@ protected: const style::conversion::Convertible& value, style::conversion::Error&) noexcept final; std::unique_ptr<RenderSource> createRenderSource(Immutable<style::Source::Impl>) noexcept final; + Value getPropertyDefaultValue(const std::string& property) noexcept final; }; } // namespace mbgl diff --git a/include/mbgl/style/source.hpp b/include/mbgl/style/source.hpp index 409384d5df..060bc0878c 100644 --- a/include/mbgl/style/source.hpp +++ b/include/mbgl/style/source.hpp @@ -166,7 +166,7 @@ public: optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value); Value getProperty(const std::string&) const; - Value getPropertyDefaultValue(const std::string&) const; + static Value getPropertyDefaultValue(const std::string&); virtual mapbox::base::WeakPtr<Source> makeWeakPtr() = 0; const SourceTypeInfo* getTypeInfo() const noexcept; @@ -180,7 +180,6 @@ protected: virtual optional<conversion::Error> setPropertyInternal(const std::string& name, const conversion::Convertible& value); virtual Value getPropertyInternal(const std::string&) const; - virtual Value getPropertyDefaultValueInternal(const std::string&) const; }; } // namespace style diff --git a/include/mbgl/style/sources/geojson_source.hpp b/include/mbgl/style/sources/geojson_source.hpp index 4366dcf858..5679007543 100644 --- a/include/mbgl/style/sources/geojson_source.hpp +++ b/include/mbgl/style/sources/geojson_source.hpp @@ -81,11 +81,12 @@ public: Value serialize() const override; + static Value getPropertyDefaultValue(const std::string&); + protected: optional<conversion::Error> setPropertyInternal(const std::string& name, const conversion::Convertible& value) override; Value getPropertyInternal(const std::string&) const override; - Value getPropertyDefaultValueInternal(const std::string&) const override; Mutable<Source::Impl> createMutable() const noexcept final; private: diff --git a/include/mbgl/style/sources/raster_dem_source.hpp b/include/mbgl/style/sources/raster_dem_source.hpp index dc068886e5..5870368b73 100644 --- a/include/mbgl/style/sources/raster_dem_source.hpp +++ b/include/mbgl/style/sources/raster_dem_source.hpp @@ -20,9 +20,10 @@ public: Value serialize() const override; + static Value getPropertyDefaultValue(const std::string&); + protected: Value getPropertyInternal(const std::string&) const override; - Value getPropertyDefaultValueInternal(const std::string&) const override; Mutable<Source::Impl> createMutable() const noexcept final; Mutable<Source::Impl> createMutable(Tileset tileset) const noexcept final; }; diff --git a/include/mbgl/style/sources/tileset_source.hpp b/include/mbgl/style/sources/tileset_source.hpp index 46b255808c..3fd3ab797a 100644 --- a/include/mbgl/style/sources/tileset_source.hpp +++ b/include/mbgl/style/sources/tileset_source.hpp @@ -17,11 +17,11 @@ public: optional<std::string> getURL() const; Value serialize() const override; + static Value getPropertyDefaultValue(const std::string&); protected: TilesetSource(Immutable<Impl>, variant<std::string, Tileset> urlOrTileset); Value getPropertyInternal(const std::string&) const override; - Value getPropertyDefaultValueInternal(const std::string&) const override; const variant<std::string, Tileset> urlOrTileset; }; diff --git a/src/mbgl/sourcemanager/annotation_source_factory.cpp b/src/mbgl/sourcemanager/annotation_source_factory.cpp index a5d1986efd..b55655e1f8 100644 --- a/src/mbgl/sourcemanager/annotation_source_factory.cpp +++ b/src/mbgl/sourcemanager/annotation_source_factory.cpp @@ -20,4 +20,8 @@ std::unique_ptr<RenderSource> AnnotationSourceFactory::createRenderSource( return std::make_unique<RenderAnnotationSource>(staticImmutableCast<AnnotationSource::Impl>(impl)); } +Value AnnotationSourceFactory::getPropertyDefaultValue(const std::string& property) noexcept { + return AnnotationSource::getPropertyDefaultValue(property); +} + } // namespace mbgl diff --git a/src/mbgl/sourcemanager/custom_geometry_source_factory.cpp b/src/mbgl/sourcemanager/custom_geometry_source_factory.cpp index 054aa15bd5..51a738b3ca 100644 --- a/src/mbgl/sourcemanager/custom_geometry_source_factory.cpp +++ b/src/mbgl/sourcemanager/custom_geometry_source_factory.cpp @@ -21,4 +21,8 @@ std::unique_ptr<RenderSource> CustomGeometrySourceFactory::createRenderSource( return std::make_unique<RenderCustomGeometrySource>(staticImmutableCast<style::CustomGeometrySource::Impl>(impl)); } +Value CustomGeometrySourceFactory::getPropertyDefaultValue(const std::string& property) noexcept { + return style::CustomGeometrySource::getPropertyDefaultValue(property); +} + } // namespace mbgl diff --git a/src/mbgl/sourcemanager/geojson_source_factory.cpp b/src/mbgl/sourcemanager/geojson_source_factory.cpp index 0a1ae75351..0edfaa8f2e 100644 --- a/src/mbgl/sourcemanager/geojson_source_factory.cpp +++ b/src/mbgl/sourcemanager/geojson_source_factory.cpp @@ -40,4 +40,8 @@ std::unique_ptr<RenderSource> GeoJSONSourceFactory::createRenderSource(Immutable return std::make_unique<RenderGeoJSONSource>(staticImmutableCast<style::GeoJSONSource::Impl>(impl)); } +Value GeoJSONSourceFactory::getPropertyDefaultValue(const std::string& property) noexcept { + return style::GeoJSONSource::getPropertyDefaultValue(property); +} + } // namespace mbgl diff --git a/src/mbgl/sourcemanager/image_source_factory.cpp b/src/mbgl/sourcemanager/image_source_factory.cpp index f3de36bcf9..8ccd32695c 100644 --- a/src/mbgl/sourcemanager/image_source_factory.cpp +++ b/src/mbgl/sourcemanager/image_source_factory.cpp @@ -46,4 +46,8 @@ std::unique_ptr<RenderSource> ImageSourceFactory::createRenderSource(Immutable<s return std::make_unique<RenderImageSource>(staticImmutableCast<style::ImageSource::Impl>(impl)); } +Value ImageSourceFactory::getPropertyDefaultValue(const std::string& property) noexcept { + return style::ImageSource::getPropertyDefaultValue(property); +} + } // namespace mbgl diff --git a/src/mbgl/sourcemanager/raster_dem_source_factory.cpp b/src/mbgl/sourcemanager/raster_dem_source_factory.cpp index 119a7eae27..fdc9163385 100644 --- a/src/mbgl/sourcemanager/raster_dem_source_factory.cpp +++ b/src/mbgl/sourcemanager/raster_dem_source_factory.cpp @@ -39,4 +39,8 @@ std::unique_ptr<RenderSource> RasterDEMSourceFactory::createRenderSource(Immutab return std::make_unique<RenderRasterDEMSource>(staticImmutableCast<style::RasterDEMSource::Impl>(impl)); } +Value RasterDEMSourceFactory::getPropertyDefaultValue(const std::string& property) noexcept { + return style::RasterDEMSource::getPropertyDefaultValue(property); +} + } // namespace mbgl diff --git a/src/mbgl/sourcemanager/raster_source_factory.cpp b/src/mbgl/sourcemanager/raster_source_factory.cpp index a06595059d..a1015f4bd8 100644 --- a/src/mbgl/sourcemanager/raster_source_factory.cpp +++ b/src/mbgl/sourcemanager/raster_source_factory.cpp @@ -40,4 +40,8 @@ std::unique_ptr<RenderSource> RasterSourceFactory::createRenderSource(Immutable< return std::make_unique<RenderRasterSource>(staticImmutableCast<style::RasterSource::Impl>(impl)); } +Value RasterSourceFactory::getPropertyDefaultValue(const std::string& property) noexcept { + return style::RasterSource::getPropertyDefaultValue(property); +} + } // namespace mbgl diff --git a/src/mbgl/sourcemanager/source_manager.cpp b/src/mbgl/sourcemanager/source_manager.cpp index 16241b41bc..0c69c2bb12 100644 --- a/src/mbgl/sourcemanager/source_manager.cpp +++ b/src/mbgl/sourcemanager/source_manager.cpp @@ -24,4 +24,11 @@ std::unique_ptr<RenderSource> SourceManager::createRenderSource(Immutable<style: return factory->createRenderSource(std::move(impl)); } +Value SourceManager::getPropertyDefaultValue(const std::string& type, const std::string& property) { + if (SourceFactory* factory = getFactory(type)) { + return factory->getPropertyDefaultValue(property); + } + return NullValue(); +} + } // namespace mbgl diff --git a/src/mbgl/sourcemanager/vector_source_factory.cpp b/src/mbgl/sourcemanager/vector_source_factory.cpp index efc6f3144f..30c19bfcff 100644 --- a/src/mbgl/sourcemanager/vector_source_factory.cpp +++ b/src/mbgl/sourcemanager/vector_source_factory.cpp @@ -46,4 +46,8 @@ std::unique_ptr<RenderSource> VectorSourceFactory::createRenderSource(Immutable< return std::make_unique<RenderVectorSource>(staticImmutableCast<style::VectorSource::Impl>(impl)); } +Value VectorSourceFactory::getPropertyDefaultValue(const std::string& property) noexcept { + return style::VectorSource::getPropertyDefaultValue(property); +} + } // namespace mbgl diff --git a/src/mbgl/style/source.cpp b/src/mbgl/style/source.cpp index dd3ec4caa6..217b4d00e6 100644 --- a/src/mbgl/style/source.cpp +++ b/src/mbgl/style/source.cpp @@ -188,13 +188,13 @@ Value Source::getProperty(const std::string& name) const { } return getPropertyInternal(name); } - -Value Source::getPropertyDefaultValue(const std::string& name) const { +// static +Value Source::getPropertyDefaultValue(const std::string& name) { const auto it = sourceProperties.find(name.c_str()); if (it != sourceProperties.end()) { return getSourcePropertyDefaultValue(static_cast<Property>(it->second)); } - return getPropertyDefaultValueInternal(name); + return Value(); } const SourceTypeInfo* Source::getTypeInfo() const noexcept { @@ -220,9 +220,5 @@ Value Source::getPropertyInternal(const std::string&) const { return NullValue(); } -Value Source::getPropertyDefaultValueInternal(const std::string&) const { - return NullValue(); -} - } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp index 2ac80517af..f6dfc21e72 100644 --- a/src/mbgl/style/sources/geojson_source.cpp +++ b/src/mbgl/style/sources/geojson_source.cpp @@ -162,10 +162,11 @@ Value GeoJSONSource::getPropertyInternal(const std::string& name) const { return Value(); } -Value GeoJSONSource::getPropertyDefaultValueInternal(const std::string& name) const { +// static +Value GeoJSONSource::getPropertyDefaultValue(const std::string& name) { using namespace conversion; if (name == "options") return makeValue(*GeoJSONOptions::defaultOptions()); - return Value(); + return Source::getPropertyDefaultValue(name); } Value GeoJSONSource::serialize() const { diff --git a/src/mbgl/style/sources/raster_dem_source.cpp b/src/mbgl/style/sources/raster_dem_source.cpp index 95dc7d1edd..149413a13a 100644 --- a/src/mbgl/style/sources/raster_dem_source.cpp +++ b/src/mbgl/style/sources/raster_dem_source.cpp @@ -44,11 +44,12 @@ Value RasterDEMSource::getPropertyInternal(const std::string& name) const { return NullValue(); } -Value RasterDEMSource::getPropertyDefaultValueInternal(const std::string& name) const { +// static +Value RasterDEMSource::getPropertyDefaultValue(const std::string& name) { if (name == "encoding") { return conversion::makeValue(TilesetDEMEncoding::Mapbox); } - return NullValue(); + return RasterSource::getPropertyDefaultValue(name); } Mutable<Source::Impl> RasterDEMSource::createMutable() const noexcept { diff --git a/src/mbgl/style/sources/tileset_source.cpp b/src/mbgl/style/sources/tileset_source.cpp index 45bad4e010..fa9e9166bb 100644 --- a/src/mbgl/style/sources/tileset_source.cpp +++ b/src/mbgl/style/sources/tileset_source.cpp @@ -76,12 +76,13 @@ Value TilesetSource::getPropertyInternal(const std::string& name) const { return Value(); } -Value TilesetSource::getPropertyDefaultValueInternal(const std::string& name) const { +// static +Value TilesetSource::getPropertyDefaultValue(const std::string& name) { using namespace conversion; if (name == "scheme") return makeValue(TilesetScheme::XYZ); if (name == "minzoom") return 0u; if (name == "maxzoom") return util::DEFAULT_MAX_ZOOM; - return Value(); + return Source::getPropertyDefaultValue(name); } } // namespace style diff --git a/test/style/conversion/source.test.cpp b/test/style/conversion/source.test.cpp index 88b8aa0b12..a8716a4c20 100644 --- a/test/style/conversion/source.test.cpp +++ b/test/style/conversion/source.test.cpp @@ -22,13 +22,6 @@ void checkGetProperty(std::unique_ptr<Source>& source, const std::string& proper EXPECT_EQ(expected, value) << "get property: " << propertyName; } -void checkGetPropertyDefaultValue(std::unique_ptr<Source>& source, - const std::string& propertyName, - const mbgl::Value& expected) { - Value value = source->getPropertyDefaultValue(propertyName); - EXPECT_EQ(expected, value) << "get property: " << propertyName; -} - void checkSetProperty(std::unique_ptr<Source>& source, const std::string& propertyName, const JSValue& value) { auto error = source->setProperty(propertyName, Convertible(&value)); EXPECT_EQ(nullopt, error) << "set property: " << propertyName << ", error: " << error->message; @@ -91,54 +84,3 @@ TEST(StyleConversion, SetSourceGenericProperties) { checkSetProperty(source, "data", *geometry); EXPECT_TRUE(geojsonSource->getGeoJSONData()); } - -TEST(StyleConversion, GetSourceGenericPropertyDefaultValues) { - auto source = parseSource(R"JSON({ - "type": "vector", - "tiles": ["http://example.com/{z}-{x}-{y}.vector.pbf"], - "scheme": "tms", - "minzoom": 11, - "maxzoom": 16, - "attribution": "mapbox", - "bounds": [-180, -73, -120, 73] - })JSON", - "vector_source"); - - ASSERT_NE(nullptr, source); - checkGetPropertyDefaultValue(source, "volatile", false); - checkGetPropertyDefaultValue(source, "minimum-tile-update-interval", 0.0); - checkGetPropertyDefaultValue(source, "prefetch-zoom-delta", 4u); - checkGetPropertyDefaultValue(source, "minzoom", 0u); - checkGetPropertyDefaultValue(source, "maxzoom", 22u); - checkGetPropertyDefaultValue(source, "scheme", "xyz"); - - source = parseSource(R"JSON({ - "type": "geojson", - "data": "http://127.0.0.1:3000/geojson.json" - })JSON", - "geojson_source"); - - ASSERT_NE(nullptr, source); - mapbox::base::ValueObject expected; - expected["minzoom"] = 0u; - expected["maxzoom"] = 18u; - expected["tileSize"] = 512u; - expected["buffer"] = 128u; - expected["tolerance"] = 0.375; - expected["lineMetrics"] = false; - expected["cluster"] = false; - expected["clusterRadius"] = 50u; - expected["clusterMaxZoom"] = 17u; - checkGetPropertyDefaultValue(source, "options", expected); - - source = parseSource(R"JSON({ - "type": "raster-dem", - "encoding": "terrarium", - "tiles": [ - "local://tiles/{z}-{x}-{y}.terrain.png" - ] - })JSON", - "rasterdem_source"); - ASSERT_NE(nullptr, source); - checkGetPropertyDefaultValue(source, "encoding", "mapbox"); -} diff --git a/test/style/source.test.cpp b/test/style/source.test.cpp index bc19b9e9d1..f3a9f737d8 100644 --- a/test/style/source.test.cpp +++ b/test/style/source.test.cpp @@ -985,3 +985,34 @@ TEST(Source, SetMaxParentOverscaleFactor) { {EventSeverity::Warning, Event::Style, -1, "Parent tile overscale factor will cap prefetch delta to 3"})); EXPECT_EQ(0u, log.uncheckedCount()); } + +namespace { + +void checkGetPropertyDefaultValue(const std::string& sourceType, const std::string& property, const Value& expected) { + auto value = SourceManager::get()->getPropertyDefaultValue(sourceType, property); + EXPECT_EQ(expected, value); +} + +} // namespace + +TEST(Source, GetSourceGenericPropertyDefaultValues) { + checkGetPropertyDefaultValue("vector", "volatile", false); + checkGetPropertyDefaultValue("vector", "minimum-tile-update-interval", 0.0); + checkGetPropertyDefaultValue("vector", "prefetch-zoom-delta", 4u); + checkGetPropertyDefaultValue("vector", "minzoom", 0u); + checkGetPropertyDefaultValue("vector", "maxzoom", 22u); + checkGetPropertyDefaultValue("vector", "scheme", "xyz"); + + mapbox::base::ValueObject expected; + expected["minzoom"] = 0u; + expected["maxzoom"] = 18u; + expected["tileSize"] = 512u; + expected["buffer"] = 128u; + expected["tolerance"] = 0.375; + expected["lineMetrics"] = false; + expected["cluster"] = false; + expected["clusterRadius"] = 50u; + expected["clusterMaxZoom"] = 17u; + checkGetPropertyDefaultValue("geojson", "options", expected); + checkGetPropertyDefaultValue("raster-dem", "encoding", "mapbox"); +}
\ No newline at end of file |