diff --git a/src/share/manipulator/manipulators/basic/basic.hpp b/src/share/manipulator/manipulators/basic/basic.hpp index fe58796b5..648657cb3 100644 --- a/src/share/manipulator/manipulators/basic/basic.hpp +++ b/src/share/manipulator/manipulators/basic/basic.hpp @@ -40,7 +40,7 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie if (value.is_object()) { try { to_ = std::vector{ - value.get(), + to_event_definition(value), }; } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what())); @@ -48,7 +48,9 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie } else if (value.is_array()) { try { - to_ = value.get>(); + for (const auto& j : value) { + to_.push_back(to_event_definition(j)); + } } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what())); } @@ -61,7 +63,7 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie if (value.is_object()) { try { to_after_key_up_ = std::vector{ - value.get(), + to_event_definition(value), }; } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what())); @@ -69,7 +71,9 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie } else if (value.is_array()) { try { - to_after_key_up_ = value.get>(); + for (const auto& j : value) { + to_after_key_up_.push_back(to_event_definition(j)); + } } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what())); } @@ -82,7 +86,7 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie if (value.is_object()) { try { to_if_alone_ = std::vector{ - value.get(), + to_event_definition(value), }; } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what())); @@ -90,7 +94,9 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie } else if (value.is_array()) { try { - to_if_alone_ = value.get>(); + for (const auto& j : value) { + to_if_alone_.push_back(to_event_definition(j)); + } } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what())); } diff --git a/src/share/manipulator/manipulators/basic/simultaneous_options.hpp b/src/share/manipulator/manipulators/basic/simultaneous_options.hpp index 076c13c9e..6ca9c4cfd 100644 --- a/src/share/manipulator/manipulators/basic/simultaneous_options.hpp +++ b/src/share/manipulator/manipulators/basic/simultaneous_options.hpp @@ -67,7 +67,7 @@ class simultaneous_options final { if (value.is_object()) { try { to_after_key_up_ = std::vector{ - value.get(), + to_event_definition(value), }; } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what())); @@ -75,7 +75,9 @@ class simultaneous_options final { } else if (value.is_array()) { try { - to_after_key_up_ = value.get>(); + for (const auto& j : value) { + to_after_key_up_.push_back(to_event_definition(j)); + } } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what())); } diff --git a/src/share/manipulator/manipulators/basic/to_delayed_action.hpp b/src/share/manipulator/manipulators/basic/to_delayed_action.hpp index 5da5033b1..e6c5dca01 100644 --- a/src/share/manipulator/manipulators/basic/to_delayed_action.hpp +++ b/src/share/manipulator/manipulators/basic/to_delayed_action.hpp @@ -21,7 +21,7 @@ class to_delayed_action final : public pqrs::dispatcher::extra::dispatcher_clien if (value.is_object()) { try { to_if_invoked_ = std::vector{ - value.get(), + to_event_definition(value), }; } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what())); @@ -29,7 +29,9 @@ class to_delayed_action final : public pqrs::dispatcher::extra::dispatcher_clien } else if (value.is_array()) { try { - to_if_invoked_ = value.get>(); + for (const auto& j : value) { + to_if_invoked_.push_back(to_event_definition(j)); + } } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what())); } @@ -42,7 +44,7 @@ class to_delayed_action final : public pqrs::dispatcher::extra::dispatcher_clien if (value.is_object()) { try { to_if_canceled_ = std::vector{ - value.get(), + to_event_definition(value), }; } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what())); @@ -50,7 +52,9 @@ class to_delayed_action final : public pqrs::dispatcher::extra::dispatcher_clien } else if (value.is_array()) { try { - to_if_canceled_ = value.get>(); + for (const auto& j : value) { + to_if_canceled_.push_back(to_event_definition(j)); + } } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what())); } diff --git a/src/share/manipulator/manipulators/basic/to_if_held_down.hpp b/src/share/manipulator/manipulators/basic/to_if_held_down.hpp index a658d4661..0ebe1f120 100644 --- a/src/share/manipulator/manipulators/basic/to_if_held_down.hpp +++ b/src/share/manipulator/manipulators/basic/to_if_held_down.hpp @@ -17,11 +17,13 @@ class to_if_held_down final : public pqrs::dispatcher::extra::dispatcher_client try { if (json.is_object()) { to_ = std::vector{ - json.get(), + to_event_definition(json), }; } else if (json.is_array()) { - to_ = json.get>(); + for (const auto& j : json) { + to_.push_back(to_event_definition(j)); + } } else { throw pqrs::json::unmarshal_error(fmt::format("json must be object or array, but is `{0}`", pqrs::json::dump_for_error_message(json))); diff --git a/src/share/manipulator/types/to_event_definition.hpp b/src/share/manipulator/types/to_event_definition.hpp index 3b22e22a1..43b6c85d0 100644 --- a/src/share/manipulator/types/to_event_definition.hpp +++ b/src/share/manipulator/types/to_event_definition.hpp @@ -9,10 +9,71 @@ namespace krbn { namespace manipulator { class to_event_definition final { public: - to_event_definition(void) : lazy_(false), - repeat_(true), - halt_(false), - hold_down_milliseconds_(0) { + to_event_definition(void) + : lazy_(false), + repeat_(true), + halt_(false), + hold_down_milliseconds_(0) { + } + + to_event_definition(const nlohmann::json& json) + : to_event_definition() { + pqrs::json::requires_object(json, "json"); + + for (const auto& [key, value] : json.items()) { + if (event_definition_.handle_json(key, value, json)) { + // Do nothing + + } else if (key == "modifiers") { + try { + modifiers_ = modifier_definition::make_modifiers(value); + } catch (const pqrs::json::unmarshal_error& e) { + throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what())); + } + + } else if (key == "lazy") { + pqrs::json::requires_boolean(value, "`" + key + "`"); + + lazy_ = value.get(); + + } else if (key == "repeat") { + pqrs::json::requires_boolean(value, "`" + key + "`"); + + repeat_ = value.get(); + + } else if (key == "halt") { + pqrs::json::requires_boolean(value, "`" + key + "`"); + + halt_ = value.get(); + + } else if (key == "hold_down_milliseconds" || + key == "held_down_milliseconds") { + pqrs::json::requires_number(value, "`" + key + "`"); + + hold_down_milliseconds_ = std::chrono::milliseconds(value.get()); + + } else { + throw pqrs::json::unmarshal_error(fmt::format("unknown key `{0}` in `{1}`", key, pqrs::json::dump_for_error_message(json))); + } + } + + // ---------------------------------------- + + switch (event_definition_.get_type()) { + case event_definition::type::momentary_switch_event: + case event_definition::type::shell_command: + case event_definition::type::select_input_source: + case event_definition::type::set_variable: + case event_definition::type::set_notification_message: + case event_definition::type::mouse_key: + case event_definition::type::sticky_modifier: + case event_definition::type::software_function: + break; + + case event_definition::type::none: + case event_definition::type::any: + throw pqrs::json::unmarshal_error(fmt::format("event type is invalid: `{0}`", pqrs::json::dump_for_error_message(json))); + } } virtual ~to_event_definition(void) { @@ -110,64 +171,5 @@ class to_event_definition final { bool halt_; std::chrono::milliseconds hold_down_milliseconds_; }; - -inline void from_json(const nlohmann::json& json, to_event_definition& d) { - pqrs::json::requires_object(json, "json"); - - for (const auto& [key, value] : json.items()) { - if (d.get_event_definition().handle_json(key, value, json)) { - // Do nothing - - } else if (key == "modifiers") { - try { - d.set_modifiers(modifier_definition::make_modifiers(value)); - } catch (const pqrs::json::unmarshal_error& e) { - throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what())); - } - - } else if (key == "lazy") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - d.set_lazy(value.get()); - - } else if (key == "repeat") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - d.set_repeat(value.get()); - - } else if (key == "halt") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - d.set_halt(value.get()); - - } else if (key == "hold_down_milliseconds" || - key == "held_down_milliseconds") { - pqrs::json::requires_number(value, "`" + key + "`"); - - d.set_hold_down_milliseconds(std::chrono::milliseconds(value.get())); - - } else { - throw pqrs::json::unmarshal_error(fmt::format("unknown key `{0}` in `{1}`", key, pqrs::json::dump_for_error_message(json))); - } - } - - // ---------------------------------------- - - switch (d.get_event_definition().get_type()) { - case event_definition::type::momentary_switch_event: - case event_definition::type::shell_command: - case event_definition::type::select_input_source: - case event_definition::type::set_variable: - case event_definition::type::set_notification_message: - case event_definition::type::mouse_key: - case event_definition::type::sticky_modifier: - case event_definition::type::software_function: - break; - - case event_definition::type::none: - case event_definition::type::any: - throw pqrs::json::unmarshal_error(fmt::format("event type is invalid: `{0}`", pqrs::json::dump_for_error_message(json))); - } -} } // namespace manipulator } // namespace krbn diff --git a/tests/src/manipulator_types/src/errors_test.hpp b/tests/src/manipulator_types/src/errors_test.hpp index 9e9759519..0e1c2b8e2 100644 --- a/tests/src/manipulator_types/src/errors_test.hpp +++ b/tests/src/manipulator_types/src/errors_test.hpp @@ -20,7 +20,7 @@ void handle_json(const nlohmann::json& json) { } else if (c == "modifier_definition") { krbn::manipulator::modifier_definition::make_modifiers(json.at("input")); } else if (c == "to_event_definition") { - json.at("input").get(); + krbn::manipulator::to_event_definition(json.at("input")); } else { expect(false); }