-
Notifications
You must be signed in to change notification settings - Fork 77
refactor: Clio Config #1544
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
refactor: Clio Config #1544
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cfbd215
implementation of new config defintion + methods + ut
PeterChen13579 21092d0
Fix Doxygen
PeterChen13579 80bdbf3
add last doxygen
PeterChen13579 d000f58
fix minor assert error
PeterChen13579 ce51e85
fix minor codecov
PeterChen13579 a869254
apply feedback
PeterChen13579 639bfb9
fix doxygen
PeterChen13579 f575927
fix up fetching values
PeterChen13579 5965ece
fix comments
PeterChen13579 5b9f8d1
Fix Doxygen
PeterChen13579 5fac0d4
fix include expected
PeterChen13579 6ba7885
fix comments + more tests
PeterChen13579 444ddf6
add more tests not covered in codecov
PeterChen13579 80e6604
fix comments
PeterChen13579 7e62747
add last file
PeterChen13579 39b2ed2
fix comments
PeterChen13579 8dfef16
Fix comment
PeterChen13579 c64979e
Merge branch 'develop' into NewConfigDef
kuznetsss 632f7c5
fix comments
PeterChen13579 fe84a9c
Merge branch 'develop' into NewConfigDef
PeterChen13579 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
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,191 @@ | ||
| //------------------------------------------------------------------------------ | ||
| /* | ||
| This file is part of clio: https://github.com/XRPLF/clio | ||
| Copyright (c) 2024, the clio developers. | ||
| Permission to use, copy, modify, and distribute this software for any | ||
| purpose with or without fee is hereby granted, provided that the above | ||
| copyright notice and this permission notice appear in all copies. | ||
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| */ | ||
| //============================================================================== | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "util/Assert.hpp" | ||
| #include "util/newconfig/ConfigValue.hpp" | ||
| #include "util/newconfig/ValueView.hpp" | ||
|
|
||
| #include <cstddef> | ||
| #include <iterator> | ||
| #include <vector> | ||
|
|
||
| namespace util::config { | ||
|
|
||
| /** | ||
| * @brief Array definition for Json/Yaml config | ||
| * | ||
| * Used in ClioConfigDefinition to represent multiple potential values (like whitelist) | ||
| */ | ||
| class Array { | ||
| public: | ||
| /** @brief Custom iterator class which returns ValueView of what's underneath the Array | ||
| */ | ||
| struct ArrayIterator { | ||
| using iterator_category = std::forward_iterator_tag; | ||
| using pointer = ConfigValue const*; | ||
| using reference = ConfigValue const&; | ||
| using valueType = ConfigValue; | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @brief Constructs an ArrayIterator with a pointer to the ConfigValue | ||
| * | ||
| * @param ptr Pointer to the ConfigValue | ||
| */ | ||
| ArrayIterator(pointer ptr) : m_ptr(ptr) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @brief Prefix increment operator | ||
| * | ||
| * @return Reference to the incremented ArrayIterator | ||
| */ | ||
| ArrayIterator& | ||
| operator++() | ||
| { | ||
| m_ptr++; | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return *this; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Postfix increment operator | ||
| * | ||
| * @return Copy of the ArrayIterator before increment | ||
| */ | ||
| ArrayIterator | ||
| operator++(int) | ||
| { | ||
| ArrayIterator temp = *this; | ||
| m_ptr++; | ||
| return temp; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Dereference operator to get a ValueView of the ConfigValue | ||
| * | ||
| * @return ValueView of the ConfigValue | ||
| */ | ||
| ValueView | ||
| operator*() | ||
| { | ||
| return ValueView(*m_ptr); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Equality operator | ||
| * | ||
| * @param other Another ArrayIterator to compare | ||
| * @return true if iterators are equal, otherwise false | ||
| */ | ||
| bool | ||
| operator==(ArrayIterator const& other) const | ||
| { | ||
| return m_ptr == other.m_ptr; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Inequality operator | ||
| * | ||
| * @param other Another ArrayIterator to compare | ||
| * @return true if iterators are not equal, otherwise false | ||
| */ | ||
| bool | ||
| operator!=(ArrayIterator const& other) const | ||
| { | ||
| return m_ptr != other.m_ptr; | ||
| } | ||
|
|
||
| private: | ||
| pointer m_ptr; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Constructs an Array with the provided arguments | ||
| * | ||
| * @tparam Args Types of the arguments | ||
| * @param args Arguments to initialize the elements of the Array | ||
| */ | ||
| template <typename... Args> | ||
| constexpr Array(Args... args) : elements_{args...} | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @brief Returns an iterator to the beginning of the Array | ||
| * | ||
| * @return Iterator to the beginning of the Array | ||
| */ | ||
| auto | ||
| begin() const | ||
| { | ||
| return ArrayIterator{elements_.data()}; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Returns an iterator to the end of the Array | ||
| * | ||
| * @return Iterator to the end of the Array | ||
| */ | ||
| auto | ||
| end() const | ||
| { | ||
| return ArrayIterator{elements_.data() + elements_.size()}; | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** @brief Add ConfigValues to Array class | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @param value The ConfigValue to add | ||
| */ | ||
| void | ||
| emplace_back(ConfigValue value) | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| elements_.emplace_back(value); | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * @brief Returns the number of values stored in the Array | ||
| * | ||
| * @return Number of values stored in the Array | ||
| */ | ||
| size_t | ||
| size() const | ||
| { | ||
| return elements_.size(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Returns the ConfigValue at the specified index | ||
| * | ||
| * @param idx Index of the ConfigValue to retrieve | ||
| * @return ConfigValue at the specified index | ||
| */ | ||
| ConfigValue const& | ||
| at(std::size_t idx) const | ||
| { | ||
| ASSERT(idx < elements_.size(), "index is out of scope"); | ||
| return elements_[idx]; | ||
| } | ||
|
|
||
| private: | ||
| std::vector<ConfigValue> elements_; | ||
| }; | ||
|
|
||
| } // namespace util::config | ||
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,72 @@ | ||
| //------------------------------------------------------------------------------ | ||
| /* | ||
| This file is part of clio: https://github.com/XRPLF/clio | ||
| Copyright (c) 2024, the clio developers. | ||
| Permission to use, copy, modify, and distribute this software for any | ||
| purpose with or without fee is hereby granted, provided that the above | ||
| copyright notice and this permission notice appear in all copies. | ||
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| */ | ||
| //============================================================================== | ||
|
|
||
| #include "util/newconfig/ArrayView.hpp" | ||
|
|
||
| #include "util/Assert.hpp" | ||
| #include "util/newconfig/Array.hpp" | ||
| #include "util/newconfig/ConfigDefinition.hpp" | ||
| #include "util/newconfig/ConfigValue.hpp" | ||
| #include "util/newconfig/ObjectView.hpp" | ||
| #include "util/newconfig/ValueView.hpp" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <string_view> | ||
|
|
||
| namespace util::config { | ||
|
|
||
| ArrayView::ArrayView(std::string_view prefix, ClioConfigDefinition const& configDef) | ||
| : prefix_{prefix}, clioConfig_{configDef} | ||
| { | ||
| auto it = std::find_if(configDef.map_.begin(), configDef.map_.end(), [this](auto const& pair) { | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return pair.first.starts_with(prefix_); | ||
| }); | ||
| ASSERT(it != clioConfig_.map_.end(), "prefix does not exist in config definition."); | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ASSERT(prefix_.contains(".[]"), "Not an array"); | ||
| } | ||
|
|
||
| ValueView | ||
| ArrayView::valueAt(std::size_t idx) const | ||
PeterChen13579 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| ASSERT(clioConfig_.map_.contains(prefix_), "Current string is prefix, not full key"); | ||
| ConfigValue const& val = std::get<Array>(clioConfig_.map_.at(prefix_)).at(idx); | ||
| return ValueView{val}; | ||
| } | ||
|
|
||
| size_t | ||
| ArrayView::size() const | ||
| { | ||
| for (auto const& pair : clioConfig_.map_) { | ||
| if (pair.first.starts_with(prefix_)) { | ||
| return std::get<Array>(pair.second).size(); | ||
| } | ||
| } | ||
| throw std::logic_error("Arrayview is initialized with incorrect prefix."); | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| ObjectView | ||
| ArrayView::objectAt(std::size_t idx) const | ||
| { | ||
| ASSERT(idx < this->size(), "Object index is out of scope"); | ||
| return ObjectView{prefix_, idx, clioConfig_}; | ||
| } | ||
|
|
||
| } // namespace util::config | ||
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,110 @@ | ||
| //------------------------------------------------------------------------------ | ||
| /* | ||
| This file is part of clio: https://github.com/XRPLF/clio | ||
| Copyright (c) 2024, the clio developers. | ||
| Permission to use, copy, modify, and distribute this software for any | ||
| purpose with or without fee is hereby granted, provided that the above | ||
| copyright notice and this permission notice appear in all copies. | ||
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| */ | ||
| //============================================================================== | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "util/Assert.hpp" | ||
| #include "util/newconfig/Array.hpp" | ||
| #include "util/newconfig/ConfigDefinition.hpp" | ||
| #include "util/newconfig/ConfigValue.hpp" | ||
| #include "util/newconfig/ObjectView.hpp" | ||
| #include "util/newconfig/ValueView.hpp" | ||
|
|
||
| #include <cstddef> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| namespace util::config { | ||
|
|
||
| /** | ||
| * @brief View for array structure for config. | ||
cindyyan317 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * This class provides a view into an array structure within ClioConfigDefinition. | ||
| * It allows accessing individual elements of the array as either values or objects, and | ||
| * is used within the ClioConfigDefinition to represent multiple potential values. | ||
| */ | ||
| class ArrayView { | ||
| public: | ||
| ArrayView() = delete; | ||
cindyyan317 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @brief Constructs an ArrayView with the given prefix and config definition. | ||
| * | ||
| * @param prefix The prefix for the array view. | ||
| * @param configDef The ClioConfigDefinition instance. | ||
| */ | ||
| ArrayView(std::string_view prefix, ClioConfigDefinition const& configDef); | ||
|
|
||
| /** | ||
| * @brief Returns an ObjectView at the specified index. | ||
| * | ||
| * @param idx Index of the object to retrieve. | ||
| * @return ObjectView at the specified index. | ||
| */ | ||
| ObjectView | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| objectAt(std::size_t idx) const; | ||
|
|
||
| /** | ||
| * @brief Returns a ValueView at the specified index. | ||
| * | ||
| * @param idx Index of the value to retrieve. | ||
| * @return ValueView at the specified index. | ||
| */ | ||
| ValueView | ||
| valueAt(std::size_t idx) const; | ||
|
|
||
| /** | ||
| * @brief Returns the number of elements in the array. | ||
| * | ||
| * @return Number of elements in the array. | ||
| * @throw std::logic_error if somehow ArrayView doesn't exist | ||
| */ | ||
| size_t | ||
| size() const; | ||
|
|
||
| /** | ||
| * @brief Returns an iterator to the beginning of the values. | ||
| * | ||
| * @return Iterator to the beginning of the values. | ||
| */ | ||
| auto | ||
| beginValues() const | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| ASSERT(clioConfig_.map_.contains(prefix_), "Current string is prefix, not full key"); | ||
| return std::get<Array>(clioConfig_.map_.at(prefix_)).begin(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Returns an iterator to the end of the values. | ||
| * | ||
| * @return Iterator to the end of the values. | ||
| */ | ||
| auto | ||
| endValues() const | ||
| { | ||
| ASSERT(clioConfig_.map_.contains(prefix_), "Current string is prefix, not full key"); | ||
| return std::get<Array>(clioConfig_.map_.at(prefix_)).end(); | ||
| } | ||
|
|
||
| private: | ||
| std::string const prefix_; | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ClioConfigDefinition const& clioConfig_; | ||
PeterChen13579 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| } // namespace util::config | ||
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.