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:
1 parent
a9ab0d6b75
commit
51f1651c18
3 files changed
+31
-477
No files matched your search
+16
-271
@@ -8,14 +8,16 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
// NOTE: The Echoion::Core::*Json mirror structs are plain reflectable
|
||||
// aggregates whose member names are exactly the desired JSON keys, so
|
||||
// Void::Json reflection (Json::to_json / Json::from_json) produces the same
|
||||
// document the former glz::meta specializations did — no annotations needed.
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
void ConfigLoader::normalize(Api::AppConfig& config)
|
||||
{
|
||||
// yt-dlp concurrency is hard-capped regardless of what the file says.
|
||||
config.youtube.ytDlpMaxConcurrent =
|
||||
std::min(config.youtube.ytDlpMaxConcurrent, size_t(8));
|
||||
}
|
||||
|
||||
Api::Result<Api::AppConfig>
|
||||
ConfigLoader::loadFromFile(const std::filesystem::path& path)
|
||||
{
|
||||
@@ -45,132 +47,19 @@ namespace Echoion::Core
|
||||
parsed.error().message));
|
||||
}
|
||||
|
||||
// Tolerant deserialization: unknown JSON fields are ignored and missing
|
||||
// fields keep their default-initialized values (matches the former
|
||||
// glz::opts{error_on_unknown_keys=false, error_on_missing_keys=false}).
|
||||
auto jsonResult = Void::Json::from_json<AppConfigJson>(*parsed);
|
||||
if (!jsonResult) {
|
||||
// Deserialize Api::AppConfig directly via reflection: unknown keys are
|
||||
// ignored and absent fields keep their defaults. Durations are duration
|
||||
// strings ("30000ms"), enums are names, the proxy is a nested object.
|
||||
auto result = Void::Json::from_json<Api::AppConfig>(*parsed);
|
||||
if (!result) {
|
||||
return Api::fail(
|
||||
Api::ErrorCode::ParseError,
|
||||
std::format("Failed to parse configuration: field '{}': {}",
|
||||
jsonResult.error().field,
|
||||
jsonResult.error().message));
|
||||
result.error().field, result.error().message));
|
||||
}
|
||||
AppConfigJson jsonConfig = std::move(*jsonResult);
|
||||
|
||||
// Convert to API config
|
||||
Api::AppConfig config;
|
||||
|
||||
// Bot config
|
||||
config.bot.botName = jsonConfig.bot.botName;
|
||||
config.bot.commandPrefix = jsonConfig.bot.commandPrefix;
|
||||
config.bot.defaultVolume = jsonConfig.bot.defaultVolume;
|
||||
config.bot.maxQueueSize = jsonConfig.bot.maxQueueSize;
|
||||
config.bot.trackTimeout =
|
||||
std::chrono::milliseconds(jsonConfig.bot.trackTimeout);
|
||||
config.bot.autoReconnect = jsonConfig.bot.autoReconnect;
|
||||
config.bot.reconnectDelay =
|
||||
std::chrono::milliseconds(jsonConfig.bot.reconnectDelay);
|
||||
config.bot.maxReconnectAttempts = jsonConfig.bot.maxReconnectAttempts;
|
||||
config.bot.preferredQuality =
|
||||
parseQuality(jsonConfig.bot.preferredQuality);
|
||||
config.bot.allowPlaylistImports = jsonConfig.bot.allowPlaylistImports;
|
||||
|
||||
// YouTube config
|
||||
if (jsonConfig.youtube.youtubeApiKey.has_value()) {
|
||||
config.youtube.youtubeApiKey = jsonConfig.youtube.youtubeApiKey;
|
||||
}
|
||||
if (jsonConfig.youtube.youtubeCookiesPath.has_value()) {
|
||||
config.youtube.youtubeCookiesPath =
|
||||
jsonConfig.youtube.youtubeCookiesPath;
|
||||
}
|
||||
if (jsonConfig.youtube.cookiesFromBrowser.has_value()) {
|
||||
config.youtube.cookiesFromBrowser =
|
||||
jsonConfig.youtube.cookiesFromBrowser;
|
||||
}
|
||||
config.youtube.ytDlpMaxConcurrent =
|
||||
std::min(static_cast<size_t>(jsonConfig.youtube.ytDlpMaxConcurrent),
|
||||
size_t(8));
|
||||
config.youtube.ytDlpAsyncEnabled = jsonConfig.youtube.ytDlpAsyncEnabled;
|
||||
config.youtube.enableUrlShortening =
|
||||
jsonConfig.youtube.enableUrlShortening;
|
||||
|
||||
// Network config
|
||||
if (jsonConfig.network.proxyHost.has_value() &&
|
||||
jsonConfig.network.proxyPort.has_value()) {
|
||||
Api::ProxyConfig proxy;
|
||||
proxy.host = *jsonConfig.network.proxyHost;
|
||||
proxy.port = *jsonConfig.network.proxyPort;
|
||||
proxy.username = jsonConfig.network.proxyUsername;
|
||||
proxy.password = jsonConfig.network.proxyPassword;
|
||||
const auto& typeStr = jsonConfig.network.proxyType;
|
||||
if (typeStr == "SOCKS5" || typeStr == "socks5") {
|
||||
proxy.type = Api::ProxyType::SOCKS5;
|
||||
} else {
|
||||
proxy.type = Api::ProxyType::HTTP;
|
||||
}
|
||||
config.network.proxy = proxy;
|
||||
}
|
||||
config.network.rpcEnabled = jsonConfig.network.rpcEnabled;
|
||||
config.network.rpcPort = jsonConfig.network.rpcPort;
|
||||
config.network.rpcBindAddress = jsonConfig.network.rpcBindAddress;
|
||||
|
||||
// TeamSpeak config
|
||||
config.teamspeak.address = jsonConfig.teamspeak.address;
|
||||
config.teamspeak.port = jsonConfig.teamspeak.port;
|
||||
config.teamspeak.serverPassword = jsonConfig.teamspeak.serverPassword;
|
||||
config.teamspeak.nickname = jsonConfig.teamspeak.nickname;
|
||||
config.teamspeak.defaultChannelId =
|
||||
jsonConfig.teamspeak.defaultChannelId;
|
||||
config.teamspeak.channelPassword = jsonConfig.teamspeak.channelPassword;
|
||||
config.teamspeak.adminGroups = jsonConfig.teamspeak.adminGroups;
|
||||
config.teamspeak.djGroups = jsonConfig.teamspeak.djGroups;
|
||||
config.teamspeak.bannedGroups = jsonConfig.teamspeak.bannedGroups;
|
||||
|
||||
// Security config
|
||||
config.security.requireAuthForAdmin =
|
||||
jsonConfig.security.requireAuthForAdmin;
|
||||
config.security.maxLoginAttempts = jsonConfig.security.maxLoginAttempts;
|
||||
config.security.lockoutDuration =
|
||||
std::chrono::milliseconds(jsonConfig.security.lockoutDuration);
|
||||
config.security.sessionTimeout =
|
||||
std::chrono::milliseconds(jsonConfig.security.sessionTimeout);
|
||||
config.security.enableAuditLog = jsonConfig.security.enableAuditLog;
|
||||
if (jsonConfig.security.apiKey.has_value()) {
|
||||
config.security.apiKey = jsonConfig.security.apiKey;
|
||||
}
|
||||
config.security.allowCors = jsonConfig.security.allowCors;
|
||||
config.security.corsOrigins = jsonConfig.security.corsOrigins;
|
||||
|
||||
// Features config
|
||||
config.features.enableHotReload = jsonConfig.features.enableHotReload;
|
||||
config.features.enableAutoReload = jsonConfig.features.enableAutoReload;
|
||||
config.features.enableAutoUpdateCheck =
|
||||
jsonConfig.features.enableAutoUpdateCheck;
|
||||
config.features.autoStartOnSubscribe =
|
||||
jsonConfig.features.autoStartOnSubscribe;
|
||||
config.features.pauseOnEmptySubscribers =
|
||||
jsonConfig.features.pauseOnEmptySubscribers;
|
||||
config.features.resumeOnSubscriberReturn =
|
||||
jsonConfig.features.resumeOnSubscriberReturn;
|
||||
config.features.shuffleMode = jsonConfig.features.shuffleMode;
|
||||
config.features.repeatMode = jsonConfig.features.repeatMode;
|
||||
config.features.maxHistorySize = jsonConfig.features.maxHistorySize;
|
||||
|
||||
// Advanced config
|
||||
config.advanced.sampleRate = jsonConfig.advanced.sampleRate;
|
||||
config.advanced.channels = jsonConfig.advanced.channels;
|
||||
config.advanced.bufferSize = jsonConfig.advanced.bufferSize;
|
||||
config.advanced.commandThreadPoolSize =
|
||||
jsonConfig.advanced.commandThreadPoolSize;
|
||||
config.advanced.networkThreadPoolSize =
|
||||
jsonConfig.advanced.networkThreadPoolSize;
|
||||
config.advanced.decoderThreads = jsonConfig.advanced.decoderThreads;
|
||||
config.advanced.cacheEnabled = jsonConfig.advanced.cacheEnabled;
|
||||
config.advanced.cacheMaxSize = jsonConfig.advanced.cacheMaxSize;
|
||||
config.advanced.cacheTTL = jsonConfig.advanced.cacheTTL;
|
||||
config.advanced.tempCleanupInterval =
|
||||
jsonConfig.advanced.tempCleanupInterval;
|
||||
Api::AppConfig config = std::move(*result);
|
||||
normalize(config);
|
||||
|
||||
std::cout << std::format("[INFO] Configuration loaded from {}",
|
||||
path.string())
|
||||
@@ -182,114 +71,8 @@ namespace Echoion::Core
|
||||
ConfigLoader::saveToFile(const std::filesystem::path& path,
|
||||
const Api::AppConfig& config)
|
||||
{
|
||||
AppConfigJson jsonConfig;
|
||||
|
||||
// Bot config
|
||||
jsonConfig.bot.botName = config.bot.botName;
|
||||
jsonConfig.bot.commandPrefix = config.bot.commandPrefix;
|
||||
jsonConfig.bot.defaultVolume = config.bot.defaultVolume;
|
||||
jsonConfig.bot.maxQueueSize = config.bot.maxQueueSize;
|
||||
jsonConfig.bot.trackTimeout =
|
||||
static_cast<uint32_t>(config.bot.trackTimeout.count());
|
||||
jsonConfig.bot.autoReconnect = config.bot.autoReconnect;
|
||||
jsonConfig.bot.reconnectDelay =
|
||||
static_cast<uint32_t>(config.bot.reconnectDelay.count());
|
||||
jsonConfig.bot.maxReconnectAttempts = config.bot.maxReconnectAttempts;
|
||||
jsonConfig.bot.preferredQuality =
|
||||
qualityToString(config.bot.preferredQuality);
|
||||
jsonConfig.bot.allowPlaylistImports = config.bot.allowPlaylistImports;
|
||||
|
||||
// YouTube config
|
||||
if (config.youtube.youtubeApiKey.has_value()) {
|
||||
jsonConfig.youtube.youtubeApiKey = *config.youtube.youtubeApiKey;
|
||||
}
|
||||
if (config.youtube.youtubeCookiesPath.has_value()) {
|
||||
jsonConfig.youtube.youtubeCookiesPath =
|
||||
*config.youtube.youtubeCookiesPath;
|
||||
}
|
||||
if (config.youtube.cookiesFromBrowser.has_value()) {
|
||||
jsonConfig.youtube.cookiesFromBrowser =
|
||||
*config.youtube.cookiesFromBrowser;
|
||||
}
|
||||
jsonConfig.youtube.ytDlpMaxConcurrent =
|
||||
static_cast<uint32_t>(config.youtube.ytDlpMaxConcurrent);
|
||||
jsonConfig.youtube.ytDlpAsyncEnabled = config.youtube.ytDlpAsyncEnabled;
|
||||
jsonConfig.youtube.enableUrlShortening =
|
||||
config.youtube.enableUrlShortening;
|
||||
|
||||
// Network config
|
||||
if (config.network.proxy.has_value()) {
|
||||
jsonConfig.network.proxyHost = config.network.proxy->host;
|
||||
jsonConfig.network.proxyPort = config.network.proxy->port;
|
||||
jsonConfig.network.proxyUsername = config.network.proxy->username;
|
||||
jsonConfig.network.proxyPassword = config.network.proxy->password;
|
||||
jsonConfig.network.proxyType =
|
||||
config.network.proxy->type == Api::ProxyType::SOCKS5 ? "SOCKS5"
|
||||
: "HTTP";
|
||||
}
|
||||
jsonConfig.network.rpcEnabled = config.network.rpcEnabled;
|
||||
jsonConfig.network.rpcPort = config.network.rpcPort;
|
||||
jsonConfig.network.rpcBindAddress = config.network.rpcBindAddress;
|
||||
|
||||
// TeamSpeak config
|
||||
jsonConfig.teamspeak.address = config.teamspeak.address;
|
||||
jsonConfig.teamspeak.port = config.teamspeak.port;
|
||||
jsonConfig.teamspeak.serverPassword = config.teamspeak.serverPassword;
|
||||
jsonConfig.teamspeak.nickname = config.teamspeak.nickname;
|
||||
jsonConfig.teamspeak.defaultChannelId =
|
||||
config.teamspeak.defaultChannelId;
|
||||
jsonConfig.teamspeak.channelPassword = config.teamspeak.channelPassword;
|
||||
jsonConfig.teamspeak.adminGroups = config.teamspeak.adminGroups;
|
||||
jsonConfig.teamspeak.djGroups = config.teamspeak.djGroups;
|
||||
jsonConfig.teamspeak.bannedGroups = config.teamspeak.bannedGroups;
|
||||
|
||||
// Security config
|
||||
jsonConfig.security.requireAuthForAdmin =
|
||||
config.security.requireAuthForAdmin;
|
||||
jsonConfig.security.maxLoginAttempts = config.security.maxLoginAttempts;
|
||||
jsonConfig.security.lockoutDuration =
|
||||
static_cast<uint32_t>(config.security.lockoutDuration.count());
|
||||
jsonConfig.security.sessionTimeout =
|
||||
static_cast<uint32_t>(config.security.sessionTimeout.count());
|
||||
jsonConfig.security.enableAuditLog = config.security.enableAuditLog;
|
||||
if (config.security.apiKey.has_value()) {
|
||||
jsonConfig.security.apiKey = *config.security.apiKey;
|
||||
}
|
||||
jsonConfig.security.allowCors = config.security.allowCors;
|
||||
jsonConfig.security.corsOrigins = config.security.corsOrigins;
|
||||
|
||||
// Features config
|
||||
jsonConfig.features.enableHotReload = config.features.enableHotReload;
|
||||
jsonConfig.features.enableAutoReload = config.features.enableAutoReload;
|
||||
jsonConfig.features.enableAutoUpdateCheck =
|
||||
config.features.enableAutoUpdateCheck;
|
||||
jsonConfig.features.autoStartOnSubscribe =
|
||||
config.features.autoStartOnSubscribe;
|
||||
jsonConfig.features.pauseOnEmptySubscribers =
|
||||
config.features.pauseOnEmptySubscribers;
|
||||
jsonConfig.features.resumeOnSubscriberReturn =
|
||||
config.features.resumeOnSubscriberReturn;
|
||||
jsonConfig.features.shuffleMode = config.features.shuffleMode;
|
||||
jsonConfig.features.repeatMode = config.features.repeatMode;
|
||||
jsonConfig.features.maxHistorySize = config.features.maxHistorySize;
|
||||
|
||||
// Advanced config
|
||||
jsonConfig.advanced.sampleRate = config.advanced.sampleRate;
|
||||
jsonConfig.advanced.channels = config.advanced.channels;
|
||||
jsonConfig.advanced.bufferSize = config.advanced.bufferSize;
|
||||
jsonConfig.advanced.commandThreadPoolSize =
|
||||
config.advanced.commandThreadPoolSize;
|
||||
jsonConfig.advanced.networkThreadPoolSize =
|
||||
config.advanced.networkThreadPoolSize;
|
||||
jsonConfig.advanced.decoderThreads = config.advanced.decoderThreads;
|
||||
jsonConfig.advanced.cacheEnabled = config.advanced.cacheEnabled;
|
||||
jsonConfig.advanced.cacheMaxSize = config.advanced.cacheMaxSize;
|
||||
jsonConfig.advanced.cacheTTL = config.advanced.cacheTTL;
|
||||
jsonConfig.advanced.tempCleanupInterval =
|
||||
config.advanced.tempCleanupInterval;
|
||||
|
||||
const std::string json = Void::Json::serialize(
|
||||
Void::Json::to_json(jsonConfig), {.pretty = true});
|
||||
Void::Json::to_json(config), {.pretty = true});
|
||||
std::ofstream file(path);
|
||||
if (!file.is_open()) {
|
||||
return Api::fail(Api::ErrorCode::PermissionDenied,
|
||||
@@ -389,42 +172,4 @@ namespace Echoion::Core
|
||||
return config;
|
||||
}
|
||||
|
||||
Api::AudioQuality ConfigLoader::parseQuality(const std::string& str)
|
||||
{
|
||||
if (str == "Low") {
|
||||
return Api::AudioQuality::Low;
|
||||
}
|
||||
if (str == "Medium") {
|
||||
return Api::AudioQuality::Medium;
|
||||
}
|
||||
if (str == "High") {
|
||||
return Api::AudioQuality::High;
|
||||
}
|
||||
if (str == "VeryHigh") {
|
||||
return Api::AudioQuality::VeryHigh;
|
||||
}
|
||||
if (str == "Lossless") {
|
||||
return Api::AudioQuality::Lossless;
|
||||
}
|
||||
return Api::AudioQuality::High; // Default
|
||||
}
|
||||
|
||||
std::string ConfigLoader::qualityToString(Api::AudioQuality quality)
|
||||
{
|
||||
switch (quality) {
|
||||
case Api::AudioQuality::Low:
|
||||
return "Low";
|
||||
case Api::AudioQuality::Medium:
|
||||
return "Medium";
|
||||
case Api::AudioQuality::High:
|
||||
return "High";
|
||||
case Api::AudioQuality::VeryHigh:
|
||||
return "VeryHigh";
|
||||
case Api::AudioQuality::Lossless:
|
||||
return "Lossless";
|
||||
default:
|
||||
return "High";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user