This addresses several changes from toml11 4.0 bump in
nixpkgs [1].
1. Added more regression tests for timestamp formats.
Special attention needs to be paid to the precision
of the subsecond range for local-time. Prior versions select the closest
(upwards) multiple of 3 with a hard cap of 9 digits.
2. Normalize local datetime and offset datetime to always
use the uppercase separator `T`. This is actually the issue
surfaced in [2]. This canonicalization is basically a requirement
by (a certain reading) of rfc3339 section 5.6 [3].
3. If using toml11 >= 4.0 also keep the old behavior wrt
to the number of digits used for subsecond part of the local-time.
[1]: https://www.github.com/NixOS/nixpkgs/pull/331649
[2]: https://www.github.com/NixOS/nix/issues/11441
[3]: https://datatracker.ietf.org/doc/html/rfc3339
(cherry picked from commit dc769d72cb8ad22a0f89768682b5499a9d2b3d8b)
Upstream-PR: https://github.com/NixOS/nix/pull/13741
Change-Id: Iac4fbe5108be79be585e9670fa42dfd11f3c5e89
(cherry picked from commit 2898b9e7dc)
173 lines
5.6 KiB
C++
173 lines
5.6 KiB
C++
#include "lix/libexpr/eval.hh"
|
|
#include "lix/libexpr/extra-primops.hh"
|
|
|
|
#include <sstream>
|
|
#include <toml.hpp>
|
|
|
|
namespace nix {
|
|
|
|
#if HAVE_TOML11_4
|
|
|
|
/**
|
|
* This is what toml11 < 4.0 did when choosing the subsecond precision.
|
|
* TOML 1.0.0 spec doesn't define how sub-millisecond ranges should be handled and calls it
|
|
* implementation defined behavior. For a lack of a better choice we stick with what older versions
|
|
* of toml11 did [1].
|
|
*
|
|
* [1]:
|
|
* https://github.com/ToruNiina/toml11/blob/dcfe39a783a94e8d52c885e5883a6fbb21529019/toml/datetime.hpp#L282
|
|
*/
|
|
static size_t normalizeSubsecondPrecision(toml::local_time lt)
|
|
{
|
|
auto millis = lt.millisecond;
|
|
auto micros = lt.microsecond;
|
|
auto nanos = lt.nanosecond;
|
|
if (millis != 0 || micros != 0 || nanos != 0) {
|
|
if (micros != 0 || nanos != 0) {
|
|
if (nanos != 0) {
|
|
return 9;
|
|
}
|
|
return 6;
|
|
}
|
|
return 3;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Normalize date/time formats to serialize to the same strings as versions prior to toml11 4.0.
|
|
*
|
|
* Several things to consider:
|
|
*
|
|
* 1. Sub-millisecond range is represented the same way as in toml11 versions prior to 4.0.
|
|
* Precision is rounded towards the next multiple of 3 or capped at 9 digits.
|
|
* 2. Seconds must be specified. This may become optional in (yet unreleased) TOML 1.1.0, but 1.0.0
|
|
* defined local time in terms of RFC3339 [1].
|
|
* 3. date-time separator (`t`, `T` or space ` `) is canonicalized to an upper T. This is compliant
|
|
* with RFC3339 [1] 5.6: > Applications that generate this format SHOULD use upper case letters.
|
|
*
|
|
* [1]: https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
|
|
*/
|
|
static void normalizeDatetimeFormat(toml::value & t)
|
|
{
|
|
if (t.is_local_datetime()) {
|
|
auto & ldt = t.as_local_datetime();
|
|
t.as_local_datetime_fmt() = {
|
|
.delimiter = toml::datetime_delimiter_kind::upper_T,
|
|
// https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
|
|
.has_seconds = true, // Mandated by TOML 1.0.0
|
|
.subsecond_precision = normalizeSubsecondPrecision(ldt.time),
|
|
};
|
|
return;
|
|
}
|
|
|
|
if (t.is_offset_datetime()) {
|
|
auto & odt = t.as_offset_datetime();
|
|
t.as_offset_datetime_fmt() = {
|
|
.delimiter = toml::datetime_delimiter_kind::upper_T,
|
|
// https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
|
|
.has_seconds = true, // Mandated by TOML 1.0.0
|
|
.subsecond_precision = normalizeSubsecondPrecision(odt.time),
|
|
};
|
|
return;
|
|
}
|
|
|
|
if (t.is_local_time()) {
|
|
auto & lt = t.as_local_time();
|
|
t.as_local_time_fmt() = {
|
|
.has_seconds = true, // Mandated by TOML 1.0.0
|
|
.subsecond_precision = normalizeSubsecondPrecision(lt),
|
|
};
|
|
return;
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
void prim_fromTOML(EvalState & state, Value ** args, Value & val)
|
|
{
|
|
auto toml = state.forceStringNoCtx(
|
|
*args[0], noPos, "while evaluating the argument passed to builtins.fromTOML"
|
|
);
|
|
|
|
std::istringstream tomlStream(std::string{toml});
|
|
|
|
auto visit = [&](this const auto & self, Value & v, toml::value t) -> void {
|
|
switch (t.type()) {
|
|
case toml::value_t::table: {
|
|
auto table = toml::get<toml::table>(t);
|
|
auto attrs = state.ctx.buildBindings(table.size());
|
|
|
|
for (auto & elem : table) {
|
|
self(attrs.alloc(elem.first), elem.second);
|
|
}
|
|
|
|
v.mkAttrs(attrs);
|
|
} break;
|
|
case toml::value_t::array: {
|
|
auto array = toml::get<std::vector<toml::value>>(t);
|
|
|
|
size_t size = array.size();
|
|
v = state.ctx.mem.newList(size);
|
|
for (size_t i = 0; i < size; ++i) {
|
|
self(*(v.listElems()[i] = state.ctx.mem.allocValue()), array[i]);
|
|
}
|
|
} break;
|
|
case toml::value_t::boolean:
|
|
v.mkBool(toml::get<bool>(t));
|
|
break;
|
|
case toml::value_t::integer:
|
|
v.mkInt(toml::get<int64_t>(t));
|
|
break;
|
|
case toml::value_t::floating:
|
|
v.mkFloat(toml::get<NixFloat>(t));
|
|
break;
|
|
case toml::value_t::string:
|
|
v.mkString(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: {
|
|
if (experimentalFeatureSettings.isEnabled(Xp::ParseTomlTimestamps)) {
|
|
#if HAVE_TOML11_4
|
|
normalizeDatetimeFormat(t);
|
|
#endif
|
|
auto attrs = state.ctx.buildBindings(2);
|
|
attrs.alloc("_type").mkString("timestamp");
|
|
std::ostringstream s;
|
|
s << t;
|
|
attrs.alloc("value").mkString(s.str());
|
|
v.mkAttrs(attrs);
|
|
} else {
|
|
// NOLINTNEXTLINE(lix-foreign-exceptions)
|
|
throw std::runtime_error("Dates and times are not supported");
|
|
}
|
|
} break;
|
|
case toml::value_t::empty:
|
|
v.mkNull();
|
|
break;
|
|
}
|
|
};
|
|
|
|
try {
|
|
visit(
|
|
val,
|
|
toml::parse(
|
|
tomlStream,
|
|
"fromTOML" /* the "filename" */
|
|
#if HAVE_TOML11_4
|
|
,
|
|
toml::spec::v(
|
|
1, 0, 0
|
|
) // Be explicit that we are parsing TOML 1.0.0 without extensions
|
|
#endif
|
|
)
|
|
);
|
|
} catch (std::exception & e) { // NOLINT(lix-foreign-exceptions) // TODO: toml::syntax_error
|
|
state.ctx.errors.make<EvalError>("while parsing TOML: %s", e.what()).debugThrow();
|
|
}
|
|
}
|
|
|
|
}
|