Also lazy-load build.current_source_publication

main
Simon Quigley 6 days ago
parent 0100d7664e
commit f6c2b58e47

@ -30,7 +30,26 @@ build::build()
title(""), title(""),
self_link(""), self_link(""),
id(-1), id(-1),
lp(nullptr) {} lp(nullptr),
current_source_publication([this]() -> std::optional<source_package_publishing_history> {
if (current_source_publication_link.empty()) return std::nullopt;
if (_current_source_publication) return _current_source_publication;
auto response = lp ? lp->api_get(current_source_publication_link) : std::nullopt;
if (!response) return std::nullopt;
auto data = nlohmann::json::parse(response.value());
auto spph = source_package_publishing_history::parse(data.dump());
if (spph) {
spph->set_lp(lp);
_current_source_publication = spph;
return spph;
}
return std::nullopt;
})
{}
build::~build() {} build::~build() {}
@ -52,6 +71,7 @@ void build::parse_json(const std::string& json_data) {
{"can_be_retried", [this](const nlohmann::json& val) { can_be_retried = val.get<bool>(); }}, {"can_be_retried", [this](const nlohmann::json& val) { can_be_retried = val.get<bool>(); }},
{"title", [this](const nlohmann::json& val) { title = val.get<std::string>(); }}, {"title", [this](const nlohmann::json& val) { title = 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_source_publication_link", [this](const nlohmann::json& val) { current_source_publication_link = val.get<std::string>(); }},
{"build_log_url", [this](const nlohmann::json& val) { build_log_url = val.get<std::string>(); }}, {"build_log_url", [this](const nlohmann::json& val) { build_log_url = val.get<std::string>(); }},
{"upload_log_url", [this](const nlohmann::json& val) { upload_log_url = val.get<std::string>(); }}, {"upload_log_url", [this](const nlohmann::json& val) { upload_log_url = val.get<std::string>(); }},
{"duration", [this](const nlohmann::json& val) { duration = val.get<std::string>(); }}, {"duration", [this](const nlohmann::json& val) { duration = val.get<std::string>(); }},
@ -149,16 +169,3 @@ bool build::rescore(int score) {
void build::set_lp(launchpad* lp_ptr) { void build::set_lp(launchpad* lp_ptr) {
lp = lp_ptr; lp = lp_ptr;
} }
const std::optional<source_package_publishing_history> build::getCurrentSourcePublication() {
if (current_source_publication_link.empty()) return std::nullopt;
auto response = lp->api_get(current_source_publication_link);
if (!response) return std::nullopt;
auto data = nlohmann::json::parse(response.value());
auto sp = source_package_publishing_history::parse(data.dump());
if (sp) {
sp->set_lp(lp);
return sp.value();
}
return std::nullopt;
}

@ -20,6 +20,7 @@
#include <optional> #include <optional>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "source_package_publishing_history.h" #include "source_package_publishing_history.h"
#include "lazy_optional.h"
class launchpad; class launchpad;
@ -80,11 +81,11 @@ public:
std::chrono::system_clock::time_point datecreated; std::chrono::system_clock::time_point datecreated;
// Object collections // Object collections
std::optional<source_package_publishing_history> current_source_publication = getCurrentSourcePublication(); lazy_optional<source_package_publishing_history, std::function<std::optional<source_package_publishing_history>()>> current_source_publication;
private: private:
launchpad* lp; launchpad* lp;
const std::optional<source_package_publishing_history> getCurrentSourcePublication(); std::optional<source_package_publishing_history> _current_source_publication = std::nullopt;
}; };
#endif // BUILD_H #endif // BUILD_H

Loading…
Cancel
Save