2020-03-17 19:54:36 +00:00
|
|
|
#include "attrs.hh"
|
|
|
|
#include "fetchers.hh"
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
namespace nix::fetchers {
|
|
|
|
|
|
|
|
Attrs jsonToAttrs(const nlohmann::json & json)
|
|
|
|
{
|
|
|
|
Attrs attrs;
|
|
|
|
|
|
|
|
for (auto & i : json.items()) {
|
|
|
|
if (i.value().is_number())
|
|
|
|
attrs.emplace(i.key(), i.value().get<int64_t>());
|
|
|
|
else if (i.value().is_string())
|
|
|
|
attrs.emplace(i.key(), i.value().get<std::string>());
|
2020-03-17 20:34:38 +00:00
|
|
|
else if (i.value().is_boolean())
|
|
|
|
attrs.emplace(i.key(), i.value().get<bool>());
|
2020-03-17 19:54:36 +00:00
|
|
|
else
|
|
|
|
throw Error("unsupported input attribute type in lock file");
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:41:18 +00:00
|
|
|
nlohmann::json attrsToJSON(const Attrs & attrs)
|
2020-03-17 19:54:36 +00:00
|
|
|
{
|
|
|
|
nlohmann::json json;
|
|
|
|
for (auto & attr : attrs) {
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
if (auto v = std::get_if<uint64_t>(&attr.second)) {
|
2020-03-17 19:54:36 +00:00
|
|
|
json[attr.first] = *v;
|
|
|
|
} else if (auto v = std::get_if<std::string>(&attr.second)) {
|
|
|
|
json[attr.first] = *v;
|
2020-03-17 20:34:38 +00:00
|
|
|
} else if (auto v = std::get_if<Explicit<bool>>(&attr.second)) {
|
|
|
|
json[attr.first] = v->t;
|
2020-03-17 19:54:36 +00:00
|
|
|
} else abort();
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> maybeGetStrAttr(const Attrs & attrs, const std::string & name)
|
|
|
|
{
|
|
|
|
auto i = attrs.find(name);
|
|
|
|
if (i == attrs.end()) return {};
|
|
|
|
if (auto v = std::get_if<std::string>(&i->second))
|
|
|
|
return *v;
|
2020-10-28 14:41:18 +00:00
|
|
|
throw Error("input attribute '%s' is not a string %s", name, attrsToJSON(attrs).dump());
|
2020-03-17 19:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string getStrAttr(const Attrs & attrs, const std::string & name)
|
|
|
|
{
|
|
|
|
auto s = maybeGetStrAttr(attrs, name);
|
|
|
|
if (!s)
|
|
|
|
throw Error("input attribute '%s' is missing", name);
|
|
|
|
return *s;
|
|
|
|
}
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
std::optional<uint64_t> maybeGetIntAttr(const Attrs & attrs, const std::string & name)
|
2020-03-17 19:54:36 +00:00
|
|
|
{
|
|
|
|
auto i = attrs.find(name);
|
|
|
|
if (i == attrs.end()) return {};
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
if (auto v = std::get_if<uint64_t>(&i->second))
|
2020-03-17 19:54:36 +00:00
|
|
|
return *v;
|
2020-03-17 20:34:38 +00:00
|
|
|
throw Error("input attribute '%s' is not an integer", name);
|
2020-03-17 19:54:36 +00:00
|
|
|
}
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
uint64_t getIntAttr(const Attrs & attrs, const std::string & name)
|
2020-03-17 19:54:36 +00:00
|
|
|
{
|
|
|
|
auto s = maybeGetIntAttr(attrs, name);
|
|
|
|
if (!s)
|
|
|
|
throw Error("input attribute '%s' is missing", name);
|
|
|
|
return *s;
|
|
|
|
}
|
|
|
|
|
2020-03-17 20:34:38 +00:00
|
|
|
std::optional<bool> maybeGetBoolAttr(const Attrs & attrs, const std::string & name)
|
|
|
|
{
|
|
|
|
auto i = attrs.find(name);
|
|
|
|
if (i == attrs.end()) return {};
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
if (auto v = std::get_if<Explicit<bool>>(&i->second))
|
|
|
|
return v->t;
|
2020-03-17 20:34:38 +00:00
|
|
|
throw Error("input attribute '%s' is not a Boolean", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getBoolAttr(const Attrs & attrs, const std::string & name)
|
|
|
|
{
|
|
|
|
auto s = maybeGetBoolAttr(attrs, name);
|
|
|
|
if (!s)
|
|
|
|
throw Error("input attribute '%s' is missing", name);
|
|
|
|
return *s;
|
|
|
|
}
|
|
|
|
|
2020-04-02 12:56:20 +00:00
|
|
|
std::map<std::string, std::string> attrsToQuery(const Attrs & attrs)
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string> query;
|
|
|
|
for (auto & attr : attrs) {
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
if (auto v = std::get_if<uint64_t>(&attr.second)) {
|
2020-04-02 12:56:20 +00:00
|
|
|
query.insert_or_assign(attr.first, fmt("%d", *v));
|
|
|
|
} else if (auto v = std::get_if<std::string>(&attr.second)) {
|
|
|
|
query.insert_or_assign(attr.first, *v);
|
|
|
|
} else if (auto v = std::get_if<Explicit<bool>>(&attr.second)) {
|
|
|
|
query.insert_or_assign(attr.first, v->t ? "1" : "0");
|
|
|
|
} else abort();
|
|
|
|
}
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
}
|