Lazy load distribution.current_series

main
Simon Quigley 6 days ago
parent f6c2b58e47
commit 5a47dd5c8d

@ -24,7 +24,26 @@ distribution::distribution()
title(""), title(""),
summary(""), summary(""),
domain_name(""), domain_name(""),
lp(nullptr) {} lp(nullptr),
current_series([this]() -> std::optional<distro_series> {
if (current_series_link.empty()) return std::nullopt;
if (_current_series) return _current_series;
auto response = lp ? lp->api_get(current_series_link) : std::nullopt;
if (!response) return std::nullopt;
auto data = nlohmann::json::parse(response.value());
auto ds = distro_series::parse(data.dump());
if (ds) {
ds->set_lp(lp);
_current_series = ds;
return ds;
}
return std::nullopt;
})
{}
distribution::~distribution() {} distribution::~distribution() {}
@ -47,6 +66,7 @@ void distribution::parse_json(const std::string& json_data) {
{"summary", [this](const nlohmann::json& val) { summary = val.get<std::string>(); }}, {"summary", [this](const nlohmann::json& val) { summary = val.get<std::string>(); }},
{"domain_name", [this](const nlohmann::json& val) { domain_name = val.get<std::string>(); }}, {"domain_name", [this](const nlohmann::json& val) { domain_name = val.get<std::string>(); }},
{"self_link", [this](const nlohmann::json& val) { self_link = val.get<std::string>(); }}, {"self_link", [this](const nlohmann::json& val) { self_link = val.get<std::string>(); }},
{"current_series_link", [this](const nlohmann::json& val) { current_series_link = val.get<std::string>(); }},
{"web_link", [this](const nlohmann::json& val) { web_link = val.get<std::string>(); }}, {"web_link", [this](const nlohmann::json& val) { web_link = val.get<std::string>(); }},
{"translationgroup_link", [this](const nlohmann::json& val) { translationgroup_link = val.get<std::string>(); }} {"translationgroup_link", [this](const nlohmann::json& val) { translationgroup_link = val.get<std::string>(); }}
}; };

@ -21,6 +21,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "launchpad.h" #include "launchpad.h"
#include "distro_series.h" #include "distro_series.h"
#include "lazy_optional.h"
#ifndef LAUNCHPAD_API #ifndef LAUNCHPAD_API
#ifdef BUILDING_LAUNCHPAD #ifdef BUILDING_LAUNCHPAD
@ -63,13 +64,16 @@ public:
std::string archive_mirrors_collection_link; std::string archive_mirrors_collection_link;
std::string archives_collection_link; std::string archives_collection_link;
std::string main_archive_link; std::string main_archive_link;
std::string current_series_link;
std::string series_collection_link; std::string series_collection_link;
std::string valid_specifications_collection_link; std::string valid_specifications_collection_link;
std::string vulnerabilities_collection_link; std::string vulnerabilities_collection_link;
std::string webhooks_collection_link; std::string webhooks_collection_link;
lazy_optional<distro_series, std::function<std::optional<distro_series>()>> current_series;
private: private:
launchpad* lp; launchpad* lp;
std::optional<distro_series> _current_series = std::nullopt;
// JSON parsing utility // JSON parsing utility
void parse_json(const std::string& json_data); void parse_json(const std::string& json_data);

Loading…
Cancel
Save