From bd10a07d17161fb4a4e1af5aa365b23d405a5216 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 1 Apr 2020 23:03:27 +0200 Subject: [PATCH] Registry: Use a struct instead of a tuple for entries --- src/libfetchers/registry.cc | 37 +++++++++++++++++++++---------------- src/libfetchers/registry.hh | 15 ++++++++------- src/nix/flake.cc | 4 ++-- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/libfetchers/registry.cc b/src/libfetchers/registry.cc index 08dbe797e..6627d3725 100644 --- a/src/libfetchers/registry.cc +++ b/src/libfetchers/registry.cc @@ -44,9 +44,10 @@ std::shared_ptr Registry::read( toAttrs.erase(j); } registry->entries.push_back( - { inputFromAttrs(jsonToAttrs(i["from"])) - , inputFromAttrs(toAttrs) - , extraAttrs + Entry { + .from = inputFromAttrs(jsonToAttrs(i["from"])), + .to = inputFromAttrs(toAttrs), + .extraAttrs = extraAttrs, }); } } @@ -61,12 +62,12 @@ std::shared_ptr Registry::read( void Registry::write(const Path & path) { nlohmann::json arr; - for (auto & elem : entries) { + for (auto & entry : entries) { nlohmann::json obj; - obj["from"] = attrsToJson(std::get<0>(elem)->toAttrs()); - obj["to"] = attrsToJson(std::get<1>(elem)->toAttrs()); - if (!std::get<2>(elem).empty()) - obj["to"].update(attrsToJson(std::get<2>(elem))); + obj["from"] = attrsToJson(entry.from->toAttrs()); + obj["to"] = attrsToJson(entry.to->toAttrs()); + if (!entry.extraAttrs.empty()) + obj["to"].update(attrsToJson(entry.extraAttrs)); arr.emplace_back(std::move(obj)); } @@ -83,14 +84,19 @@ void Registry::add( const std::shared_ptr & to, const Attrs & extraAttrs) { - entries.emplace_back(from, to, extraAttrs); + entries.emplace_back( + Entry { + .from = from, + .to = to, + .extraAttrs = extraAttrs + }); } void Registry::remove(const std::shared_ptr & input) { // FIXME: use C++20 std::erase. for (auto i = entries.begin(); i != entries.end(); ) - if (*std::get<0>(*i) == *input) + if (*i->from == *input) i = entries.erase(i); else ++i; @@ -179,12 +185,11 @@ std::pair, Attrs> lookupInRegistries( for (auto & registry : getRegistries(store)) { // FIXME: O(n) for (auto & entry : registry->entries) { - auto from = std::get<0>(entry); - if (from->contains(*input)) { - input = std::get<1>(entry)->applyOverrides( - !from->getRef() && input->getRef() ? input->getRef() : std::optional(), - !from->getRev() && input->getRev() ? input->getRev() : std::optional()); - extraAttrs = std::get<2>(entry); + if (entry.from->contains(*input)) { + input = entry.to->applyOverrides( + !entry.from->getRef() && input->getRef() ? input->getRef() : std::optional(), + !entry.from->getRev() && input->getRev() ? input->getRev() : std::optional()); + extraAttrs = entry.extraAttrs; goto restart; } } diff --git a/src/libfetchers/registry.hh b/src/libfetchers/registry.hh index 5e8857850..722c41b10 100644 --- a/src/libfetchers/registry.hh +++ b/src/libfetchers/registry.hh @@ -18,13 +18,14 @@ struct Registry RegistryType type; - std::vector< - std::tuple< - std::shared_ptr, // from - std::shared_ptr, // to - Attrs // extra attributes - > - > entries; + struct Entry + { + std::shared_ptr from; + std::shared_ptr to; + Attrs extraAttrs; + }; + + std::vector entries; Registry(RegistryType type) : type(type) diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 16b797de8..2b7497a84 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -71,8 +71,8 @@ struct CmdFlakeList : EvalCommand registry->type == Registry::User ? "user " : registry->type == Registry::System ? "system" : "global", - std::get<0>(entry)->to_string(), - std::get<1>(entry)->to_string()); + entry.from->to_string(), + entry.to->to_string()); } } }