diff --git a/src/libutil/configuration.cc b/src/libutil/configuration.cc index e520a6e38..ee24bb4e5 100644 --- a/src/libutil/configuration.cc +++ b/src/libutil/configuration.cc @@ -433,10 +433,18 @@ std::string BaseSetting::to_string() const [](const auto & kvpair) { return kvpair.first + "=" + kvpair.second; }); } +static Path parsePath(const AbstractSetting & s, const std::string & str) +{ + if (str == "") + throw UsageError("setting '%s' is a path and paths cannot be empty", s.name); + else + return canonPath(str); +} + template<> std::filesystem::path BaseSetting::parse(const std::string & str) const { - return std::filesystem::path(str).lexically_normal(); + return parsePath(*this, str); } template<> @@ -445,6 +453,22 @@ std::string BaseSetting::to_string() const return value.string(); } +template<> +std::optional +BaseSetting>::parse(const std::string & str) const +{ + if (str == "") + return std::nullopt; + else + return parsePath(*this, str); +} + +template<> +std::string BaseSetting>::to_string() const +{ + return value ? value->string() : ""; +} + template class BaseSetting; template class BaseSetting; template class BaseSetting; @@ -458,14 +482,7 @@ template class BaseSetting; template class BaseSetting; template class BaseSetting>; template class BaseSetting; - -static Path parsePath(const AbstractSetting & s, const std::string & str) -{ - if (str == "") - throw UsageError("setting '%s' is a path and paths cannot be empty", s.name); - else - return canonPath(str); -} +template class BaseSetting>; PathSetting::PathSetting( Config * options, diff --git a/src/libutil/include/nix/util/file-path.hh b/src/libutil/include/nix/util/file-path.hh index 25349eaf7..52dae32ff 100644 --- a/src/libutil/include/nix/util/file-path.hh +++ b/src/libutil/include/nix/util/file-path.hh @@ -5,6 +5,7 @@ #include "nix/util/types.hh" #include "nix/util/os-string.hh" +#include "nix/util/json-non-null.hh" namespace nix { @@ -53,4 +54,8 @@ std::optional maybePath(PathView path); std::filesystem::path pathNG(PathView path); +template<> +struct json_avoids_null : std::true_type +{}; + } // namespace nix diff --git a/src/libutil/include/nix/util/logging.hh b/src/libutil/include/nix/util/logging.hh index ec107304b..4673895aa 100644 --- a/src/libutil/include/nix/util/logging.hh +++ b/src/libutil/include/nix/util/logging.hh @@ -54,7 +54,7 @@ struct LoggerSettings : Config expression evaluation errors. )"}; - Setting jsonLogPath{ + Setting> jsonLogPath{ this, {}, "json-log-path", diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 48dc9e069..8f7ec2d29 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -367,10 +367,10 @@ std::unique_ptr makeJSONLogger(const std::filesystem::path & path, bool void applyJSONLogger() { - if (!loggerSettings.jsonLogPath.get().empty()) { + if (auto & opt = loggerSettings.jsonLogPath.get()) { try { std::vector> loggers; - loggers.push_back(makeJSONLogger(loggerSettings.jsonLogPath.get(), false)); + loggers.push_back(makeJSONLogger(*opt, false)); try { logger = makeTeeLogger(std::move(logger), std::move(loggers)); } catch (...) {