2018-07-03 16:15:59 +00:00
|
|
|
#include "primops.hh"
|
|
|
|
#include "eval-inline.hh"
|
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
#include "../../toml11/toml.hpp"
|
2018-07-03 16:15:59 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
static void prim_fromTOML(EvalState & state, const Pos & pos, Value * * args, Value & val)
|
2018-07-03 16:15:59 +00:00
|
|
|
{
|
|
|
|
auto toml = state.forceStringNoCtx(*args[0], pos);
|
|
|
|
|
|
|
|
std::istringstream tomlStream(toml);
|
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
std::function<void(Value &, toml::value)> visit;
|
2018-07-03 16:15:59 +00:00
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
visit = [&](Value & v, toml::value t) {
|
2018-07-03 16:15:59 +00:00
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
switch(t.type())
|
|
|
|
{
|
|
|
|
case toml::value_t::table:
|
|
|
|
{
|
|
|
|
auto table = toml::get<toml::table>(t);
|
2018-07-03 16:15:59 +00:00
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
size_t size = 0;
|
|
|
|
for (auto & i : table) { (void) i; size++; }
|
2018-07-03 16:15:59 +00:00
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
state.mkAttrs(v, size);
|
2018-07-03 16:15:59 +00:00
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
for(auto & elem: table) {
|
2019-06-24 15:09:09 +00:00
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
auto & v2 = *state.allocAttr(v, state.symbols.create(elem.first));
|
2019-06-24 15:09:09 +00:00
|
|
|
|
2021-12-17 21:03:48 +00:00
|
|
|
// TODO: note about creating children here in old code
|
|
|
|
visit(v2, elem.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;;
|
|
|
|
case toml::value_t::array:
|
|
|
|
{
|
|
|
|
auto array = toml::get<std::vector<toml::value>>(t);
|
|
|
|
|
|
|
|
size_t size = array.size();
|
|
|
|
state.mkList(v, size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
visit(*(v.listElems()[i] = state.allocValue()), array[i]);
|
|
|
|
}
|
|
|
|
break;;
|
|
|
|
case toml::value_t::boolean:
|
|
|
|
mkBool(v, toml::get<bool>(t));
|
|
|
|
break;;
|
|
|
|
case toml::value_t::integer:
|
2021-12-17 22:15:26 +00:00
|
|
|
mkInt(v, toml::get<long>(t));
|
2021-12-17 21:03:48 +00:00
|
|
|
break;;
|
|
|
|
case toml::value_t::floating:
|
|
|
|
mkFloat(v, toml::get<double>(t));
|
|
|
|
break;;
|
|
|
|
case toml::value_t::string:
|
|
|
|
mkString(v, toml::get<std::string>(t));
|
|
|
|
break;;
|
|
|
|
case toml::value_t::local_datetime:
|
|
|
|
case toml::value_t::offset_datetime:
|
|
|
|
case toml::value_t::local_date:
|
|
|
|
case toml::value_t::local_time:
|
|
|
|
// TODO: convert to string?
|
|
|
|
throw std::runtime_error("Dates and times are not supported");
|
|
|
|
break;;
|
|
|
|
case toml::value_t::empty:
|
|
|
|
mkNull(v);
|
|
|
|
break;;
|
2019-06-24 15:09:09 +00:00
|
|
|
|
2018-07-03 16:15:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2021-12-17 21:03:48 +00:00
|
|
|
visit(val, toml::parse(tomlStream)); // TODO give filename
|
|
|
|
} catch (std::exception & e) { // TODO: toml::syntax_error
|
2020-06-15 12:06:58 +00:00
|
|
|
throw EvalError({
|
2021-01-20 23:27:36 +00:00
|
|
|
.msg = hintfmt("while parsing a TOML string: %s", e.what()),
|
2020-06-23 21:30:13 +00:00
|
|
|
.errPos = pos
|
2020-06-15 12:06:58 +00:00
|
|
|
});
|
2018-07-03 16:15:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static RegisterPrimOp primop_fromTOML("fromTOML", 1, prim_fromTOML);
|
2018-07-03 16:15:59 +00:00
|
|
|
|
|
|
|
}
|