Skip to content

Commit

Permalink
Changed to omit core_configuration::global if it has the default value
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jun 10, 2024
1 parent e670267 commit 0e1b905
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace krbn {
namespace core_configuration {
namespace configuration_json_helper {

class value_base {
class base_t {
public:
virtual ~value_base() = default;
virtual ~base_t() = default;
virtual void update_value(const nlohmann::json& json) = 0;
virtual void update_json(nlohmann::json& json) const = 0;
};

template <typename T>
class value_t final : public value_base {
class value_t final : public base_t {
public:
value_t(const std::string& key,
T& value,
Expand Down Expand Up @@ -44,6 +44,35 @@ class value_t final : public value_base {
const T& default_value_;
};

template <typename T>
class object_t final : public base_t {
public:
object_t(const std::string& key,
std::unique_ptr<T>& value)
: key_(key),
value_(value) {
}

void update_value(const nlohmann::json& json) override {
if (auto v = pqrs::json::find_object(json, key_)) {
value_ = std::make_unique<T>(v->value());
}
}

void update_json(nlohmann::json& json) const override {
auto j = value_->to_json();
if (!j.empty()) {
json[key_] = j;
} else {
json.erase(key_);
}
}

private:
std::string key_;
std::unique_ptr<T>& value_;
};

} // namespace configuration_json_helper
} // namespace core_configuration
} // namespace krbn
16 changes: 11 additions & 5 deletions src/share/core_configuration/core_configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ class core_configuration final {

core_configuration(const std::string& file_path,
uid_t expected_file_owner)
: loaded_(false),
: json_(nlohmann::json::object()),
loaded_(false),
global_configuration_(std::make_unique<details::global_configuration>(nlohmann::json::object())),
machine_specific_(nlohmann::json::object()) {
helper_values_.push_back(std::make_unique<configuration_json_helper::object_t<details::global_configuration>>("global",
global_configuration_));

bool valid_file_owner = false;

// Load karabiner.json only when the owner is root or current session user.
Expand All @@ -58,8 +62,8 @@ class core_configuration final {
try {
json_ = json_utility::parse_jsonc(input);

if (auto v = pqrs::json::find_object(json_, "global")) {
global_configuration_ = std::make_unique<details::global_configuration>(v->value());
for (const auto& v : helper_values_) {
v->update_value(json_);
}

if (auto v = pqrs::json::find_object(json_, "machine_specific")) {
Expand All @@ -76,7 +80,6 @@ class core_configuration final {

} catch (std::exception& e) {
logger::get_logger()->error("parse error in {0}: {1}", file_path, e.what());
json_ = nlohmann::json::object();
}
} else {
logger::get_logger()->error("failed to open {0}", file_path);
Expand All @@ -96,7 +99,9 @@ class core_configuration final {
nlohmann::json to_json(void) const {
auto json = json_;

json["global"] = global_configuration_->to_json();
for (const auto& v : helper_values_) {
v->update_json(json);
}

{
auto j = machine_specific_.to_json();
Expand Down Expand Up @@ -264,6 +269,7 @@ class core_configuration final {
std::unique_ptr<details::global_configuration> global_configuration_;
details::machine_specific machine_specific_;
std::vector<details::profile> profiles_;
std::vector<std::unique_ptr<configuration_json_helper::base_t>> helper_values_;
};
} // namespace core_configuration
} // namespace krbn
43 changes: 20 additions & 23 deletions src/share/core_configuration/details/global_configuration.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "../types.hpp"
#include "../configuration_json_helper.hpp"
#include <pqrs/json.hpp>

namespace krbn {
Expand All @@ -10,45 +10,45 @@ class global_configuration final {
public:
static constexpr bool check_for_updates_on_startup_default_value = true;
static constexpr bool show_in_menu_bar_default_value = true;
static constexpr bool show_profile_name_in_menu_bar_default_value = false;
static constexpr bool enable_notification_window_default_value = true;
static constexpr bool ask_for_confirmation_before_quitting_default_value = true;
static constexpr bool unsafe_ui_default_value = false;

global_configuration(const global_configuration&) = delete;

global_configuration(const nlohmann::json& json)
: json_(json),
check_for_updates_on_startup_(check_for_updates_on_startup_default_value),
show_in_menu_bar_(show_in_menu_bar_default_value),
show_profile_name_in_menu_bar_(false),
show_profile_name_in_menu_bar_(show_profile_name_in_menu_bar_default_value),
enable_notification_window_(enable_notification_window_default_value),
ask_for_confirmation_before_quitting(true),
unsafe_ui_(false) {
ask_for_confirmation_before_quitting_(ask_for_confirmation_before_quitting_default_value),
unsafe_ui_(unsafe_ui_default_value) {
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("check_for_updates_on_startup",
check_for_updates_on_startup_,
check_for_updates_on_startup_default_value));
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("show_in_menu_bar",
show_in_menu_bar_,
show_in_menu_bar_default_value));
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("show_profile_name_in_menu_bar",
show_profile_name_in_menu_bar_,
show_profile_name_in_menu_bar_default_value));
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("enable_notification_window",
enable_notification_window_,
enable_notification_window_default_value));
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("ask_for_confirmation_before_quitting",
ask_for_confirmation_before_quitting_,
ask_for_confirmation_before_quitting_default_value));
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("unsafe_ui",
unsafe_ui_,
unsafe_ui_default_value));

pqrs::json::requires_object(json, "json");

for (const auto& v : helper_values_) {
v->update_value(json);
}

if (auto v = pqrs::json::find<bool>(json, "show_profile_name_in_menu_bar")) {
show_profile_name_in_menu_bar_ = *v;
}

if (auto v = pqrs::json::find<bool>(json, "ask_for_confirmation_before_quitting")) {
ask_for_confirmation_before_quitting = *v;
}

if (auto v = pqrs::json::find<bool>(json, "unsafe_ui")) {
unsafe_ui_ = *v;
}
}

nlohmann::json to_json(void) const {
Expand All @@ -58,9 +58,6 @@ class global_configuration final {
v->update_json(j);
}

j["show_profile_name_in_menu_bar"] = show_profile_name_in_menu_bar_;
j["ask_for_confirmation_before_quitting"] = ask_for_confirmation_before_quitting;
j["unsafe_ui"] = unsafe_ui_;
return j;
}

Expand Down Expand Up @@ -93,10 +90,10 @@ class global_configuration final {
}

bool get_ask_for_confirmation_before_quitting(void) const {
return ask_for_confirmation_before_quitting;
return ask_for_confirmation_before_quitting_;
}
void set_ask_for_confirmation_before_quitting(bool value) {
ask_for_confirmation_before_quitting = value;
ask_for_confirmation_before_quitting_ = value;
}

bool get_unsafe_ui(void) const {
Expand All @@ -112,9 +109,9 @@ class global_configuration final {
bool show_in_menu_bar_;
bool show_profile_name_in_menu_bar_;
bool enable_notification_window_;
bool ask_for_confirmation_before_quitting;
bool ask_for_confirmation_before_quitting_;
bool unsafe_ui_;
std::vector<std::unique_ptr<configuration_json_helper::value_base>> helper_values_;
std::vector<std::unique_ptr<configuration_json_helper::base_t>> helper_values_;
};

inline void to_json(nlohmann::json& json, const global_configuration& global_configuration) {
Expand Down
5 changes: 0 additions & 5 deletions tests/src/core_configuration/json/to_json_default.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
{
"global": {
"show_profile_name_in_menu_bar": false,
"ask_for_confirmation_before_quitting": true,
"unsafe_ui": false
},
"profiles": [
{
"complex_modifications": {
Expand Down
1 change: 0 additions & 1 deletion tests/src/core_configuration/json/to_json_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"global": {
"check_for_updates_on_startup": false,
"show_in_menu_bar": false,
"show_profile_name_in_menu_bar": false,
"ask_for_confirmation_before_quitting": false,
"unsafe_ui": true
},
Expand Down
6 changes: 1 addition & 5 deletions tests/src/core_configuration/src/core_configuration_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,7 @@ void run_core_configuration_test(void) {
{
auto json = nlohmann::json::object();
krbn::core_configuration::details::global_configuration global_configuration(json);
nlohmann::json expected({
{"show_profile_name_in_menu_bar", false},
{"ask_for_confirmation_before_quitting", true},
{"unsafe_ui", false},
});
nlohmann::json expected({});
expect(global_configuration.to_json() == expected);

auto actual = global_configuration.to_json();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ void run_global_configuration_test(void) {

global_configuration.set_check_for_updates_on_startup(true);
global_configuration.set_show_in_menu_bar(true);
global_configuration.set_show_profile_name_in_menu_bar(false);
global_configuration.set_enable_notification_window(true);
global_configuration.set_ask_for_confirmation_before_quitting(true);
global_configuration.set_unsafe_ui(false);
nlohmann::json j(global_configuration);
expect(!j.contains("check_for_updates_on_startup"));
expect(!j.contains("show_in_menu_bar"));
expect(!j.contains("enable_notification_window"));
expect(j.empty());
}

// invalid values in json
Expand Down

0 comments on commit 0e1b905

Please sign in to comment.