mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 12:06:01 +01:00
Make ValidPathInfo, NarInfo JSON instances, but don't yet use in the CLI
Make instances for them that share code with `nix path-info`, but do a slightly different format without store paths containing store dirs (matching the other latest JSON formats). Progress on #13570. If we depend on the store dir, our JSON serializers/deserializers take extra arguements, and that interfaces with the likes of various frameworks for associating these with types (e.g. nlohmann in C++, Serde in Rust, and Aeson in Haskell). For now, `nix path-info` still uses the previous format, with store dirs. We may yet decide to "rip of the band-aid", and just switch it over, but that is left as a future PR.
This commit is contained in:
parent
4f1c8f62c3
commit
94712c11db
7 changed files with 89 additions and 32 deletions
|
|
@ -65,7 +65,7 @@ static NarInfo makeNarInfo(const Store & store, bool includeImpureInfo)
|
||||||
readTest(#STEM, [&](const auto & encoded_) { \
|
readTest(#STEM, [&](const auto & encoded_) { \
|
||||||
auto encoded = json::parse(encoded_); \
|
auto encoded = json::parse(encoded_); \
|
||||||
auto expected = makeNarInfo(*store, PURE); \
|
auto expected = makeNarInfo(*store, PURE); \
|
||||||
NarInfo got = NarInfo::fromJSON(*store, expected.path, encoded); \
|
auto got = UnkeyedNarInfo::fromJSON(&*store, encoded); \
|
||||||
ASSERT_EQ(got, expected); \
|
ASSERT_EQ(got, expected); \
|
||||||
}); \
|
}); \
|
||||||
} \
|
} \
|
||||||
|
|
@ -74,7 +74,7 @@ static NarInfo makeNarInfo(const Store & store, bool includeImpureInfo)
|
||||||
{ \
|
{ \
|
||||||
writeTest( \
|
writeTest( \
|
||||||
#STEM, \
|
#STEM, \
|
||||||
[&]() -> json { return makeNarInfo(*store, PURE).toJSON(*store, PURE); }, \
|
[&]() -> json { return makeNarInfo(*store, PURE).toJSON(&*store, PURE); }, \
|
||||||
[](const auto & file) { return json::parse(readFile(file)); }, \
|
[](const auto & file) { return json::parse(readFile(file)); }, \
|
||||||
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
|
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ static UnkeyedValidPathInfo makeFull(const Store & store, bool includeImpureInfo
|
||||||
{ \
|
{ \
|
||||||
readTest(#STEM, [&](const auto & encoded_) { \
|
readTest(#STEM, [&](const auto & encoded_) { \
|
||||||
auto encoded = json::parse(encoded_); \
|
auto encoded = json::parse(encoded_); \
|
||||||
UnkeyedValidPathInfo got = UnkeyedValidPathInfo::fromJSON(*store, encoded); \
|
UnkeyedValidPathInfo got = UnkeyedValidPathInfo::fromJSON(&*store, encoded); \
|
||||||
auto expected = OBJ; \
|
auto expected = OBJ; \
|
||||||
ASSERT_EQ(got, expected); \
|
ASSERT_EQ(got, expected); \
|
||||||
}); \
|
}); \
|
||||||
|
|
@ -80,7 +80,7 @@ static UnkeyedValidPathInfo makeFull(const Store & store, bool includeImpureInfo
|
||||||
{ \
|
{ \
|
||||||
writeTest( \
|
writeTest( \
|
||||||
#STEM, \
|
#STEM, \
|
||||||
[&]() -> json { return OBJ.toJSON(*store, PURE); }, \
|
[&]() -> json { return OBJ.toJSON(&*store, PURE); }, \
|
||||||
[](const auto & file) { return json::parse(readFile(file)); }, \
|
[](const auto & file) { return json::parse(readFile(file)); }, \
|
||||||
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
|
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,17 +9,38 @@ namespace nix {
|
||||||
|
|
||||||
struct StoreDirConfig;
|
struct StoreDirConfig;
|
||||||
|
|
||||||
struct NarInfo : ValidPathInfo
|
struct UnkeyedNarInfo : virtual UnkeyedValidPathInfo
|
||||||
{
|
{
|
||||||
std::string url;
|
std::string url;
|
||||||
std::string compression;
|
std::string compression;
|
||||||
std::optional<Hash> fileHash;
|
std::optional<Hash> fileHash;
|
||||||
uint64_t fileSize = 0;
|
uint64_t fileSize = 0;
|
||||||
|
|
||||||
|
UnkeyedNarInfo(UnkeyedValidPathInfo info)
|
||||||
|
: UnkeyedValidPathInfo(std::move(info))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const UnkeyedNarInfo &) const = default;
|
||||||
|
// TODO libc++ 16 (used by darwin) missing `std::optional::operator <=>`, can't do yet
|
||||||
|
// auto operator <=>(const NarInfo &) const = default;
|
||||||
|
|
||||||
|
nlohmann::json toJSON(const StoreDirConfig * store, bool includeImpureInfo) const override;
|
||||||
|
static UnkeyedNarInfo fromJSON(const StoreDirConfig * store, const nlohmann::json & json);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key and the extra NAR fields
|
||||||
|
*/
|
||||||
|
struct NarInfo : ValidPathInfo, UnkeyedNarInfo
|
||||||
|
{
|
||||||
NarInfo() = delete;
|
NarInfo() = delete;
|
||||||
|
|
||||||
NarInfo(ValidPathInfo info)
|
NarInfo(ValidPathInfo info)
|
||||||
: ValidPathInfo{std::move(info)}
|
: UnkeyedValidPathInfo(std::move(static_cast<UnkeyedValidPathInfo &&>(info)))
|
||||||
|
// later moves will be partially ignored
|
||||||
|
, ValidPathInfo(std::move(info))
|
||||||
|
, UnkeyedNarInfo(std::move(info))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,13 +58,10 @@ struct NarInfo : ValidPathInfo
|
||||||
NarInfo(const StoreDirConfig & store, const std::string & s, const std::string & whence);
|
NarInfo(const StoreDirConfig & store, const std::string & s, const std::string & whence);
|
||||||
|
|
||||||
bool operator==(const NarInfo &) const = default;
|
bool operator==(const NarInfo &) const = default;
|
||||||
// TODO libc++ 16 (used by darwin) missing `std::optional::operator <=>`, can't do yet
|
|
||||||
// auto operator <=>(const NarInfo &) const = default;
|
|
||||||
|
|
||||||
std::string to_string(const StoreDirConfig & store) const;
|
std::string to_string(const StoreDirConfig & store) const;
|
||||||
|
|
||||||
nlohmann::json toJSON(const StoreDirConfig & store, bool includeImpureInfo) const override;
|
|
||||||
static NarInfo fromJSON(const StoreDirConfig & store, const StorePath & path, const nlohmann::json & json);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
|
|
||||||
|
JSON_IMPL(nix::UnkeyedNarInfo)
|
||||||
|
|
|
||||||
|
|
@ -117,11 +117,11 @@ struct UnkeyedValidPathInfo
|
||||||
* @param includeImpureInfo If true, variable elements such as the
|
* @param includeImpureInfo If true, variable elements such as the
|
||||||
* registration time are included.
|
* registration time are included.
|
||||||
*/
|
*/
|
||||||
virtual nlohmann::json toJSON(const StoreDirConfig & store, bool includeImpureInfo) const;
|
virtual nlohmann::json toJSON(const StoreDirConfig * store, bool includeImpureInfo) const;
|
||||||
static UnkeyedValidPathInfo fromJSON(const StoreDirConfig & store, const nlohmann::json & json);
|
static UnkeyedValidPathInfo fromJSON(const StoreDirConfig * store, const nlohmann::json & json);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ValidPathInfo : UnkeyedValidPathInfo
|
struct ValidPathInfo : virtual UnkeyedValidPathInfo
|
||||||
{
|
{
|
||||||
StorePath path;
|
StorePath path;
|
||||||
|
|
||||||
|
|
@ -174,10 +174,14 @@ struct ValidPathInfo : UnkeyedValidPathInfo
|
||||||
|
|
||||||
ValidPathInfo(StorePath && path, UnkeyedValidPathInfo info)
|
ValidPathInfo(StorePath && path, UnkeyedValidPathInfo info)
|
||||||
: UnkeyedValidPathInfo(info)
|
: UnkeyedValidPathInfo(info)
|
||||||
, path(std::move(path)) {};
|
, path(std::move(path))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
ValidPathInfo(const StorePath & path, UnkeyedValidPathInfo info)
|
ValidPathInfo(const StorePath & path, UnkeyedValidPathInfo info)
|
||||||
: UnkeyedValidPathInfo(info)
|
: ValidPathInfo(StorePath{path}, std::move(info))
|
||||||
, path(path) {};
|
{
|
||||||
|
}
|
||||||
|
|
||||||
static ValidPathInfo
|
static ValidPathInfo
|
||||||
makeFromCA(const StoreDirConfig & store, std::string_view name, ContentAddressWithReferences && ca, Hash narHash);
|
makeFromCA(const StoreDirConfig & store, std::string_view name, ContentAddressWithReferences && ca, Hash narHash);
|
||||||
|
|
@ -191,3 +195,5 @@ static_assert(std::is_move_constructible_v<ValidPathInfo>);
|
||||||
using ValidPathInfos = std::map<StorePath, ValidPathInfo>;
|
using ValidPathInfos = std::map<StorePath, ValidPathInfo>;
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
|
|
||||||
|
JSON_IMPL(nix::UnkeyedValidPathInfo)
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,9 @@
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
NarInfo::NarInfo(const StoreDirConfig & store, const std::string & s, const std::string & whence)
|
NarInfo::NarInfo(const StoreDirConfig & store, const std::string & s, const std::string & whence)
|
||||||
: ValidPathInfo(StorePath(StorePath::dummy), Hash(Hash::dummy)) // FIXME: hack
|
: UnkeyedValidPathInfo(Hash::dummy) // FIXME: hack
|
||||||
|
, ValidPathInfo(StorePath::dummy, static_cast<const UnkeyedValidPathInfo &>(*this)) // FIXME: hack
|
||||||
|
, UnkeyedNarInfo(static_cast<const UnkeyedValidPathInfo &>(*this))
|
||||||
{
|
{
|
||||||
unsigned line = 1;
|
unsigned line = 1;
|
||||||
|
|
||||||
|
|
@ -130,11 +132,11 @@ std::string NarInfo::to_string(const StoreDirConfig & store) const
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
nlohmann::json NarInfo::toJSON(const StoreDirConfig & store, bool includeImpureInfo) const
|
nlohmann::json UnkeyedNarInfo::toJSON(const StoreDirConfig * store, bool includeImpureInfo) const
|
||||||
{
|
{
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
auto jsonObject = ValidPathInfo::toJSON(store, includeImpureInfo);
|
auto jsonObject = UnkeyedValidPathInfo::toJSON(store, includeImpureInfo);
|
||||||
|
|
||||||
if (includeImpureInfo) {
|
if (includeImpureInfo) {
|
||||||
if (!url.empty())
|
if (!url.empty())
|
||||||
|
|
@ -150,14 +152,11 @@ nlohmann::json NarInfo::toJSON(const StoreDirConfig & store, bool includeImpureI
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
NarInfo NarInfo::fromJSON(const StoreDirConfig & store, const StorePath & path, const nlohmann::json & json)
|
UnkeyedNarInfo UnkeyedNarInfo::fromJSON(const StoreDirConfig * store, const nlohmann::json & json)
|
||||||
{
|
{
|
||||||
using nlohmann::detail::value_t;
|
using nlohmann::detail::value_t;
|
||||||
|
|
||||||
NarInfo res{ValidPathInfo{
|
UnkeyedNarInfo res{UnkeyedValidPathInfo::fromJSON(store, json)};
|
||||||
path,
|
|
||||||
UnkeyedValidPathInfo::fromJSON(store, json),
|
|
||||||
}};
|
|
||||||
|
|
||||||
auto & obj = getObject(json);
|
auto & obj = getObject(json);
|
||||||
|
|
||||||
|
|
@ -177,3 +176,19 @@ NarInfo NarInfo::fromJSON(const StoreDirConfig & store, const StorePath & path,
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
|
|
||||||
|
namespace nlohmann {
|
||||||
|
|
||||||
|
using namespace nix;
|
||||||
|
|
||||||
|
UnkeyedNarInfo adl_serializer<UnkeyedNarInfo>::from_json(const json & json)
|
||||||
|
{
|
||||||
|
return UnkeyedNarInfo::fromJSON(nullptr, json);
|
||||||
|
}
|
||||||
|
|
||||||
|
void adl_serializer<UnkeyedNarInfo>::to_json(json & json, const UnkeyedNarInfo & c)
|
||||||
|
{
|
||||||
|
json = c.toJSON(nullptr, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nlohmann
|
||||||
|
|
|
||||||
|
|
@ -149,7 +149,7 @@ ValidPathInfo ValidPathInfo::makeFromCA(
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
nlohmann::json UnkeyedValidPathInfo::toJSON(const StoreDirConfig & store, bool includeImpureInfo) const
|
nlohmann::json UnkeyedValidPathInfo::toJSON(const StoreDirConfig * store, bool includeImpureInfo) const
|
||||||
{
|
{
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
|
|
@ -163,13 +163,15 @@ nlohmann::json UnkeyedValidPathInfo::toJSON(const StoreDirConfig & store, bool i
|
||||||
{
|
{
|
||||||
auto & jsonRefs = jsonObject["references"] = json::array();
|
auto & jsonRefs = jsonObject["references"] = json::array();
|
||||||
for (auto & ref : references)
|
for (auto & ref : references)
|
||||||
jsonRefs.emplace_back(store.printStorePath(ref));
|
jsonRefs.emplace_back(store ? static_cast<json>(store->printStorePath(ref)) : static_cast<json>(ref));
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonObject["ca"] = ca;
|
jsonObject["ca"] = ca;
|
||||||
|
|
||||||
if (includeImpureInfo) {
|
if (includeImpureInfo) {
|
||||||
jsonObject["deriver"] = deriver ? (std::optional{store.printStorePath(*deriver)}) : std::nullopt;
|
jsonObject["deriver"] = deriver ? (store ? static_cast<json>(std::optional{store->printStorePath(*deriver)})
|
||||||
|
: static_cast<json>(std::optional{*deriver}))
|
||||||
|
: static_cast<json>(std::optional<StorePath>{});
|
||||||
|
|
||||||
jsonObject["registrationTime"] = registrationTime ? (std::optional{registrationTime}) : std::nullopt;
|
jsonObject["registrationTime"] = registrationTime ? (std::optional{registrationTime}) : std::nullopt;
|
||||||
|
|
||||||
|
|
@ -183,7 +185,7 @@ nlohmann::json UnkeyedValidPathInfo::toJSON(const StoreDirConfig & store, bool i
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(const StoreDirConfig & store, const nlohmann::json & _json)
|
UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(const StoreDirConfig * store, const nlohmann::json & _json)
|
||||||
{
|
{
|
||||||
UnkeyedValidPathInfo res{
|
UnkeyedValidPathInfo res{
|
||||||
Hash(Hash::dummy),
|
Hash(Hash::dummy),
|
||||||
|
|
@ -203,7 +205,7 @@ UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(const StoreDirConfig & store
|
||||||
try {
|
try {
|
||||||
auto references = getStringList(valueAt(json, "references"));
|
auto references = getStringList(valueAt(json, "references"));
|
||||||
for (auto & input : references)
|
for (auto & input : references)
|
||||||
res.references.insert(store.parseStorePath(static_cast<const std::string &>(input)));
|
res.references.insert(store ? store->parseStorePath(getString(input)) : static_cast<StorePath>(input));
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
e.addTrace({}, "while reading key 'references'");
|
e.addTrace({}, "while reading key 'references'");
|
||||||
throw;
|
throw;
|
||||||
|
|
@ -218,7 +220,7 @@ UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(const StoreDirConfig & store
|
||||||
|
|
||||||
if (auto * rawDeriver0 = optionalValueAt(json, "deriver"))
|
if (auto * rawDeriver0 = optionalValueAt(json, "deriver"))
|
||||||
if (auto * rawDeriver = getNullable(*rawDeriver0))
|
if (auto * rawDeriver = getNullable(*rawDeriver0))
|
||||||
res.deriver = store.parseStorePath(getString(*rawDeriver));
|
res.deriver = store ? store->parseStorePath(getString(*rawDeriver)) : static_cast<StorePath>(*rawDeriver);
|
||||||
|
|
||||||
if (auto * rawRegistrationTime0 = optionalValueAt(json, "registrationTime"))
|
if (auto * rawRegistrationTime0 = optionalValueAt(json, "registrationTime"))
|
||||||
if (auto * rawRegistrationTime = getNullable(*rawRegistrationTime0))
|
if (auto * rawRegistrationTime = getNullable(*rawRegistrationTime0))
|
||||||
|
|
@ -234,3 +236,19 @@ UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(const StoreDirConfig & store
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
|
|
||||||
|
namespace nlohmann {
|
||||||
|
|
||||||
|
using namespace nix;
|
||||||
|
|
||||||
|
UnkeyedValidPathInfo adl_serializer<UnkeyedValidPathInfo>::from_json(const json & json)
|
||||||
|
{
|
||||||
|
return UnkeyedValidPathInfo::fromJSON(nullptr, json);
|
||||||
|
}
|
||||||
|
|
||||||
|
void adl_serializer<UnkeyedValidPathInfo>::to_json(json & json, const UnkeyedValidPathInfo & c)
|
||||||
|
{
|
||||||
|
json = c.toJSON(nullptr, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nlohmann
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ static json pathInfoToJSON(Store & store, const StorePathSet & storePaths, bool
|
||||||
// know the name yet until we've read the NAR info.
|
// know the name yet until we've read the NAR info.
|
||||||
printedStorePath = store.printStorePath(info->path);
|
printedStorePath = store.printStorePath(info->path);
|
||||||
|
|
||||||
jsonObject = info->toJSON(store, true);
|
jsonObject = info->toJSON(&store, true);
|
||||||
|
|
||||||
if (showClosureSize) {
|
if (showClosureSize) {
|
||||||
StorePathSet closure;
|
StorePathSet closure;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue