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