1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-02 21:51:50 +02:00

Make nix path-info --json return an object not array

Before it returned a list of JSON objects with store object information,
including the path in each object. Now, it maps the paths to JSON
objects with the metadata sans path.

This matches how `nix derivation show` works.

Quite hillariously, none of our existing functional tests caught this
change to `path-info --json` though they did use it. So just new
functional tests need to be added.
This commit is contained in:
John Ericson 2023-10-22 21:12:54 -04:00
parent a7212e169b
commit cc46ea1630
14 changed files with 108 additions and 54 deletions

View file

@ -164,8 +164,12 @@ NarInfo NarInfo::fromJSON(
{
using nlohmann::detail::value_t;
NarInfo res { ValidPathInfo::fromJSON(store, json) };
res.path = path;
NarInfo res {
ValidPathInfo {
path,
UnkeyedValidPathInfo::fromJSON(store, json),
}
};
if (json.contains("url"))
res.url = ensureType(valueAt(json, "url"), value_t::string);

View file

@ -148,6 +148,11 @@ static nlohmann::json pathInfoToJSON(
auto & jsonPath = jsonList.emplace_back(
info->toJSON(store, false, HashFormat::Base32));
// Add the path to the object whose metadata we are including.
jsonPath["path"] = store.printStorePath(storePath);
jsonPath["valid"] = true;
jsonPath["closureSize"] = ({
uint64_t totalNarSize = 0;
StorePathSet closure;

View file

@ -148,7 +148,7 @@ ValidPathInfo::ValidPathInfo(
}
nlohmann::json ValidPathInfo::toJSON(
nlohmann::json UnkeyedValidPathInfo::toJSON(
const Store & store,
bool includeImpureInfo,
HashFormat hashFormat) const
@ -157,8 +157,6 @@ nlohmann::json ValidPathInfo::toJSON(
auto jsonObject = json::object();
jsonObject["path"] = store.printStorePath(path);
jsonObject["valid"] = true;
jsonObject["narHash"] = narHash.to_string(hashFormat, true);
jsonObject["narSize"] = narSize;
@ -190,21 +188,17 @@ nlohmann::json ValidPathInfo::toJSON(
return jsonObject;
}
ValidPathInfo ValidPathInfo::fromJSON(
UnkeyedValidPathInfo UnkeyedValidPathInfo::fromJSON(
const Store & store,
const nlohmann::json & json)
{
using nlohmann::detail::value_t;
ValidPathInfo res {
StorePath(StorePath::dummy),
UnkeyedValidPathInfo res {
Hash(Hash::dummy),
};
ensureType(json, value_t::object);
res.path = store.parseStorePath(
static_cast<const std::string &>(
ensureType(valueAt(json, "path"), value_t::string)));
res.narHash = Hash::parseAny(
static_cast<const std::string &>(
ensureType(valueAt(json, "narHash"), value_t::string)),

View file

@ -78,6 +78,18 @@ struct UnkeyedValidPathInfo
DECLARE_CMP(UnkeyedValidPathInfo);
virtual ~UnkeyedValidPathInfo() { }
/**
* @param includeImpureInfo If true, variable elements such as the
* registration time are included.
*/
virtual nlohmann::json toJSON(
const Store & store,
bool includeImpureInfo,
HashFormat hashFormat) const;
static UnkeyedValidPathInfo fromJSON(
const Store & store,
const nlohmann::json & json);
};
struct ValidPathInfo : UnkeyedValidPathInfo {
@ -125,18 +137,6 @@ struct ValidPathInfo : UnkeyedValidPathInfo {
Strings shortRefs() const;
/**
* @param includeImpureInfo If true, variable elements such as the
* registration time are included.
*/
virtual nlohmann::json toJSON(
const Store & store,
bool includeImpureInfo,
HashFormat hashFormat) const;
static ValidPathInfo fromJSON(
const Store & store,
const nlohmann::json & json);
ValidPathInfo(const ValidPathInfo & other) = default;
ValidPathInfo(StorePath && path, UnkeyedValidPathInfo info) : UnkeyedValidPathInfo(info), path(std::move(path)) { };

View file

@ -19,8 +19,8 @@ class PathInfoTest : public CharacterizationTest, public LibStoreTest
}
};
static ValidPathInfo makePathInfo(const Store & store, bool includeImpureInfo) {
ValidPathInfo info {
static UnkeyedValidPathInfo makePathInfo(const Store & store, bool includeImpureInfo) {
UnkeyedValidPathInfo info = ValidPathInfo {
store,
"foo",
FixedOutputInfo {
@ -54,7 +54,7 @@ static ValidPathInfo makePathInfo(const Store & store, bool includeImpureInfo) {
TEST_F(PathInfoTest, PathInfo_ ## STEM ## _from_json) { \
readTest(#STEM, [&](const auto & encoded_) { \
auto encoded = json::parse(encoded_); \
ValidPathInfo got = ValidPathInfo::fromJSON( \
UnkeyedValidPathInfo got = UnkeyedValidPathInfo::fromJSON( \
*store, \
encoded); \
auto expected = makePathInfo(*store, PURE); \

View file

@ -38,20 +38,21 @@ static json pathInfoToJSON(
const StorePathSet & storePaths,
bool showClosureSize)
{
json::array_t jsonList = json::array();
json::object_t jsonAllObjects = json::object();
for (auto & storePath : storePaths) {
json jsonObject;
try {
auto info = store.queryPathInfo(storePath);
auto & jsonPath = jsonList.emplace_back(
info->toJSON(store, true, HashFormat::SRI));
jsonObject = info->toJSON(store, true, HashFormat::SRI);
if (showClosureSize) {
StorePathSet closure;
store.computeFSClosure(storePath, closure, false, false);
jsonPath["closureSize"] = getStoreObjectsTotalSize(store, closure);
jsonObject["closureSize"] = getStoreObjectsTotalSize(store, closure);
if (auto * narInfo = dynamic_cast<const NarInfo *>(&*info)) {
uint64_t totalDownloadSize = 0;
@ -64,17 +65,17 @@ static json pathInfoToJSON(
store.printStorePath(p),
store.printStorePath(storePath));
}
jsonPath["closureDownloadSize"] = totalDownloadSize;
jsonObject["closureDownloadSize"] = totalDownloadSize;
}
}
} catch (InvalidPath &) {
auto & jsonPath = jsonList.emplace_back(json::object());
jsonPath["path"] = store.printStorePath(storePath);
jsonPath["valid"] = false;
jsonObject = nullptr;
}
jsonAllObjects[store.printStorePath(storePath)] = std::move(jsonObject);
}
return jsonList;
return jsonAllObjects;
}

View file

@ -43,7 +43,7 @@ R""(
command):
```console
# nix path-info --json --all | jq -r 'sort_by(.registrationTime)[-11:-1][].path'
# nix path-info --json --all | jq -r 'to_entries | sort_by(.value.registrationTime) | .[-11:-1][] | .key'
```
* Show the size of the entire Nix store:
@ -58,13 +58,13 @@ R""(
```console
# nix path-info --json --all --closure-size \
| jq 'map(select(.closureSize > 1e9)) | sort_by(.closureSize) | map([.path, .closureSize])'
| jq 'map_values(.closureSize | select(. < 1e9)) | to_entries | sort_by(.value)'
[
…,
[
"/nix/store/zqamz3cz4dbzfihki2mk7a63mbkxz9xq-nixos-system-machine-20.09.20201112.3090c65",
5887562256
]
{
.key = "/nix/store/zqamz3cz4dbzfihki2mk7a63mbkxz9xq-nixos-system-machine-20.09.20201112.3090c65",
.value = 5887562256,
}
]
```