From 132b68e4fc277da10e2fbc457e54931e1c8ffd4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 8 Dec 2015 16:36:33 -0800 Subject: [PATCH] [core] split Source parsing into separate functions --- src/mbgl/style/style_parser.cpp | 94 ++++++++++++++++++++++++--------- src/mbgl/style/style_parser.hpp | 2 + 2 files changed, 70 insertions(+), 26 deletions(-) diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index a22615d43..d029f633a 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -73,41 +73,83 @@ void StyleParser::parseSources(const JSVal& value) { source->info.type = SourceTypeClass({ typeVal.GetString(), typeVal.GetStringLength() }); - if (sourceVal.HasMember("url")) { - const JSVal& urlVal = sourceVal["url"]; - - if (!urlVal.IsString()) { - Log::Warning(Event::ParseStyle, "source url must be a string"); + switch (source->info.type) { + case SourceType::Vector: + if (!parseVectorSource(*source, sourceVal)) { continue; } - - source->info.url = { urlVal.GetString(), urlVal.GetStringLength() }; + break; + case SourceType::Raster: + if (!parseRasterSource(*source, sourceVal)) { + continue; + } + break; + default: + Log::Warning(Event::ParseStyle, "source type %s is not supported", SourceTypeClass(source->info.type).c_str()); } - if (sourceVal.HasMember("tileSize")) { - const JSVal& tileSizeVal = sourceVal["tileSize"]; - - if (!tileSizeVal.IsUint()) { - Log::Warning(Event::ParseStyle, "source tileSize must be an unsigned integer"); - continue; - } - - unsigned int intValue = tileSizeVal.GetUint(); - if (intValue > std::numeric_limits::max()) { - Log::Warning(Event::ParseStyle, "values for tileSize that are larger than %d are not supported", std::numeric_limits::max()); - continue; - } - - source->info.tile_size = intValue; - } - - source->info.parseTileJSONProperties(sourceVal); - sourcesMap.emplace(source->info.source_id, source.get()); sources.emplace_back(std::move(source)); } } +bool StyleParser::parseVectorSource(Source& source, const JSVal& sourceVal) { + // A vector tile source either specifies the URL of a TileJSON file... + if (sourceVal.HasMember("url")) { + const JSVal& urlVal = sourceVal["url"]; + + if (!urlVal.IsString()) { + Log::Warning(Event::ParseStyle, "source url must be a string"); + return false; + } + + source.info.url = { urlVal.GetString(), urlVal.GetStringLength() }; + + } else { + // ...or the TileJSON directly. + source.info.parseTileJSONProperties(sourceVal); + } + + return true; +} + +bool StyleParser::parseRasterSource(Source& source, const JSVal& sourceVal) { + if (sourceVal.HasMember("tileSize")) { + const JSVal& tileSizeVal = sourceVal["tileSize"]; + + if (!tileSizeVal.IsUint()) { + Log::Warning(Event::ParseStyle, "source tileSize must be an unsigned integer"); + return false; + } + + unsigned int intValue = tileSizeVal.GetUint(); + if (intValue > std::numeric_limits::max()) { + Log::Warning(Event::ParseStyle, "values for tileSize that are larger than %d are not supported", std::numeric_limits::max()); + return false; + } + + source.info.tile_size = intValue; + } + + // A raster tile source either specifies the URL of a TileJSON file... + if (sourceVal.HasMember("url")) { + const JSVal& urlVal = sourceVal["url"]; + + if (!urlVal.IsString()) { + Log::Warning(Event::ParseStyle, "source url must be a string"); + return false; + } + + source.info.url = { urlVal.GetString(), urlVal.GetStringLength() }; + + } else { + // ...or the TileJSON directly. + source.info.parseTileJSONProperties(sourceVal); + } + + return true; +} + void StyleParser::parseLayers(const JSVal& value) { std::vector ids; diff --git a/src/mbgl/style/style_parser.hpp b/src/mbgl/style/style_parser.hpp index 2c5a43065..2307517e3 100644 --- a/src/mbgl/style/style_parser.hpp +++ b/src/mbgl/style/style_parser.hpp @@ -33,6 +33,8 @@ public: private: void parseSources(const JSVal&); + bool parseVectorSource(Source&, const JSVal&); + bool parseRasterSource(Source&, const JSVal&); void parseLayers(const JSVal&); void parseLayer(const std::string& id, const JSVal&, std::unique_ptr&); void parseVisibility(StyleLayer&, const JSVal& value);