2014-07-04 14:34:15 +03:00
|
|
|
#include "json-to-value.hh"
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
#include "value.hh"
|
|
|
|
#include "eval.hh"
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
#include <variant>
|
|
|
|
#include <nlohmann/json.hpp>
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
using json = nlohmann::json;
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
namespace nix {
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
// for more information, refer to
|
|
|
|
// https://github.com/nlohmann/json/blob/master/include/nlohmann/detail/input/json_sax.hpp
|
|
|
|
class JSONSax : nlohmann::json_sax<json> {
|
|
|
|
class JSONState {
|
|
|
|
protected:
|
2020-04-16 18:28:32 +03:00
|
|
|
std::unique_ptr<JSONState> parent;
|
|
|
|
RootValue v;
|
2020-01-09 18:38:27 +02:00
|
|
|
public:
|
2020-04-16 18:28:32 +03:00
|
|
|
virtual std::unique_ptr<JSONState> resolve(EvalState &)
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
|
|
|
throw std::logic_error("tried to close toplevel json parser state");
|
2020-04-16 18:28:32 +03:00
|
|
|
}
|
|
|
|
explicit JSONState(std::unique_ptr<JSONState> && p) : parent(std::move(p)) {}
|
|
|
|
explicit JSONState(Value * v) : v(allocRootValue(v)) {}
|
|
|
|
JSONState(JSONState & p) = delete;
|
|
|
|
Value & value(EvalState & state)
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
2020-04-16 18:28:32 +03:00
|
|
|
if (!v)
|
|
|
|
v = allocRootValue(state.allocValue());
|
|
|
|
return **v;
|
|
|
|
}
|
|
|
|
virtual ~JSONState() {}
|
|
|
|
virtual void add() {}
|
2020-01-09 18:38:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class JSONObjectState : public JSONState {
|
|
|
|
using JSONState::JSONState;
|
2020-04-16 18:28:32 +03:00
|
|
|
ValueMap attrs;
|
|
|
|
std::unique_ptr<JSONState> resolve(EvalState & state) override
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
2022-01-04 21:29:17 +02:00
|
|
|
auto attrs2 = state.buildBindings(attrs.size());
|
2020-01-09 18:38:27 +02:00
|
|
|
for (auto & i : attrs)
|
2022-01-04 21:29:17 +02:00
|
|
|
attrs2.insert(i.first, i.second);
|
|
|
|
parent->value(state).mkAttrs(attrs2.alreadySorted());
|
2020-01-09 23:46:41 +02:00
|
|
|
return std::move(parent);
|
2020-01-09 18:38:27 +02:00
|
|
|
}
|
2020-04-16 18:28:32 +03:00
|
|
|
void add() override { v = nullptr; }
|
2020-01-09 18:38:27 +02:00
|
|
|
public:
|
2020-04-16 18:28:32 +03:00
|
|
|
void key(string_t & name, EvalState & state)
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
2020-04-16 18:28:32 +03:00
|
|
|
attrs.insert_or_assign(state.symbols.create(name), &value(state));
|
2020-01-09 18:38:27 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class JSONListState : public JSONState {
|
2020-04-16 18:28:32 +03:00
|
|
|
ValueVector values;
|
|
|
|
std::unique_ptr<JSONState> resolve(EvalState & state) override
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
2024-03-14 20:10:31 +02:00
|
|
|
auto list = state.buildList(values.size());
|
|
|
|
for (const auto & [n, v2] : enumerate(list))
|
|
|
|
v2 = values[n];
|
|
|
|
parent->value(state).mkList(list);
|
2020-01-09 23:46:41 +02:00
|
|
|
return std::move(parent);
|
2020-01-09 18:38:27 +02:00
|
|
|
}
|
2020-04-16 18:28:32 +03:00
|
|
|
void add() override {
|
|
|
|
values.push_back(*v);
|
2020-01-09 18:38:27 +02:00
|
|
|
v = nullptr;
|
2020-04-16 18:28:32 +03:00
|
|
|
}
|
2020-01-09 18:38:27 +02:00
|
|
|
public:
|
2020-04-16 18:28:32 +03:00
|
|
|
JSONListState(std::unique_ptr<JSONState> && p, std::size_t reserve) : JSONState(std::move(p))
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
|
|
|
values.reserve(reserve);
|
|
|
|
}
|
|
|
|
};
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
EvalState & state;
|
2020-04-16 18:28:32 +03:00
|
|
|
std::unique_ptr<JSONState> rs;
|
2020-01-07 01:06:49 +02:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
public:
|
|
|
|
JSONSax(EvalState & state, Value & v) : state(state), rs(new JSONState(&v)) {};
|
2020-01-07 01:06:49 +02:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool null()
|
2020-01-07 01:06:49 +02:00
|
|
|
{
|
2022-01-04 19:40:39 +02:00
|
|
|
rs->value(state).mkNull();
|
|
|
|
rs->add();
|
|
|
|
return true;
|
2020-01-07 01:06:49 +02:00
|
|
|
}
|
2020-01-09 18:38:27 +02:00
|
|
|
|
|
|
|
bool boolean(bool val)
|
2020-01-07 01:06:49 +02:00
|
|
|
{
|
2022-01-04 19:40:39 +02:00
|
|
|
rs->value(state).mkBool(val);
|
|
|
|
rs->add();
|
|
|
|
return true;
|
2020-01-07 01:06:49 +02:00
|
|
|
}
|
2020-01-09 18:38:27 +02:00
|
|
|
|
|
|
|
bool number_integer(number_integer_t val)
|
2020-01-07 01:06:49 +02:00
|
|
|
{
|
2022-01-04 19:40:39 +02:00
|
|
|
rs->value(state).mkInt(val);
|
|
|
|
rs->add();
|
|
|
|
return true;
|
2020-01-07 01:06:49 +02:00
|
|
|
}
|
2020-01-09 18:38:27 +02:00
|
|
|
|
|
|
|
bool number_unsigned(number_unsigned_t val)
|
2020-01-07 01:06:49 +02:00
|
|
|
{
|
2022-01-04 19:40:39 +02:00
|
|
|
rs->value(state).mkInt(val);
|
|
|
|
rs->add();
|
|
|
|
return true;
|
2020-01-07 01:06:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:28:32 +03:00
|
|
|
bool number_float(number_float_t val, const string_t & s)
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
2022-01-04 19:40:39 +02:00
|
|
|
rs->value(state).mkFloat(val);
|
|
|
|
rs->add();
|
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:28:32 +03:00
|
|
|
bool string(string_t & val)
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
2022-01-04 19:24:42 +02:00
|
|
|
rs->value(state).mkString(val);
|
|
|
|
rs->add();
|
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
2022-01-04 19:24:42 +02:00
|
|
|
|
2020-09-19 17:46:02 +03:00
|
|
|
#if NLOHMANN_JSON_VERSION_MAJOR >= 3 && NLOHMANN_JSON_VERSION_MINOR >= 8
|
|
|
|
bool binary(binary_t&)
|
|
|
|
{
|
|
|
|
// This function ought to be unreachable
|
|
|
|
assert(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool start_object(std::size_t len)
|
|
|
|
{
|
2020-01-09 23:46:41 +02:00
|
|
|
rs = std::make_unique<JSONObjectState>(std::move(rs));
|
2020-01-09 18:38:27 +02:00
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:28:32 +03:00
|
|
|
bool key(string_t & name)
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
2020-01-09 23:46:41 +02:00
|
|
|
dynamic_cast<JSONObjectState*>(rs.get())->key(name, state);
|
2020-01-09 18:38:27 +02:00
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool end_object() {
|
2020-01-09 23:46:41 +02:00
|
|
|
rs = rs->resolve(state);
|
2020-01-09 18:38:27 +02:00
|
|
|
rs->add();
|
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool end_array() {
|
|
|
|
return end_object();
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool start_array(size_t len) {
|
2020-01-09 23:46:41 +02:00
|
|
|
rs = std::make_unique<JSONListState>(std::move(rs),
|
|
|
|
len != std::numeric_limits<size_t>::max() ? len : 128);
|
2020-01-09 18:38:27 +02:00
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool parse_error(std::size_t, const std::string&, const nlohmann::detail::exception& ex) {
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
throw JSONParseError("%s", ex.what());
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
2020-01-09 18:38:27 +02:00
|
|
|
};
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2022-01-21 15:44:00 +02:00
|
|
|
void parseJSON(EvalState & state, const std::string_view & s_, Value & v)
|
2014-07-04 14:34:15 +03:00
|
|
|
{
|
2020-01-09 18:38:27 +02:00
|
|
|
JSONSax parser(state, v);
|
|
|
|
bool res = json::sax_parse(s_, &parser);
|
|
|
|
if (!res)
|
|
|
|
throw JSONParseError("Invalid JSON Value");
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|