Registry: Use a struct instead of a tuple for entries

This commit is contained in:
Eelco Dolstra 2020-04-01 23:03:27 +02:00
parent 77ffaea4fa
commit bd10a07d17
3 changed files with 31 additions and 25 deletions

View file

@ -44,9 +44,10 @@ std::shared_ptr<Registry> Registry::read(
toAttrs.erase(j); toAttrs.erase(j);
} }
registry->entries.push_back( registry->entries.push_back(
{ inputFromAttrs(jsonToAttrs(i["from"])) Entry {
, inputFromAttrs(toAttrs) .from = inputFromAttrs(jsonToAttrs(i["from"])),
, extraAttrs .to = inputFromAttrs(toAttrs),
.extraAttrs = extraAttrs,
}); });
} }
} }
@ -61,12 +62,12 @@ std::shared_ptr<Registry> Registry::read(
void Registry::write(const Path & path) void Registry::write(const Path & path)
{ {
nlohmann::json arr; nlohmann::json arr;
for (auto & elem : entries) { for (auto & entry : entries) {
nlohmann::json obj; nlohmann::json obj;
obj["from"] = attrsToJson(std::get<0>(elem)->toAttrs()); obj["from"] = attrsToJson(entry.from->toAttrs());
obj["to"] = attrsToJson(std::get<1>(elem)->toAttrs()); obj["to"] = attrsToJson(entry.to->toAttrs());
if (!std::get<2>(elem).empty()) if (!entry.extraAttrs.empty())
obj["to"].update(attrsToJson(std::get<2>(elem))); obj["to"].update(attrsToJson(entry.extraAttrs));
arr.emplace_back(std::move(obj)); arr.emplace_back(std::move(obj));
} }
@ -83,14 +84,19 @@ void Registry::add(
const std::shared_ptr<const Input> & to, const std::shared_ptr<const Input> & to,
const Attrs & extraAttrs) 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<const Input> & input) void Registry::remove(const std::shared_ptr<const Input> & input)
{ {
// FIXME: use C++20 std::erase. // FIXME: use C++20 std::erase.
for (auto i = entries.begin(); i != entries.end(); ) for (auto i = entries.begin(); i != entries.end(); )
if (*std::get<0>(*i) == *input) if (*i->from == *input)
i = entries.erase(i); i = entries.erase(i);
else else
++i; ++i;
@ -179,12 +185,11 @@ std::pair<std::shared_ptr<const Input>, Attrs> lookupInRegistries(
for (auto & registry : getRegistries(store)) { for (auto & registry : getRegistries(store)) {
// FIXME: O(n) // FIXME: O(n)
for (auto & entry : registry->entries) { for (auto & entry : registry->entries) {
auto from = std::get<0>(entry); if (entry.from->contains(*input)) {
if (from->contains(*input)) { input = entry.to->applyOverrides(
input = std::get<1>(entry)->applyOverrides( !entry.from->getRef() && input->getRef() ? input->getRef() : std::optional<std::string>(),
!from->getRef() && input->getRef() ? input->getRef() : std::optional<std::string>(), !entry.from->getRev() && input->getRev() ? input->getRev() : std::optional<Hash>());
!from->getRev() && input->getRev() ? input->getRev() : std::optional<Hash>()); extraAttrs = entry.extraAttrs;
extraAttrs = std::get<2>(entry);
goto restart; goto restart;
} }
} }

View file

@ -18,13 +18,14 @@ struct Registry
RegistryType type; RegistryType type;
std::vector< struct Entry
std::tuple< {
std::shared_ptr<const Input>, // from std::shared_ptr<const Input> from;
std::shared_ptr<const Input>, // to std::shared_ptr<const Input> to;
Attrs // extra attributes Attrs extraAttrs;
> };
> entries;
std::vector<Entry> entries;
Registry(RegistryType type) Registry(RegistryType type)
: type(type) : type(type)

View file

@ -71,8 +71,8 @@ struct CmdFlakeList : EvalCommand
registry->type == Registry::User ? "user " : registry->type == Registry::User ? "user " :
registry->type == Registry::System ? "system" : registry->type == Registry::System ? "system" :
"global", "global",
std::get<0>(entry)->to_string(), entry.from->to_string(),
std::get<1>(entry)->to_string()); entry.to->to_string());
} }
} }
} }