-
Notifications
You must be signed in to change notification settings - Fork 139
Simpler config override #4926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Simpler config override #4926
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b10e2ef
miral: introduce OverridesList for live config file-change batching
tarek-y-ismail c095f75
miral: extract INI line-parsing into a shared utility function
tarek-y-ismail fa55f2e
miral: add IniFileWithOverrides live config store
tarek-y-ismail 2f4e421
tests: add tests for IniFileWithOverrides configuration merging
tarek-y-ismail f0370f6
miral: export symbols for OverridesList and IniFileWithOverrides
tarek-y-ismail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright © Canonical Ltd. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 or 3 as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * 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 General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #ifndef MIRAL_INI_FILE_WITH_OVERRIDES_H | ||
| #define MIRAL_INI_FILE_WITH_OVERRIDES_H | ||
|
|
||
| #include <miral/live_config.h> | ||
|
|
||
| #include <memory> | ||
| #include <span> | ||
|
|
||
| namespace miral::live_config | ||
| { | ||
| class OverridesList; | ||
|
|
||
| /// An ini-file-based live configuration store that aggregates values from | ||
| /// multiple files (a base config plus zero or more override files). | ||
| /// | ||
| /// Non-array keys follow last-writer-wins semantics: if the same key appears | ||
| /// in a later (higher-priority) file, it replaces the earlier value. | ||
| /// | ||
| /// Array keys accumulate across files; an empty assignment (\c key=) in a | ||
| /// later file clears all previously accumulated values from earlier files. | ||
| /// | ||
| /// A single call to load() wraps the entire set of files in one transaction, | ||
| /// so all attribute handlers and \c on_done fire exactly once per call. | ||
| /// | ||
| /// \remark Since MirAL 5.8 | ||
| class IniFileWithOverrides : public Store | ||
| { | ||
| public: | ||
| IniFileWithOverrides(); | ||
| ~IniFileWithOverrides() override; | ||
|
|
||
| IniFileWithOverrides(IniFileWithOverrides const&) = delete; | ||
| IniFileWithOverrides& operator=(IniFileWithOverrides const&) = delete; | ||
|
|
||
| void add_int_attribute(Key const& key, std::string_view description, HandleInt handler) override; | ||
| void add_ints_attribute(Key const& key, std::string_view description, HandleInts handler) override; | ||
| void add_bool_attribute(Key const& key, std::string_view description, HandleBool handler) override; | ||
| void add_float_attribute(Key const& key, std::string_view description, HandleFloat handler) override; | ||
| void add_floats_attribute(Key const& key, std::string_view description, HandleFloats handler) override; | ||
| void add_string_attribute(Key const& key, std::string_view description, HandleString handler) override; | ||
| void add_strings_attribute(Key const& key, std::string_view description, HandleStrings handler) override; | ||
|
|
||
| void add_int_attribute(Key const& key, std::string_view description, int preset, HandleInt handler) override; | ||
| void add_ints_attribute( | ||
| Key const& key, std::string_view description, std::span<int const> preset, HandleInts handler) override; | ||
| void add_bool_attribute(Key const& key, std::string_view description, bool preset, HandleBool handler) override; | ||
| void add_float_attribute( | ||
| Key const& key, std::string_view description, float preset, HandleFloat handler) override; | ||
| void add_floats_attribute( | ||
| Key const& key, std::string_view description, std::span<float const> preset, HandleFloats handler) override; | ||
| void add_string_attribute( | ||
| Key const& key, std::string_view description, std::string_view preset, HandleString handler) override; | ||
| void add_strings_attribute( | ||
| Key const& key, | ||
| std::string_view description, | ||
| std::span<std::string const> preset, | ||
| HandleStrings handler) override; | ||
|
|
||
| void on_done(HandleDone handler) override; | ||
|
|
||
| /// Parse a batch of file-system events and dispatch all handlers once. | ||
| /// | ||
| /// \c on_done fires exactly once per call. | ||
| /// | ||
| /// \remark Since MirAL 5.8 | ||
| void load(OverridesList const& changes); | ||
|
|
||
| private: | ||
| class Self; | ||
| std::shared_ptr<Self> self; | ||
| }; | ||
| } | ||
|
|
||
| #endif // MIRAL_INI_FILE_WITH_OVERRIDES_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright © Canonical Ltd. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 or 3 as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * 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 General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #ifndef MIRAL_LIVE_CONFIG_OVERRIDES_LIST_H | ||
| #define MIRAL_LIVE_CONFIG_OVERRIDES_LIST_H | ||
|
|
||
| #include <filesystem> | ||
| #include <functional> | ||
| #include <iosfwd> | ||
| #include <memory> | ||
|
|
||
| namespace miral::live_config | ||
| { | ||
| class OverridesListBuilder; | ||
|
|
||
| /// An ordered list of configuration file overrides to be applied. Each | ||
| /// callback represents a file that should be loaded into the configuration in | ||
| /// the given order (unchanged, fresh, modified, or dropped). | ||
| /// | ||
| /// \remark Since MirAL 5.8 | ||
| class OverridesList | ||
| { | ||
| struct Context; | ||
| friend OverridesListBuilder; | ||
|
|
||
| public: | ||
| /// \pre The context must not be null | ||
| explicit OverridesList(std::unique_ptr<Context> ctx); | ||
|
|
||
| /// Callback type for file-content events (unchanged, moved/new, or modified). | ||
| using Loader = std::move_only_function<void(std::filesystem::path const&, std::istream&)>; | ||
|
|
||
| /// Callback type for file-removal events. | ||
| using Dropped = std::move_only_function<void(std::filesystem::path const&)>; | ||
|
|
||
| /// Iterate all events in push order, invoking the matching callback for each. | ||
| void for_each(Loader unchanged, Loader fresh, Loader modified, Dropped dropped) const; | ||
|
|
||
| ~OverridesList(); | ||
|
|
||
| private: | ||
| std::unique_ptr<Context> ctx; | ||
| }; | ||
| } | ||
|
|
||
| #endif //MIRAL_LIVE_CONFIG_OVERRIDES_LIST_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright © Canonical Ltd. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 or 3 as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * 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 General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "live_config_ini_file_common.h" | ||
|
|
||
| #include <mir/log.h> | ||
| #include <miral/live_config.h> | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace mlc = miral::live_config; | ||
|
|
||
| namespace | ||
| { | ||
| auto trim_start_and_end(std::string_view string) -> std::string_view | ||
| { | ||
| constexpr auto whitespace = " \t\n\r\f\v"; | ||
|
|
||
| auto const first_non_space = string.find_first_not_of(whitespace); | ||
| if (first_non_space == std::string::npos) | ||
| { | ||
| return string.substr(0, 0); | ||
| } | ||
|
|
||
| auto const last_non_space = string.find_last_not_of(whitespace); | ||
| return string.substr(first_non_space, last_non_space - first_non_space + 1); | ||
| } | ||
| } | ||
|
|
||
| auto mlc::parse_ini(std::istream& istream, std::filesystem::path path, std::function<void(Key const&, std::string_view, std::filesystem::path const&)> const &update_key) -> void | ||
| { | ||
| for (std::string line; std::getline(istream, line);) | ||
| { | ||
| auto const line_view = std::string_view{line}; | ||
| if (!line_view.starts_with('#') && line_view.contains("=")) | ||
| try | ||
| { | ||
| auto const eq = line_view.find_first_of("="); | ||
| auto const key = Key{trim_start_and_end(line_view.substr(0, eq))}; | ||
| auto const value = trim_start_and_end(line_view.substr(eq + 1)); | ||
|
|
||
| update_key(key, value, path); | ||
| } | ||
| catch (std::exception const& e) | ||
| { | ||
| mir::log_warning("Error processing '%s': %s", path.c_str(), e.what()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.