treewide: add nix::JSON

this will become a proper specialization of `nlohmann::basic_json` soon.
specialing basic_json will let us get rid of our `adl_serializer` hacks,
and it'll open the door to better enum serializing behavior without also
forcing all those who use lix as a library to set certain defines (which
may not even be possible depending on how those users use json already).

Change-Id: I5228d2b9df581a189552c993363207cfbd20f445
This commit is contained in:
eldritch horrors
2025-03-23 20:42:39 +00:00
parent a186bc5021
commit 19d7c8352e
81 changed files with 372 additions and 387 deletions
@@ -69,7 +69,7 @@ auto topoSort(const std::set<AggregateJob> &items)
}
} // namespace
auto resolveNamedConstituents(const std::map<std::string, nlohmann::json> &jobs)
auto resolveNamedConstituents(const std::map<std::string, nix::JSON> &jobs)
-> std::variant<std::vector<AggregateJob>, DependencyCycle> {
std::set<AggregateJob> aggregateJobs;
for (auto const &[jobName, job] : jobs) {
@@ -80,7 +80,7 @@ auto resolveNamedConstituents(const std::map<std::string, nlohmann::json> &jobs)
auto isBroken = [&brokenJobs,
&jobName](const std::string &childJobName,
const nlohmann::json &job) -> bool {
const nix::JSON &job) -> bool {
if (job.find("error") != job.end()) {
std::string error = job["error"];
nix::logger->log(
@@ -118,7 +118,7 @@ auto resolveNamedConstituents(const std::map<std::string, nlohmann::json> &jobs)
}
}
void rewriteAggregates(std::map<std::string, nlohmann::json> &jobs,
void rewriteAggregates(std::map<std::string, nix::JSON> &jobs,
const std::vector<AggregateJob> &aggregateJobs,
nix::ref<nix::Store> &store, nix::Path &gcRootsDir,
nix::AsyncIoRoot &aio) {
@@ -36,10 +36,10 @@ struct AggregateJob {
}
};
auto resolveNamedConstituents(const std::map<std::string, nlohmann::json> &jobs)
auto resolveNamedConstituents(const std::map<std::string, nix::JSON> &jobs)
-> std::variant<std::vector<AggregateJob>, DependencyCycle>;
void rewriteAggregates(std::map<std::string, nlohmann::json> &jobs,
void rewriteAggregates(std::map<std::string, nix::JSON> &jobs,
const std::vector<AggregateJob> &aggregateJobs,
nix::ref<nix::Store> &store, nix::Path &gcRootsDir,
nix::AsyncIoRoot &aio);
+6 -6
View File
@@ -73,7 +73,7 @@ Drv::Drv(std::string &attrPath, nix::EvalState &state, nix::DrvInfo &drvInfo,
}
if (args.meta) {
nlohmann::json meta_;
nix::JSON meta_;
for (auto &metaName : drvInfo.queryMetaNames(state)) {
nix::NixStringContext context;
std::stringstream ss;
@@ -88,7 +88,7 @@ Drv::Drv(std::string &attrPath, nix::EvalState &state, nix::DrvInfo &drvInfo,
nix::printValueAsJSON(state, true, *metaValue, nix::noPos, ss,
context);
meta_[metaName] = nlohmann::json::parse(ss.str());
meta_[metaName] = nix::JSON::parse(ss.str());
}
meta = meta_;
}
@@ -114,14 +114,14 @@ Drv::Drv(std::string &attrPath, nix::EvalState &state, nix::DrvInfo &drvInfo,
system = drv.platform;
}
void to_json(nlohmann::json &json, const Drv &drv) {
std::map<std::string, nlohmann::json> outputsJson;
void to_json(nix::JSON &json, const Drv &drv) {
std::map<std::string, nix::JSON> outputsJson;
for (auto &[name, optPath] : drv.outputs) {
outputsJson[name] =
optPath ? nlohmann::json(*optPath) : nlohmann::json(nullptr);
optPath ? nix::JSON(*optPath) : nix::JSON(nullptr);
}
json = nlohmann::json{{"name", drv.name},
json = nix::JSON{{"name", drv.name},
{"system", drv.system},
{"drvPath", drv.drvPath},
{"outputs", outputsJson},
+2 -2
View File
@@ -34,13 +34,13 @@ struct Drv {
enum class CacheStatus { Cached, Uncached, Unknown } cacheStatus;
std::map<std::string, std::optional<std::string>> outputs;
std::map<std::string, std::set<std::string>> inputDrvs;
std::optional<nlohmann::json> meta;
std::optional<nix::JSON> meta;
std::optional<Constituents> constituents;
Drv(std::string &attrPath, nix::EvalState &state, nix::DrvInfo &drvInfo,
MyArgs &args, std::optional<Constituents> constituents);
};
void to_json(nlohmann::json &json, const Drv &drv);
void to_json(nix::JSON &json, const Drv &drv);
void register_gc_root(nix::Path &gcRootsDir, std::string &drvPath,
const nix::ref<nix::Store> &store, nix::AsyncIoRoot &aio);
+13 -14
View File
@@ -43,7 +43,6 @@
#include "worker.hh"
using namespace nix;
using namespace nlohmann;
using Processor = std::function<void(
ref<nix::eval_cache::CachingEvaluator> state, Bindings &autoArgs,
@@ -75,7 +74,7 @@ struct Proc {
Bindings &autoArgs = *myArgs.getAutoArgs(*evaluator);
proc(evaluator, autoArgs, *to, *from, myArgs, aio);
} catch (Error &e) {
nlohmann::json err;
JSON err;
auto msg = e.msg();
err["error"] = nix::filterANSIEscapes(msg, true);
printError(msg);
@@ -150,10 +149,10 @@ struct Thread {
};
struct State {
std::set<json> todo = json::array({json::array()});
std::set<json> active;
std::set<JSON> todo = JSON::array({JSON::array()});
std::set<JSON> active;
std::exception_ptr exc;
std::map<std::string, nlohmann::json> jobs;
std::map<std::string, JSON> jobs;
};
void handleBrokenWorkerPipe(Proc &proc, std::string_view msg, bool retry = true) {
@@ -226,7 +225,7 @@ void handleBrokenWorkerPipe(Proc &proc, std::string_view msg, bool retry = true)
}
}
std::string joinAttrPath(json &attrPath) {
std::string joinAttrPath(JSON &attrPath) {
std::string joined;
for (auto &element : attrPath) {
if (!joined.empty()) {
@@ -262,9 +261,9 @@ void collector(MyArgs &myArgs, Sync<State> &state_,
continue;
} else if (s != "next") {
try {
auto json = json::parse(s);
auto json = JSON::parse(s);
throw Error("worker error: %s", (std::string)json["error"]);
} catch (const json::exception &e) {
} catch (const JSON::exception &e) {
throw Error(
"Received invalid JSON from worker: %s\n json: '%s'",
e.what(), s);
@@ -272,7 +271,7 @@ void collector(MyArgs &myArgs, Sync<State> &state_,
}
/* Wait for a job name to become available. */
json attrPath;
JSON attrPath;
while (true) {
checkInterrupt();
@@ -306,20 +305,20 @@ void collector(MyArgs &myArgs, Sync<State> &state_,
joinAttrPath(attrPath) + "'";
handleBrokenWorkerPipe(*proc.get(), msg);
}
json response;
JSON response;
try {
response = json::parse(respString);
} catch (const json::exception &e) {
response = JSON::parse(respString);
} catch (const JSON::exception &e) {
throw Error(
"Received invalid JSON from worker: %s\n json: '%s'",
e.what(), respString);
}
/* Handle the response. */
std::vector<json> newAttrs;
std::vector<JSON> newAttrs;
if (response.find("attrs") != response.end()) {
for (auto &i : response["attrs"]) {
json newAttr = json(response["attrPath"]);
JSON newAttr = JSON(response["attrPath"]);
newAttr.emplace_back(i);
newAttrs.push_back(newAttr);
}
+7 -7
View File
@@ -61,7 +61,7 @@ static nix::Value *releaseExprTopLevelValue(nix::EvalState &state,
return vRoot;
}
static std::string attrPathJoin(nlohmann::json input) {
static std::string attrPathJoin(nix::JSON input) {
return std::accumulate(input.begin(), input.end(), std::string(),
[](std::string ss, std::string s) {
// Escape token if containing dots
@@ -157,12 +157,12 @@ void worker(nix::ref<nix::eval_cache::CachingEvaluator> evaluator,
s.data());
abort();
}
auto path = nlohmann::json::parse(s.substr(3));
auto path = nix::JSON::parse(s.substr(3));
auto attrPathS = attrPathJoin(path);
/* Evaluate it and send info back to the collector. */
nlohmann::json reply =
nlohmann::json{{"attr", attrPathS}, {"attrPath", path}};
nix::JSON reply =
nix::JSON{{"attr", attrPathS}, {"attrPath", path}};
try {
auto vTmp =
nix::findAlongAttrPath(*state, attrPathS, autoArgs, *vRoot)
@@ -187,7 +187,7 @@ void worker(nix::ref<nix::eval_cache::CachingEvaluator> evaluator,
done. */
register_gc_root(args.gcRootsDir, drv.drvPath, evaluator->store, aio);
} else {
auto attrs = nlohmann::json::array();
auto attrs = nix::JSON::array();
bool recurse =
args.forceRecurse ||
path.size() == 0; // Dont require `recurseForDerivations
@@ -210,11 +210,11 @@ void worker(nix::ref<nix::eval_cache::CachingEvaluator> evaluator,
if (recurse)
reply["attrs"] = std::move(attrs);
else
reply["attrs"] = nlohmann::json::array();
reply["attrs"] = nix::JSON::array();
}
} else {
// We ignore everything that cannot be build
reply["attrs"] = nlohmann::json::array();
reply["attrs"] = nix::JSON::array();
}
} catch (nix::EvalError &e) {
auto err = e.info();