Skip to content

Commit

Permalink
Add core_configuration::error_handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jun 12, 2024
1 parent 212b77d commit e3fab29
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 240 deletions.
55 changes: 42 additions & 13 deletions src/share/core_configuration/configuration_json_helper.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "logger.hpp"
#include "types.hpp"
#include <gsl/gsl>
#include <pqrs/json.hpp>

Expand All @@ -11,7 +13,8 @@ class base_t {
public:
virtual ~base_t() = default;
virtual const std::string& get_key(void) const = 0;
virtual void update_value(const nlohmann::json& json) = 0;
virtual void update_value(const nlohmann::json& json,
error_handling error_handling) = 0;
virtual void update_json(nlohmann::json& json) const = 0;
};

Expand All @@ -35,9 +38,27 @@ class value_t final : public base_t {
return value_;
}

void update_value(const nlohmann::json& json) override {
if (auto v = pqrs::json::find<T>(json, key_)) {
value_ = *v;
void update_value(const nlohmann::json& json,
error_handling error_handling) override {
pqrs::json::requires_object(json, "json");

auto it = json.find(key_);
if (it == std::end(json)) {
return;
}

try {
if constexpr (std::is_same<T, bool>::value) {
pqrs::json::requires_boolean(*it, "`" + key_ + "`");
} else if constexpr (std::is_same<T, int>::value) {
pqrs::json::requires_number(*it, "`" + key_ + "`");
}

value_ = it->template get<T>();
} catch (...) {
if (error_handling == error_handling::strict) {
throw;
}
}
}

Expand Down Expand Up @@ -68,9 +89,11 @@ class object_t final : public base_t {
return key_;
}

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

Expand Down Expand Up @@ -101,16 +124,21 @@ class array_t final : public base_t {
return key_;
}

void update_value(const nlohmann::json& json) override {
if (!json.contains(key_)) {
void update_value(const nlohmann::json& json,
error_handling error_handling) override {
pqrs::json::requires_object(json, "json");

auto it = json.find(key_);
if (it == std::end(json)) {
return;
}

pqrs::json::requires_array(json[key_], "`" + key_ + "`");
pqrs::json::requires_array(*it, "`" + key_ + "`");

for (const auto& j : json[key_]) {
for (const auto& j : *it) {
try {
value_.push_back(std::make_shared<T>(j));
value_.push_back(std::make_shared<T>(j,
error_handling));
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key_, e.what()));
}
Expand Down Expand Up @@ -164,9 +192,10 @@ class helper_values final {
value));
}

void update_value(const nlohmann::json& json) {
void update_value(const nlohmann::json& json,
error_handling error_handling) {
for (const auto& v : values_) {
v->update_value(json);
v->update_value(json, error_handling);
}
}

Expand Down
32 changes: 21 additions & 11 deletions src/share/core_configuration/core_configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ class core_configuration final {
public:
core_configuration(const core_configuration&) = delete;

core_configuration(void) : core_configuration("", 0) {
core_configuration(error_handling error_handling)
: core_configuration("", 0, error_handling) {
}

core_configuration(const std::string& file_path,
uid_t expected_file_owner)
uid_t expected_file_owner,
error_handling error_handling)
: json_(nlohmann::json::object()),
error_handling_(error_handling),
loaded_(false),
global_configuration_(std::make_shared<details::global_configuration>(nlohmann::json::object())),
machine_specific_(std::make_shared<details::machine_specific>(nlohmann::json::object())) {
global_configuration_(std::make_shared<details::global_configuration>(nlohmann::json::object(),
error_handling)),
machine_specific_(std::make_shared<details::machine_specific>(nlohmann::json::object(),
error_handling)) {
helper_values_.push_back_object<details::global_configuration>("global",
global_configuration_);
helper_values_.push_back_object<details::machine_specific>("machine_specific",
Expand Down Expand Up @@ -69,7 +74,8 @@ class core_configuration final {
try {
json_ = json_utility::parse_jsonc(input);

helper_values_.update_value(json_);
helper_values_.update_value(json_,
error_handling);

loaded_ = true;

Expand All @@ -85,9 +91,10 @@ class core_configuration final {
// Fallbacks
if (profiles_.empty()) {
profiles_.push_back(std::make_shared<details::profile>(nlohmann::json({
{"name", "Default profile"},
{"selected", true},
})));
{"name", "Default profile"},
{"selected", true},
}),
error_handling_));
}
}

Expand Down Expand Up @@ -139,12 +146,14 @@ class core_configuration final {

void push_back_profile(void) {
profiles_.push_back(std::make_shared<details::profile>(nlohmann::json({
{"name", "New profile"},
})));
{"name", "New profile"},
}),
error_handling_));
}

void duplicate_profile(const details::profile& profile) {
profiles_.push_back(std::make_shared<details::profile>(nlohmann::json(profile)));
profiles_.push_back(std::make_shared<details::profile>(nlohmann::json(profile),
error_handling_));

auto& p = *(profiles_.back());
p.set_name(p.get_name() + " (copy)");
Expand Down Expand Up @@ -247,6 +256,7 @@ class core_configuration final {
}

nlohmann::json json_;
error_handling error_handling_;
bool loaded_;

gsl::not_null<std::shared_ptr<details::global_configuration>> global_configuration_;
Expand Down
5 changes: 3 additions & 2 deletions src/share/core_configuration/details/global_configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class global_configuration final {
public:
global_configuration(const global_configuration&) = delete;

global_configuration(const nlohmann::json& json)
global_configuration(const nlohmann::json& json,
error_handling error_handling)
: json_(json) {
helper_values_.push_back_value<bool>("check_for_updates_on_startup",
check_for_updates_on_startup_,
Expand All @@ -33,7 +34,7 @@ class global_configuration final {

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

helper_values_.update_value(json);
helper_values_.update_value(json, error_handling);
}

nlohmann::json to_json(void) const {
Expand Down
18 changes: 12 additions & 6 deletions src/share/core_configuration/details/machine_specific.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ class machine_specific final {
public:
entry(const entry&) = delete;

entry(const nlohmann::json& json)
entry(const nlohmann::json& json,
error_handling error_handling)
: json_(json) {
helper_values_.push_back_value<bool>("enable_multitouch_extension",
enable_multitouch_extension_,
false);

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

helper_values_.update_value(json);
helper_values_.update_value(json, error_handling);
}

nlohmann::json to_json(void) const {
Expand All @@ -47,13 +48,16 @@ class machine_specific final {

machine_specific(const machine_specific&) = delete;

machine_specific(const nlohmann::json& json)
: json_(json) {
machine_specific(const nlohmann::json& json,
error_handling error_handling)
: json_(json),
error_handling_(error_handling) {
pqrs::json::requires_object(json, "json");

for (const auto& [key, value] : json.items()) {
if (value.is_object()) {
entries_[karabiner_machine_identifier(key)] = std::make_shared<entry>(value);
entries_[karabiner_machine_identifier(key)] = std::make_shared<entry>(value,
error_handling_);
}
}
}
Expand All @@ -75,14 +79,16 @@ class machine_specific final {

entry& get_entry(const karabiner_machine_identifier& identifier = constants::get_karabiner_machine_identifier()) {
if (!entries_.contains(identifier)) {
entries_[identifier] = std::make_shared<entry>(nlohmann::json::object());
entries_[identifier] = std::make_shared<entry>(nlohmann::json::object(),
error_handling_);
}

return *(entries_[identifier]);
}

private:
nlohmann::json json_;
error_handling error_handling_;
std::unordered_map<karabiner_machine_identifier, std::shared_ptr<entry>> entries_;
};

Expand Down
27 changes: 17 additions & 10 deletions src/share/core_configuration/details/profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class profile final {
public:
profile(const profile&) = delete;

profile(const nlohmann::json& json)
profile(const nlohmann::json& json,
error_handling error_handling)
: json_(json),
error_handling_(error_handling),
selected_(false) {
helper_values_.push_back_array<details::device>("devices",
devices_);
Expand All @@ -33,7 +35,7 @@ class profile final {

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

helper_values_.update_value(json);
helper_values_.update_value(json, error_handling);

for (const auto& [key, value] : json.items()) {
if (key == "name") {
Expand Down Expand Up @@ -268,8 +270,9 @@ class profile final {
}

details::device d(nlohmann::json({
{"identifiers", identifiers},
}));
{"identifiers", identifiers},
}),
error_handling_);
return d.get_ignore();
}

Expand All @@ -293,8 +296,9 @@ class profile final {
}

details::device d(nlohmann::json({
{"identifiers", identifiers},
}));
{"identifiers", identifiers},
}),
error_handling_);
return d.get_manipulate_caps_lock_led();
}

Expand All @@ -318,8 +322,9 @@ class profile final {
}

details::device d(nlohmann::json({
{"identifiers", identifiers},
}));
{"identifiers", identifiers},
}),
error_handling_);
return d.get_treat_as_built_in_keyboard();
}

Expand Down Expand Up @@ -976,11 +981,13 @@ class profile final {
}

devices_.push_back(std::make_shared<details::device>(nlohmann::json({
{"identifiers", identifiers},
})));
{"identifiers", identifiers},
}),
error_handling_));
}

nlohmann::json json_;
error_handling error_handling_;
std::string name_;
bool selected_;
details::parameters parameters_;
Expand Down
Loading

0 comments on commit e3fab29

Please sign in to comment.