Work around yaml's greedy assignment

FossilOrigin-Name: 2efd8985a336d99de5381702ef0f94f1af272c25c04178006fbf67699c559769
This commit is contained in:
nekobit 2022-10-10 04:39:06 +00:00
parent cd3d5cf2b2
commit d72d15b888
10 changed files with 28 additions and 92 deletions

View file

@ -18,7 +18,6 @@ add_executable(wormhole src/main.cpp
src/config/config_http.cpp
src/config/config_db.cpp
src/config/config_loader.cpp
src/config/config_helpers.cpp
src/config/config_instance.cpp
src/logger.cpp)

View file

@ -17,7 +17,6 @@
*/
#include "config/config_db.h"
#include "config/config_helpers.h"
// Decl
namespace
@ -26,28 +25,25 @@ namespace
}
// Calls other (local) database functions if the database block exists
void Config::load_database(Config::Database& cfg, YAML::Node node)
{
if (!Config::enter_block(node, "database"))
return;
cfg.name = node["db_name"].as<std::string>();
void Config::load_database(Config::Database& cfg, YAML::Node& node)
try {
cfg.name = node["database"]["db_name"].as<std::string>();
if (!load_database_sqlite(cfg, node))
return;
load_database_sqlite(cfg, node);
}
catch(const YAML::BadConversion& err)
{}
namespace
{
bool load_database_sqlite(Config::Database& cfg, YAML::Node& node)
{
// If database block doesn't exist, give up anyway
if (node["db_type"].as<std::string>() != "sqlite" &&
!Config::enter_block(node, "sqlite"))
if (node["database"]["db_type"].as<std::string>() != "sqlite")
return false;
cfg.type = DB::Database::Type::SQLite;
cfg.file = node["db_file"].as<std::string>();
cfg.file = node["database"]["sqlite"]["db_file"].as<std::string>();
return true;
}
}

View file

@ -33,6 +33,6 @@ namespace Config
DB::Database::Type type{DB::Database::Type::Unset};
};
void load_database(Config::Database& cfg, YAML::Node node);
void load_database(Config::Database& cfg, YAML::Node& node);
}

View file

@ -1,29 +0,0 @@
/*
* Wormhole - Federated social network
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config_helpers.h"
bool Config::enter_block(YAML::Node& node, const std::string& block)
{
if (node[block])
{
node = node[block];
return true;
}
return false;
}

View file

@ -1,32 +0,0 @@
/*
* Wormhole - Federated social network
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include "yaml-cpp/yaml.h"
namespace Config
{
/**
* @brief Enters a yaml block by move the node forward.
* @param node Parent node to move forward if block exists.
* @return True unless the block couldn't enter (didn't exist)
*/
bool enter_block(YAML::Node& node, const std::string& block);
}

View file

@ -17,13 +17,12 @@
*/
#include "config_http.h"
#include "config_helpers.h"
#include <yaml-cpp/exceptions.h>
void Config::load_http(Config::HTTP& cfg, YAML::Node node)
{
if (!Config::enter_block(node, "http")) return;
if (node["port"])
cfg.port = node["port"].as<unsigned>();
void Config::load_http(Config::HTTP& cfg, YAML::Node& node)
try {
cfg.port = node["http"]["port"].as<unsigned>();
}
catch(const YAML::BadConversion& err)
{}

View file

@ -18,6 +18,7 @@
#pragma once
#include <string>
#include <yaml-cpp/yaml.h>
#include <cstdint>
@ -28,6 +29,6 @@ namespace Config
uint16_t port{8080};
};
void load_http(Config::HTTP& cfg, YAML::Node node);
void load_http(Config::HTTP& cfg, YAML::Node& node);
}

View file

@ -17,13 +17,10 @@
*/
#include "config_instance.h"
#include "config_helpers.h"
void Config::load_instance(Config::Instance& cfg, YAML::Node node)
{
if (!Config::enter_block(node, "instance")) return;
if (node["host"])
cfg.host = node["host"].as<std::string>();
void Config::load_instance(Config::Instance& cfg, YAML::Node& node)
try {
cfg.host = node["instance"]["host"].as<std::string>();
}
catch(const YAML::BadConversion& err)
{}

View file

@ -28,5 +28,5 @@ namespace Config
std::string host{"localhost"};
};
void load_instance(Config::Instance& cfg, YAML::Node node);
void load_instance(Config::Instance& cfg, YAML::Node& node);
}

View file

@ -51,3 +51,8 @@ void ConfigLoader::load_config(const std::filesystem::path& path)
Logger::instance().log("Couldn't load config: \""s + path.string() + "\"", Logger::Level::ERROR);
throw err;
}
catch (const YAML::BadConversion& err)
{
// NOOP
}