Skip to content

Commit

Permalink
Fix deserialization of config with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 21, 2024
1 parent a97cbff commit cef74b9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
89 changes: 89 additions & 0 deletions nano/core_test/toml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,3 +917,92 @@ TEST (toml, tls_config_defaults)
ASSERT_EQ (conf.server_key_passphrase, defaults.server_key_passphrase);
ASSERT_EQ (conf.server_dh_path, defaults.server_dh_path);
}

TEST (toml, log_config_defaults)
{
std::stringstream ss;

// A config with no values
ss << R"toml()toml";

nano::tomlconfig toml;
toml.read (ss);
nano::log_config confg{};
nano::log_config defaults{};
confg.deserialize_toml (toml);

ASSERT_FALSE (toml.get_error ()) << toml.get_error ().get_message ();

ASSERT_EQ (confg.default_level, defaults.default_level);
ASSERT_EQ (confg.flush_level, defaults.flush_level);
ASSERT_EQ (confg.levels, defaults.levels);
ASSERT_EQ (confg.console.enable, defaults.console.enable);
ASSERT_EQ (confg.console.colors, defaults.console.colors);
ASSERT_EQ (confg.console.to_cerr, defaults.console.to_cerr);
ASSERT_EQ (confg.file.enable, defaults.file.enable);
ASSERT_EQ (confg.file.max_size, defaults.file.max_size);
ASSERT_EQ (confg.file.rotation_count, defaults.file.rotation_count);
}

TEST (toml, log_config_no_defaults)
{
std::stringstream ss;

// A config file with values that differs from defaults
ss << R"toml(
[log]
default_level = "trace"
[log.console]
colors = false
enable = false
to_cerr = true
[log.file]
enable = false
max_size = 999
rotation_count = 999
[log.levels]
active_transactions = "trace"
blockprocessor = "trace"
)toml";

nano::tomlconfig toml;
toml.read (ss);
nano::log_config confg{};
nano::log_config defaults{};
confg.deserialize_toml (toml);

ASSERT_FALSE (toml.get_error ()) << toml.get_error ().get_message ();

ASSERT_NE (confg.default_level, defaults.default_level);
ASSERT_NE (confg.levels, defaults.levels);
ASSERT_NE (confg.console.enable, defaults.console.enable);
ASSERT_NE (confg.console.colors, defaults.console.colors);
ASSERT_NE (confg.console.to_cerr, defaults.console.to_cerr);
ASSERT_NE (confg.file.enable, defaults.file.enable);
ASSERT_NE (confg.file.max_size, defaults.file.max_size);
ASSERT_NE (confg.file.rotation_count, defaults.file.rotation_count);
}

TEST (toml, log_config_no_required)
{
std::stringstream ss;

// A config with no values, only categories
ss << R"toml(
[log]
[log.console]
[log.file]
[log.levels]
)toml";

nano::tomlconfig toml;
toml.read (ss);
nano::log_config confg{};
nano::log_config defaults{};
confg.deserialize_toml (toml);

ASSERT_FALSE (toml.get_error ()) << toml.get_error ().get_message ();
}
12 changes: 6 additions & 6 deletions nano/lib/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,17 @@ void nano::log_config::deserialize (nano::tomlconfig & toml)
if (toml.has_key ("console"))
{
auto console_config = toml.get_required_child ("console");
console.enable = console_config.get<bool> ("enable");
console.to_cerr = console_config.get<bool> ("to_cerr");
console.colors = console_config.get<bool> ("colors");
console_config.get ("enable", console.enable);
console_config.get ("to_cerr", console.to_cerr);
console_config.get ("colors", console.colors);
}

if (toml.has_key ("file"))
{
auto file_config = toml.get_required_child ("file");
file.enable = file_config.get<bool> ("enable");
file.max_size = file_config.get<std::size_t> ("max_size");
file.rotation_count = file_config.get<std::size_t> ("rotation_count");
file_config.get ("enable", file.enable);
file_config.get ("max_size", file.max_size);
file_config.get ("rotation_count", file.rotation_count);
}

if (toml.has_key ("levels"))
Expand Down

0 comments on commit cef74b9

Please sign in to comment.