mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 11:36:03 +01:00
Rework ValidPathInfo, NarInfo JSON instances
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).
This commit is contained in:
parent
147e183c68
commit
8dfab7c2dd
7 changed files with 123 additions and 55 deletions
|
|
@ -65,7 +65,7 @@ static NarInfo makeNarInfo(const Store & store, bool includeImpureInfo)
|
|||
readTest(#STEM, [&](const auto & encoded_) { \
|
||||
auto encoded = json::parse(encoded_); \
|
||||
auto expected = makeNarInfo(*store, PURE); \
|
||||
NarInfo got = NarInfo::fromJSON(*store, expected.path, encoded); \
|
||||
auto got = UnkeyedNarInfo::fromJSON(&*store, encoded); \
|
||||
ASSERT_EQ(got, expected); \
|
||||
}); \
|
||||
} \
|
||||
|
|
@ -74,7 +74,7 @@ static NarInfo makeNarInfo(const Store & store, bool includeImpureInfo)
|
|||
{ \
|
||||
writeTest( \
|
||||
#STEM, \
|
||||
[&]() -> json { return makeNarInfo(*store, PURE).toJSON(*store, PURE, HashFormat::SRI); }, \
|
||||
[&]() -> json { return makeNarInfo(*store, PURE).toJSON(&*store, PURE); }, \
|
||||
[](const auto & file) { return json::parse(readFile(file)); }, \
|
||||
[](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_) { \
|
||||
auto encoded = json::parse(encoded_); \
|
||||
UnkeyedValidPathInfo got = UnkeyedValidPathInfo::fromJSON(*store, encoded); \
|
||||
UnkeyedValidPathInfo got = UnkeyedValidPathInfo::fromJSON(&*store, encoded); \
|
||||
auto expected = OBJ; \
|
||||
ASSERT_EQ(got, expected); \
|
||||
}); \
|
||||
|
|
@ -80,7 +80,7 @@ static UnkeyedValidPathInfo makeFull(const Store & store, bool includeImpureInfo
|
|||
{ \
|
||||
writeTest( \
|
||||
#STEM, \
|
||||
[&]() -> json { return OBJ.toJSON(*store, PURE, HashFormat::SRI); }, \
|
||||
[&]() -> json { return OBJ.toJSON(&*store, PURE); }, \
|
||||
[](const auto & file) { return json::parse(readFile(file)); }, \
|
||||
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,17 +9,38 @@ namespace nix {
|
|||
|
||||
struct StoreDirConfig;
|
||||
|
||||
struct NarInfo : ValidPathInfo
|
||||
struct UnkeyedNarInfo : virtual UnkeyedValidPathInfo
|
||||
{
|
||||
std::string url;
|
||||
std::string compression;
|
||||
std::optional<Hash> fileHash;
|
||||
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(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);
|
||||
|
||||
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;
|
||||
|
||||
nlohmann::json toJSON(const StoreDirConfig & store, bool includeImpureInfo, HashFormat hashFormat) const override;
|
||||
static NarInfo fromJSON(const StoreDirConfig & store, const StorePath & path, const nlohmann::json & json);
|
||||
};
|
||||
|
||||
} // namespace nix
|
||||
|
||||
JSON_IMPL(nix::UnkeyedNarInfo)
|
||||
|
|
|
|||
|
|
@ -117,11 +117,11 @@ struct UnkeyedValidPathInfo
|
|||
* @param includeImpureInfo If true, variable elements such as the
|
||||
* registration time are included.
|
||||
*/
|
||||
virtual nlohmann::json toJSON(const StoreDirConfig & store, bool includeImpureInfo, HashFormat hashFormat) const;
|
||||
static UnkeyedValidPathInfo fromJSON(const StoreDirConfig & store, const nlohmann::json & json);
|
||||
virtual nlohmann::json toJSON(const StoreDirConfig * store, bool includeImpureInfo) const;
|
||||
static UnkeyedValidPathInfo fromJSON(const StoreDirConfig * store, const nlohmann::json & json);
|
||||
};
|
||||
|
||||
struct ValidPathInfo : UnkeyedValidPathInfo
|
||||
struct ValidPathInfo : virtual UnkeyedValidPathInfo
|
||||
{
|
||||
StorePath path;
|
||||
|
||||
|
|
@ -174,10 +174,14 @@ struct ValidPathInfo : UnkeyedValidPathInfo
|
|||
|
||||
ValidPathInfo(StorePath && path, UnkeyedValidPathInfo info)
|
||||
: UnkeyedValidPathInfo(info)
|
||||
, path(std::move(path)) {};
|
||||
, path(std::move(path))
|
||||
{
|
||||
}
|
||||
|
||||
ValidPathInfo(const StorePath & path, UnkeyedValidPathInfo info)
|
||||
: UnkeyedValidPathInfo(info)
|
||||
, path(path) {};
|
||||
: ValidPathInfo(StorePath{path}, std::move(info))
|
||||
{
|
||||
}
|
||||
|
||||
static ValidPathInfo
|
||||
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>;
|
||||
|
||||
} // namespace nix
|
||||
|
||||
JSON_IMPL(nix::UnkeyedValidPathInfo)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,9 @@
|
|||
namespace nix {
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -130,19 +132,23 @@ std::string NarInfo::to_string(const StoreDirConfig & store) const
|
|||
return res;
|
||||
}
|
||||
|
||||
nlohmann::json NarInfo::toJSON(const StoreDirConfig & store, bool includeImpureInfo, HashFormat hashFormat) const
|
||||
nlohmann::json UnkeyedNarInfo::toJSON(const StoreDirConfig * store, bool includeImpureInfo) const
|
||||
{
|
||||
using nlohmann::json;
|
||||
|
||||
auto jsonObject = ValidPathInfo::toJSON(store, includeImpureInfo, hashFormat);
|
||||
auto jsonObject = UnkeyedValidPathInfo::toJSON(store, includeImpureInfo);
|
||||
|
||||
if (includeImpureInfo) {
|
||||
if (!url.empty())
|
||||
jsonObject["url"] = url;
|
||||
if (!compression.empty())
|
||||
jsonObject["compression"] = compression;
|
||||
if (fileHash)
|
||||
jsonObject["downloadHash"] = fileHash->to_string(hashFormat, true);
|
||||
if (fileHash) {
|
||||
/* Back compat hack, see comment for "narHash" in
|
||||
`UnkeyedValidPathInfo::toJSON` for details. */
|
||||
jsonObject["downloadHash"] =
|
||||
store ? static_cast<json>(fileHash->to_string(HashFormat::SRI, true)) : static_cast<json>(*fileHash);
|
||||
}
|
||||
if (fileSize)
|
||||
jsonObject["downloadSize"] = fileSize;
|
||||
}
|
||||
|
|
@ -150,14 +156,11 @@ nlohmann::json NarInfo::toJSON(const StoreDirConfig & store, bool includeImpureI
|
|||
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;
|
||||
|
||||
NarInfo res{ValidPathInfo{
|
||||
path,
|
||||
UnkeyedValidPathInfo::fromJSON(store, json),
|
||||
}};
|
||||
UnkeyedNarInfo res{UnkeyedValidPathInfo::fromJSON(store, json)};
|
||||
|
||||
auto & obj = getObject(json);
|
||||
|
||||
|
|
@ -177,3 +180,19 @@ NarInfo NarInfo::fromJSON(const StoreDirConfig & store, const StorePath & path,
|
|||
}
|
||||
|
||||
} // 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,26 +149,32 @@ ValidPathInfo ValidPathInfo::makeFromCA(
|
|||
return res;
|
||||
}
|
||||
|
||||
nlohmann::json
|
||||
UnkeyedValidPathInfo::toJSON(const StoreDirConfig & store, bool includeImpureInfo, HashFormat hashFormat) const
|
||||
nlohmann::json UnkeyedValidPathInfo::toJSON(const StoreDirConfig * store, bool includeImpureInfo) const
|
||||
{
|
||||
using nlohmann::json;
|
||||
|
||||
auto jsonObject = json::object();
|
||||
|
||||
jsonObject["narHash"] = narHash.to_string(hashFormat, true);
|
||||
/* Back-compat hack, if we are passing a `StoreDirConfig`, do SRI,
|
||||
which `nix path-info` has always down. Otherwise, use the new
|
||||
cannonical JSON serialization for `Hash`. */
|
||||
jsonObject["narHash"] =
|
||||
store ? static_cast<json>(narHash.to_string(HashFormat::SRI, true)) : static_cast<json>(narHash);
|
||||
jsonObject["narSize"] = narSize;
|
||||
|
||||
{
|
||||
auto & jsonRefs = jsonObject["references"] = json::array();
|
||||
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 ? (std::optional{renderContentAddress(*ca)}) : std::nullopt;
|
||||
jsonObject["ca"] = ca ? (store ? static_cast<json>(renderContentAddress(*ca)) : static_cast<json>(*ca))
|
||||
: static_cast<json>(nullptr);
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -182,20 +188,23 @@ UnkeyedValidPathInfo::toJSON(const StoreDirConfig & store, bool includeImpureInf
|
|||
return jsonObject;
|
||||
}
|
||||
|
||||
UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(const StoreDirConfig & store, const nlohmann::json & _json)
|
||||
UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(const StoreDirConfig * store, const nlohmann::json & _json)
|
||||
{
|
||||
UnkeyedValidPathInfo res{
|
||||
Hash(Hash::dummy),
|
||||
};
|
||||
|
||||
auto & json = getObject(_json);
|
||||
res.narHash = Hash::parseAny(getString(valueAt(json, "narHash")), std::nullopt);
|
||||
res.narHash = [&] {
|
||||
auto & j = valueAt(json, "narHash");
|
||||
return store ? Hash::parseAny(getString(j), std::nullopt) : static_cast<Hash>(j);
|
||||
}();
|
||||
res.narSize = getUnsigned(valueAt(json, "narSize"));
|
||||
|
||||
try {
|
||||
auto references = getStringList(valueAt(json, "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) {
|
||||
e.addTrace({}, "while reading key 'references'");
|
||||
throw;
|
||||
|
|
@ -205,11 +214,11 @@ UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(const StoreDirConfig & store
|
|||
// missing is for back-compat.
|
||||
if (auto * rawCa0 = optionalValueAt(json, "ca"))
|
||||
if (auto * rawCa = getNullable(*rawCa0))
|
||||
res.ca = ContentAddress::parse(getString(*rawCa));
|
||||
res.ca = store ? ContentAddress::parse(getString(*rawCa)) : static_cast<ContentAddress>(*rawCa);
|
||||
|
||||
if (auto * rawDeriver0 = optionalValueAt(json, "deriver"))
|
||||
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 * rawRegistrationTime = getNullable(*rawRegistrationTime0))
|
||||
|
|
@ -225,3 +234,19 @@ UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(const StoreDirConfig & store
|
|||
}
|
||||
|
||||
} // 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.
|
||||
printedStorePath = store.printStorePath(info->path);
|
||||
|
||||
jsonObject = info->toJSON(store, true, HashFormat::SRI);
|
||||
jsonObject = info->toJSON(&store, true);
|
||||
|
||||
if (showClosureSize) {
|
||||
StorePathSet closure;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue