Config: Use nlohmann/json
This commit is contained in:
parent
b4ef3d7078
commit
acb99f03f9
|
@ -56,3 +56,5 @@
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
@ -160,9 +162,9 @@ template<> std::string BaseSetting<SandboxMode>::to_string() const
|
||||||
else abort();
|
else abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> void BaseSetting<SandboxMode>::toJSON(JSONPlaceholder & out)
|
template<> nlohmann::json BaseSetting<SandboxMode>::toJSON()
|
||||||
{
|
{
|
||||||
AbstractSetting::toJSON(out);
|
return AbstractSetting::toJSON();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::string & category)
|
template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::string & category)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "config.hh"
|
#include "config.hh"
|
||||||
#include "args.hh"
|
#include "args.hh"
|
||||||
#include "json.hh"
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
@ -131,15 +132,17 @@ void Config::resetOverriden()
|
||||||
s.second.setting->overriden = false;
|
s.second.setting->overriden = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::toJSON(JSONObject & out)
|
nlohmann::json Config::toJSON()
|
||||||
{
|
{
|
||||||
|
auto res = nlohmann::json::object();
|
||||||
for (auto & s : _settings)
|
for (auto & s : _settings)
|
||||||
if (!s.second.isAlias) {
|
if (!s.second.isAlias) {
|
||||||
JSONObject out2(out.object(s.first));
|
auto obj = nlohmann::json::object();
|
||||||
out2.attr("description", s.second.setting->description);
|
obj.emplace("description", s.second.setting->description);
|
||||||
JSONPlaceholder out3(out2.placeholder("value"));
|
obj.emplace("value", s.second.setting->toJSON());
|
||||||
s.second.setting->toJSON(out3);
|
res.emplace(s.first, obj);
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::convertToArgs(Args & args, const std::string & category)
|
void Config::convertToArgs(Args & args, const std::string & category)
|
||||||
|
@ -199,9 +202,9 @@ void AbstractSetting::setDefault(const std::string & str)
|
||||||
if (!overriden) set(str);
|
if (!overriden) set(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractSetting::toJSON(JSONPlaceholder & out)
|
nlohmann::json AbstractSetting::toJSON()
|
||||||
{
|
{
|
||||||
out.write(to_string());
|
return to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractSetting::convertToArg(Args & args, const std::string & category)
|
void AbstractSetting::convertToArg(Args & args, const std::string & category)
|
||||||
|
@ -209,9 +212,9 @@ void AbstractSetting::convertToArg(Args & args, const std::string & category)
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void BaseSetting<T>::toJSON(JSONPlaceholder & out)
|
nlohmann::json BaseSetting<T>::toJSON()
|
||||||
{
|
{
|
||||||
out.write(value);
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -292,11 +295,9 @@ template<> std::string BaseSetting<Strings>::to_string() const
|
||||||
return concatStringsSep(" ", value);
|
return concatStringsSep(" ", value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> void BaseSetting<Strings>::toJSON(JSONPlaceholder & out)
|
template<> nlohmann::json BaseSetting<Strings>::toJSON()
|
||||||
{
|
{
|
||||||
JSONList list(out.list());
|
return value;
|
||||||
for (auto & s : value)
|
|
||||||
list.elem(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> void BaseSetting<StringSet>::set(const std::string & str)
|
template<> void BaseSetting<StringSet>::set(const std::string & str)
|
||||||
|
@ -309,11 +310,9 @@ template<> std::string BaseSetting<StringSet>::to_string() const
|
||||||
return concatStringsSep(" ", value);
|
return concatStringsSep(" ", value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> void BaseSetting<StringSet>::toJSON(JSONPlaceholder & out)
|
template<> nlohmann::json BaseSetting<StringSet>::toJSON()
|
||||||
{
|
{
|
||||||
JSONList list(out.list());
|
return value;
|
||||||
for (auto & s : value)
|
|
||||||
list.elem(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template class BaseSetting<int>;
|
template class BaseSetting<int>;
|
||||||
|
@ -360,10 +359,12 @@ void GlobalConfig::resetOverriden()
|
||||||
config->resetOverriden();
|
config->resetOverriden();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalConfig::toJSON(JSONObject & out)
|
nlohmann::json GlobalConfig::toJSON()
|
||||||
{
|
{
|
||||||
|
auto res = nlohmann::json::object();
|
||||||
for (auto & config : *configRegistrations)
|
for (auto & config : *configRegistrations)
|
||||||
config->toJSON(out);
|
res.update(config->toJSON());
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalConfig::convertToArgs(Args & args, const std::string & category)
|
void GlobalConfig::convertToArgs(Args & args, const std::string & category)
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
#include "types.hh"
|
#include "types.hh"
|
||||||
|
|
||||||
|
#include <nlohmann/json_fwd.hpp>
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
@ -42,8 +44,6 @@ namespace nix {
|
||||||
|
|
||||||
class Args;
|
class Args;
|
||||||
class AbstractSetting;
|
class AbstractSetting;
|
||||||
class JSONPlaceholder;
|
|
||||||
class JSONObject;
|
|
||||||
|
|
||||||
class AbstractConfig
|
class AbstractConfig
|
||||||
{
|
{
|
||||||
|
@ -97,7 +97,7 @@ public:
|
||||||
* Outputs all settings to JSON
|
* Outputs all settings to JSON
|
||||||
* - out: JSONObject to write the configuration to
|
* - out: JSONObject to write the configuration to
|
||||||
*/
|
*/
|
||||||
virtual void toJSON(JSONObject & out) = 0;
|
virtual nlohmann::json toJSON() = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts settings to `Args` to be used on the command line interface
|
* Converts settings to `Args` to be used on the command line interface
|
||||||
|
@ -167,7 +167,7 @@ public:
|
||||||
|
|
||||||
void resetOverriden() override;
|
void resetOverriden() override;
|
||||||
|
|
||||||
void toJSON(JSONObject & out) override;
|
nlohmann::json toJSON() override;
|
||||||
|
|
||||||
void convertToArgs(Args & args, const std::string & category) override;
|
void convertToArgs(Args & args, const std::string & category) override;
|
||||||
};
|
};
|
||||||
|
@ -206,7 +206,7 @@ protected:
|
||||||
|
|
||||||
virtual std::string to_string() const = 0;
|
virtual std::string to_string() const = 0;
|
||||||
|
|
||||||
virtual void toJSON(JSONPlaceholder & out);
|
virtual nlohmann::json toJSON();
|
||||||
|
|
||||||
virtual void convertToArg(Args & args, const std::string & category);
|
virtual void convertToArg(Args & args, const std::string & category);
|
||||||
|
|
||||||
|
@ -251,7 +251,7 @@ public:
|
||||||
|
|
||||||
void convertToArg(Args & args, const std::string & category) override;
|
void convertToArg(Args & args, const std::string & category) override;
|
||||||
|
|
||||||
void toJSON(JSONPlaceholder & out) override;
|
nlohmann::json toJSON() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -319,7 +319,7 @@ struct GlobalConfig : public AbstractConfig
|
||||||
|
|
||||||
void resetOverriden() override;
|
void resetOverriden() override;
|
||||||
|
|
||||||
void toJSON(JSONObject & out) override;
|
nlohmann::json toJSON() override;
|
||||||
|
|
||||||
void convertToArgs(Args & args, const std::string & category) override;
|
void convertToArgs(Args & args, const std::string & category) override;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
#include "common-args.hh"
|
#include "common-args.hh"
|
||||||
#include "shared.hh"
|
#include "shared.hh"
|
||||||
#include "store-api.hh"
|
#include "store-api.hh"
|
||||||
#include "json.hh"
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
using namespace nix;
|
using namespace nix;
|
||||||
|
|
||||||
|
@ -19,8 +20,7 @@ struct CmdShowConfig : Command, MixJSON
|
||||||
{
|
{
|
||||||
if (json) {
|
if (json) {
|
||||||
// FIXME: use appropriate JSON types (bool, ints, etc).
|
// FIXME: use appropriate JSON types (bool, ints, etc).
|
||||||
JSONObject jsonObj(std::cout);
|
logger->stdout("%s", globalConfig.toJSON().dump());
|
||||||
globalConfig.toJSON(jsonObj);
|
|
||||||
} else {
|
} else {
|
||||||
std::map<std::string, Config::SettingInfo> settings;
|
std::map<std::string, Config::SettingInfo> settings;
|
||||||
globalConfig.getSettings(settings);
|
globalConfig.getSettings(settings);
|
||||||
|
|
Loading…
Reference in a new issue