2013-11-19 01:33:06 +02:00
|
|
|
#include "value-to-json.hh"
|
2013-11-19 01:03:11 +02:00
|
|
|
#include "eval-inline.hh"
|
|
|
|
#include "util.hh"
|
2022-12-20 15:58:39 +02:00
|
|
|
#include "store-api.hh"
|
2013-11-19 01:03:11 +02:00
|
|
|
|
|
|
|
#include <cstdlib>
|
2014-09-30 01:41:18 +03:00
|
|
|
#include <iomanip>
|
2022-11-16 17:49:49 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2013-11-19 01:03:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
2022-11-16 17:49:49 +02:00
|
|
|
using json = nlohmann::json;
|
|
|
|
json printValueAsJSON(EvalState & state, bool strict,
|
|
|
|
Value & v, const PosIdx pos, PathSet & context, bool copyToStore)
|
2013-11-19 01:03:11 +02:00
|
|
|
{
|
|
|
|
checkInterrupt();
|
|
|
|
|
2021-10-26 00:13:35 +03:00
|
|
|
if (strict) state.forceValue(v, pos);
|
2013-11-19 01:03:11 +02:00
|
|
|
|
2022-11-16 17:49:49 +02:00
|
|
|
json out;
|
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
switch (v.type()) {
|
2013-11-19 01:03:11 +02:00
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nInt:
|
2022-11-16 17:49:49 +02:00
|
|
|
out = v.integer;
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nBool:
|
2022-11-16 17:49:49 +02:00
|
|
|
out = v.boolean;
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nString:
|
2013-11-19 01:03:11 +02:00
|
|
|
copyContext(v, context);
|
2022-11-16 17:49:49 +02:00
|
|
|
out = v.string.s;
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nPath:
|
2022-08-16 13:23:37 +03:00
|
|
|
if (copyToStore)
|
2022-12-20 15:58:39 +02:00
|
|
|
out = state.store->printStorePath(state.copyPathToStore(context, v.path));
|
2022-08-16 13:23:37 +03:00
|
|
|
else
|
2022-11-16 17:49:49 +02:00
|
|
|
out = v.path;
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nNull:
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nAttrs: {
|
2021-10-27 22:48:48 +03:00
|
|
|
auto maybeString = state.tryAttrsToString(pos, v, context, false, false);
|
2019-10-27 11:15:51 +02:00
|
|
|
if (maybeString) {
|
2022-11-16 17:49:49 +02:00
|
|
|
out = *maybeString;
|
2019-10-27 11:15:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto i = v.attrs->find(state.sOutPath);
|
2013-11-19 01:03:11 +02:00
|
|
|
if (i == v.attrs->end()) {
|
2022-11-16 17:49:49 +02:00
|
|
|
out = json::object();
|
2013-11-19 01:03:11 +02:00
|
|
|
StringSet names;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & j : *v.attrs)
|
2022-03-05 15:40:24 +02:00
|
|
|
names.emplace(state.symbols[j.name]);
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & j : names) {
|
|
|
|
Attr & a(*v.attrs->find(state.symbols.create(j)));
|
2022-11-16 17:49:49 +02:00
|
|
|
out[j] = printValueAsJSON(state, strict, *a.value, a.pos, context, copyToStore);
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|
|
|
|
} else
|
2022-11-16 17:49:49 +02:00
|
|
|
return printValueAsJSON(state, strict, *i->value, i->pos, context, copyToStore);
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nList: {
|
2022-11-16 17:49:49 +02:00
|
|
|
out = json::array();
|
|
|
|
for (auto elem : v.listItems())
|
|
|
|
out.push_back(printValueAsJSON(state, strict, *elem, pos, context, copyToStore));
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nExternal:
|
2022-11-16 17:49:49 +02:00
|
|
|
return v.external->printValueAsJSON(state, strict, context, copyToStore);
|
2014-11-30 20:16:19 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nFloat:
|
2022-11-16 17:49:49 +02:00
|
|
|
out = v.fpoint;
|
2016-01-05 01:40:40 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nThunk:
|
|
|
|
case nFunction:
|
2021-10-27 22:48:48 +03:00
|
|
|
auto e = TypeError({
|
2021-10-26 00:13:35 +03:00
|
|
|
.msg = hintfmt("cannot convert %1% to JSON", showType(v)),
|
2022-03-04 20:31:59 +02:00
|
|
|
.errPos = state.positions[v.determinePos(pos)]
|
2021-10-26 00:13:35 +03:00
|
|
|
});
|
2022-03-04 20:31:59 +02:00
|
|
|
e.addTrace(state.positions[pos], hintfmt("message for the trace"));
|
2022-05-06 17:47:21 +03:00
|
|
|
state.debugThrowLastTrace(e);
|
2022-04-08 21:34:27 +03:00
|
|
|
throw e;
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|
2022-11-16 17:49:49 +02:00
|
|
|
return out;
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 19:55:55 +03:00
|
|
|
void printValueAsJSON(EvalState & state, bool strict,
|
2022-08-16 13:23:37 +03:00
|
|
|
Value & v, const PosIdx pos, std::ostream & str, PathSet & context, bool copyToStore)
|
2016-08-26 19:55:55 +03:00
|
|
|
{
|
2022-11-16 17:49:49 +02:00
|
|
|
str << printValueAsJSON(state, strict, v, pos, context, copyToStore);
|
2016-08-26 19:55:55 +03:00
|
|
|
}
|
2013-11-19 01:03:11 +02:00
|
|
|
|
2022-11-16 17:49:49 +02:00
|
|
|
json ExternalValueBase::printValueAsJSON(EvalState & state, bool strict,
|
|
|
|
PathSet & context, bool copyToStore) const
|
2014-11-30 20:16:19 +02:00
|
|
|
{
|
2022-05-25 13:32:22 +03:00
|
|
|
state.debugThrowLastTrace(TypeError("cannot convert %1% to JSON", showType()));
|
2014-11-30 20:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|