[core] split Source parsing into separate functions

This commit is contained in:
Konstantin Käfer
2015-12-08 16:36:33 -08:00
parent 2b9f20461a
commit 132b68e4fc
2 changed files with 70 additions and 26 deletions

View File

@@ -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<uint16_t>::max()) {
Log::Warning(Event::ParseStyle, "values for tileSize that are larger than %d are not supported", std::numeric_limits<uint16_t>::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<uint16_t>::max()) {
Log::Warning(Event::ParseStyle, "values for tileSize that are larger than %d are not supported", std::numeric_limits<uint16_t>::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<std::string> ids;

View File

@@ -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<StyleLayer>&);
void parseVisibility(StyleLayer&, const JSVal& value);