Serialize AppConfig directly via Void::Json reflection

Drop the parallel *Json mirror structs and the ~370 LOC of field-by-field
mapping in ConfigLoader and Core. Api::AppConfig is now reflected straight
to/from JSON:

- ConfigLoader::{loadFromFile,saveToFile} use from_json/to_json<AppConfig>
  directly; a small normalize() keeps the yt-dlp concurrency cap that the
  mapping layer used to enforce.
- Core::{getConfigJson,updateConfigJson} likewise reflect directly, which
  also extends them to honor youtube/features/advanced sections that the
  old hand-mapping silently dropped.
- parseQuality/qualityToString removed — enums serialize by name.

On-disk format change (v3, clean cutover): durations are strings
("30000ms", also accepts "30s"/"5m"/"1h"), enums are names, the proxy is a
nested object (or null) instead of flat proxyType/proxyHost fields, and the
admin/custom sections now round-trip. Verified: build green, a
serialize->parse->deserialize round-trip of AppConfig (incl. a SOCKS5
proxy) passes, and config.example.json loads through the reflection path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-23 03:00:58 +03:00
1 parent a9ab0d6b75
commit 51f1651c18
3 files changed
+31 -477

No files matched your search

+6 -106
View File
@@ -854,7 +854,8 @@ namespace Echoion::Core
parsed.error().line, parsed.error().col,
parsed.error().message));
}
auto jsonResult = Void::Json::from_json<AppConfigJson>(*parsed);
// Deserialize the full Api::AppConfig document directly via reflection.
auto jsonResult = Void::Json::from_json<Api::AppConfig>(*parsed);
if (!jsonResult.has_value()) {
return Api::fail(
Api::ErrorCode::ParseError,
@@ -863,116 +864,15 @@ namespace Echoion::Core
jsonResult.error().message));
}
// Convert to AppConfig
Api::AppConfig config;
const auto& json = jsonResult.value();
// Bot config
config.bot.botName = json.bot.botName;
config.bot.commandPrefix = json.bot.commandPrefix;
config.bot.defaultVolume = json.bot.defaultVolume;
config.bot.autoReconnect = json.bot.autoReconnect;
config.bot.reconnectDelay =
std::chrono::milliseconds(json.bot.reconnectDelay);
config.bot.maxReconnectAttempts = json.bot.maxReconnectAttempts;
config.bot.preferredQuality =
ConfigLoader::parseQuality(json.bot.preferredQuality);
config.bot.allowPlaylistImports = json.bot.allowPlaylistImports;
config.bot.maxQueueSize = json.bot.maxQueueSize;
config.bot.trackTimeout =
std::chrono::milliseconds(json.bot.trackTimeout);
// Proxy config
if (json.network.proxyHost.has_value() &&
json.network.proxyPort.has_value()) {
Api::ProxyConfig proxy;
proxy.host = *json.network.proxyHost;
proxy.port = *json.network.proxyPort;
proxy.username = json.network.proxyUsername;
proxy.password = json.network.proxyPassword;
proxy.type = (json.network.proxyType == "SOCKS5")
? Api::ProxyType::SOCKS5
: Api::ProxyType::HTTP;
config.network.proxy = proxy;
}
// Server config
config.teamspeak.address = json.teamspeak.address;
config.teamspeak.port = json.teamspeak.port;
config.teamspeak.serverPassword = json.teamspeak.serverPassword;
config.teamspeak.nickname = json.teamspeak.nickname;
config.teamspeak.defaultChannelId = json.teamspeak.defaultChannelId;
config.teamspeak.channelPassword = json.teamspeak.channelPassword;
config.teamspeak.adminGroups = json.teamspeak.adminGroups;
config.teamspeak.djGroups = json.teamspeak.djGroups;
config.teamspeak.bannedGroups = json.teamspeak.bannedGroups;
// Security config
config.security.requireAuthForAdmin = json.security.requireAuthForAdmin;
config.security.maxLoginAttempts = json.security.maxLoginAttempts;
config.security.lockoutDuration =
std::chrono::milliseconds(json.security.lockoutDuration);
config.security.sessionTimeout =
std::chrono::milliseconds(json.security.sessionTimeout);
config.security.enableAuditLog = json.security.enableAuditLog;
// Update the config
Api::AppConfig config = std::move(jsonResult.value());
ConfigLoader::normalize(config);
return updateConfig(config);
}
Api::Result<std::string> Core::getConfigJson() const
{
// Convert current config to JSON
AppConfigJson json;
// Bot config
json.bot.botName = config_.bot.botName;
json.bot.commandPrefix = config_.bot.commandPrefix;
json.bot.defaultVolume = config_.bot.defaultVolume;
json.bot.autoReconnect = config_.bot.autoReconnect;
json.bot.reconnectDelay =
static_cast<uint32_t>(config_.bot.reconnectDelay.count());
json.bot.maxReconnectAttempts = config_.bot.maxReconnectAttempts;
json.bot.preferredQuality =
ConfigLoader::qualityToString(config_.bot.preferredQuality);
json.bot.allowPlaylistImports = config_.bot.allowPlaylistImports;
json.bot.maxQueueSize = config_.bot.maxQueueSize;
json.bot.trackTimeout =
static_cast<uint32_t>(config_.bot.trackTimeout.count());
// Proxy config
if (config_.network.proxy.has_value()) {
json.network.proxyHost = config_.network.proxy->host;
json.network.proxyPort = config_.network.proxy->port;
json.network.proxyUsername = config_.network.proxy->username;
json.network.proxyPassword = config_.network.proxy->password;
json.network.proxyType =
config_.network.proxy->type == Api::ProxyType::SOCKS5 ? "SOCKS5"
: "HTTP";
}
// Server config
json.teamspeak.address = config_.teamspeak.address;
json.teamspeak.port = config_.teamspeak.port;
json.teamspeak.serverPassword = config_.teamspeak.serverPassword;
json.teamspeak.nickname = config_.teamspeak.nickname;
json.teamspeak.defaultChannelId = config_.teamspeak.defaultChannelId;
json.teamspeak.channelPassword = config_.teamspeak.channelPassword;
json.teamspeak.adminGroups = config_.teamspeak.adminGroups;
json.teamspeak.djGroups = config_.teamspeak.djGroups;
json.teamspeak.bannedGroups = config_.teamspeak.bannedGroups;
// Security config
json.security.requireAuthForAdmin =
config_.security.requireAuthForAdmin;
json.security.maxLoginAttempts = config_.security.maxLoginAttempts;
json.security.lockoutDuration =
static_cast<uint32_t>(config_.security.lockoutDuration.count());
json.security.sessionTimeout =
static_cast<uint32_t>(config_.security.sessionTimeout.count());
json.security.enableAuditLog = config_.security.enableAuditLog;
return Void::Json::serialize(Void::Json::to_json(json),
// Serialize the current config directly via reflection.
return Void::Json::serialize(Void::Json::to_json(config_),
{.pretty = true});
}