From f6c2b58e475ffa805f22c5085a3bc26e2fbbb209 Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Tue, 17 Dec 2024 00:54:36 -0600 Subject: [PATCH] Also lazy-load build.current_source_publication --- src/build.cpp | 35 +++++++++++++++++++++-------------- src/build.h | 5 +++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/build.cpp b/src/build.cpp index 71ee646..3d75a7e 100644 --- a/src/build.cpp +++ b/src/build.cpp @@ -30,7 +30,26 @@ build::build() title(""), self_link(""), id(-1), - lp(nullptr) {} + lp(nullptr), + current_source_publication([this]() -> std::optional { + 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() {} @@ -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(); }}, {"title", [this](const nlohmann::json& val) { title = val.get(); }}, {"self_link", [this](const nlohmann::json& val) { self_link = val.get(); }}, + {"current_source_publication_link", [this](const nlohmann::json& val) { current_source_publication_link = val.get(); }}, {"build_log_url", [this](const nlohmann::json& val) { build_log_url = val.get(); }}, {"upload_log_url", [this](const nlohmann::json& val) { upload_log_url = val.get(); }}, {"duration", [this](const nlohmann::json& val) { duration = val.get(); }}, @@ -149,16 +169,3 @@ bool build::rescore(int score) { void build::set_lp(launchpad* lp_ptr) { lp = lp_ptr; } - -const std::optional 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; -} diff --git a/src/build.h b/src/build.h index e8b30c4..f8141a1 100644 --- a/src/build.h +++ b/src/build.h @@ -20,6 +20,7 @@ #include #include #include "source_package_publishing_history.h" +#include "lazy_optional.h" class launchpad; @@ -80,11 +81,11 @@ public: std::chrono::system_clock::time_point datecreated; // Object collections - std::optional current_source_publication = getCurrentSourcePublication(); + lazy_optional()>> current_source_publication; private: launchpad* lp; - const std::optional getCurrentSourcePublication(); + std::optional _current_source_publication = std::nullopt; }; #endif // BUILD_H