optional<Error> setPaintProperties(Layer& layer, const V& value) { auto paintValue = objectMember(value, "paint"); if (!paintValue) { return {}; } return eachMember(*paintValue, [&] (const std::string& k, const V& v) { return setPaintProperty(layer, k, v); }); }
optional<Error> setPaintProperties(Layer& layer, const Convertible& value) { auto paintValue = objectMember(value, "paint"); if (!paintValue) { return nullopt; } if (!isObject(*paintValue)) { return { { "paint must be an object" } }; } return eachMember(*paintValue, [&] (const std::string& k, const Convertible& v) { return layer.setPaintProperty(k, v); }); }
optional<std::unique_ptr<Layer>> operator()(const V& value, Error& error) const { if (!isObject(value)) { error = { "layer must be an object" }; return {}; } auto idValue = objectMember(value, "id"); if (!idValue) { error = { "layer must have an id" }; return {}; } optional<std::string> id = toString(*idValue); if (!id) { error = { "layer id must be a string" }; return {}; } auto typeValue = objectMember(value, "type"); if (!typeValue) { error = { "layer must have a type" }; return {}; } optional<std::string> type = toString(*typeValue); if (!type) { error = { "layer type must be a string" }; return {}; } optional<std::unique_ptr<Layer>> converted; if (*type == "fill") { converted = convertVectorLayer<FillLayer>(*id, value, error); } else if (*type == "fill-extrusion") { converted = convertVectorLayer<FillExtrusionLayer>(*id, value, error); } else if (*type == "line") { converted = convertVectorLayer<LineLayer>(*id, value, error); } else if (*type == "circle") { converted = convertVectorLayer<CircleLayer>(*id, value, error); } else if (*type == "symbol") { converted = convertVectorLayer<SymbolLayer>(*id, value, error); } else if (*type == "raster") { converted = convertRasterLayer(*id, value, error); } else if (*type == "background") { converted = convertBackgroundLayer(*id, value, error); } else { error = { "invalid layer type" }; return {}; } if (!converted) { return converted; } std::unique_ptr<Layer> layer = std::move(*converted); auto minzoomValue = objectMember(value, "minzoom"); if (minzoomValue) { optional<float> minzoom = toNumber(*minzoomValue); if (!minzoom) { error = { "minzoom must be numeric" }; return {}; } layer->setMinZoom(*minzoom); } auto maxzoomValue = objectMember(value, "maxzoom"); if (maxzoomValue) { optional<float> maxzoom = toNumber(*maxzoomValue); if (!maxzoom) { error = { "maxzoom must be numeric" }; return {}; } layer->setMaxZoom(*maxzoom); } auto layoutValue = objectMember(value, "layout"); if (layoutValue) { if (!isObject(*layoutValue)) { error = { "layout must be an object" }; return {}; } optional<Error> error_ = eachMember(*layoutValue, [&] (const std::string& k, const V& v) { return setLayoutProperty(*layer, k, v); }); if (error_) { error = *error_; return {}; } } optional<Error> error_ = setPaintProperties(*layer, value); if (error_) { error = *error_; return {}; } return std::move(layer); }
optional<std::unique_ptr<Layer>> Converter<std::unique_ptr<Layer>>::operator()(const Convertible& value, Error& error) const { if (!isObject(value)) { error.message = "layer must be an object"; return nullopt; } auto idValue = objectMember(value, "id"); if (!idValue) { error.message = "layer must have an id"; return nullopt; } optional<std::string> id = toString(*idValue); if (!id) { error.message = "layer id must be a string"; return nullopt; } auto typeValue = objectMember(value, "type"); if (!typeValue) { error.message = "layer must have a type"; return nullopt; } optional<std::string> type = toString(*typeValue); if (!type) { error.message = "layer type must be a string"; return nullopt; } std::unique_ptr<Layer> layer = LayerManager::get()->createLayer(*type, *id, value, error); if (!layer) { return nullopt; } auto minzoomValue = objectMember(value, "minzoom"); if (minzoomValue) { optional<float> minzoom = toNumber(*minzoomValue); if (!minzoom) { error.message = "minzoom must be numeric"; return nullopt; } layer->setMinZoom(*minzoom); } auto maxzoomValue = objectMember(value, "maxzoom"); if (maxzoomValue) { optional<float> maxzoom = toNumber(*maxzoomValue); if (!maxzoom) { error.message = "maxzoom must be numeric"; return nullopt; } layer->setMaxZoom(*maxzoom); } auto layoutValue = objectMember(value, "layout"); if (layoutValue) { if (!isObject(*layoutValue)) { error.message = "layout must be an object"; return nullopt; } optional<Error> error_ = eachMember(*layoutValue, [&] (const std::string& k, const Convertible& v) { return layer->setLayoutProperty(k, v); }); if (error_) { error = *error_; return nullopt; } } optional<Error> error_ = setPaintProperties(*layer, value); if (error_) { error = *error_; return nullopt; } return std::move(layer); }