Skip to content
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

refactor: Clio Config #1593

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ target_sources(
TimeUtils.cpp
TxUtils.cpp
LedgerUtils.cpp
newconfig/Array.cpp
newconfig/ArrayView.cpp
newconfig/ConfigConstraints.cpp
newconfig/ConfigDefinition.cpp
newconfig/ConfigFileJson.cpp
newconfig/ObjectView.cpp
newconfig/ArrayView.cpp
newconfig/ValueView.cpp
)

Expand Down
67 changes: 67 additions & 0 deletions src/util/newconfig/Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//------------------------------------------------------------------------------
/*
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/Array.hpp"

#include "util/Assert.hpp"
#include "util/newconfig/ConfigValue.hpp"

#include <cstddef>
#include <utility>
#include <vector>

namespace util::config {

void
Array::emplaceBack(ConfigValue value)
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
{
ASSERT(value.type() == elements_.front().type(), "Trying to insert a Value of Wrong Type");
if (!elements_.front().hasValue()) {
elements_.front() = std::move(value);
} else {
elements_.push_back(std::move(value));
}
}

size_t
Array::size() const
{
return elements_.size();
}

ConfigValue const&
Array::at(std::size_t idx) const
{
ASSERT(idx < elements_.size(), "Index is out of scope");
return elements_[idx];
}

std::vector<ConfigValue>::const_iterator
Array::begin() const
{
return elements_.begin();
}

std::vector<ConfigValue>::const_iterator
Array::end() const
{
return elements_.end();
}

} // namespace util::config
49 changes: 27 additions & 22 deletions src/util/newconfig/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,32 @@

#include "util/Assert.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/ObjectView.hpp"
#include "util/newconfig/ValueView.hpp"

#include <cstddef>
#include <iterator>
#include <type_traits>
#include <utility>
#include <vector>

namespace util::config {

/**
* @brief Array definition for Json/Yaml config
* @brief Array definition to store multiple values provided by the user from Json/Yaml
*
* Used in ClioConfigDefinition to represent multiple potential values (like whitelist)
* Is constructed with only 1 element which states which type/constraint must every element
* In the array satisfy
*/
class Array {
public:
/**
* @brief Constructs an Array with the provided arguments
* @brief Constructs an Array with provided Arg
*
* @tparam Args Types of the arguments
* @param args Arguments to initialize the elements of the Array
* @param arg Argument to set the type and constraint of ConfigValues in Array
*/
template <typename... Args>
constexpr Array(Args&&... args) : elements_{std::forward<Args>(args)...}
template <typename Arg>
constexpr Array(Arg&& arg) : elements_{std::forward<Arg>(arg)}
{
ASSERT(!elements_.at(0).hasValue(), "Array does not include default values");
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -56,21 +55,15 @@ class Array {
* @param value The ConfigValue to add
*/
void
emplaceBack(ConfigValue value)
{
elements_.push_back(std::move(value));
}
emplaceBack(ConfigValue value);

/**
* @brief Returns the number of values stored in the Array
*
* @return Number of values stored in the Array
*/
[[nodiscard]] size_t
size() const
{
return elements_.size();
}
size() const;

/**
* @brief Returns the ConfigValue at the specified index
Expand All @@ -79,11 +72,23 @@ class Array {
* @return ConfigValue at the specified index
*/
[[nodiscard]] ConfigValue const&
at(std::size_t idx) const
{
ASSERT(idx < elements_.size(), "index is out of scope");
return elements_[idx];
}
at(std::size_t idx) const;

/**
* @brief Returns an iterator to the beginning of the ConfigValue vector.
*
* @return A constant iterator to the beginning of the vector.
*/
[[nodiscard]] std::vector<ConfigValue>::const_iterator
begin() const;

/**
* @brief Returns an iterator to the end of the ConfigValue vector.
*
* @return A constant iterator to the end of the vector.
*/
[[nodiscard]] std::vector<ConfigValue>::const_iterator
end() const;

private:
std::vector<ConfigValue> elements_;
Expand Down
137 changes: 137 additions & 0 deletions src/util/newconfig/ConfigConstraints.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//------------------------------------------------------------------------------
/*
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/ConfigConstraints.hpp"

#include "util/newconfig/Errors.hpp"

#include <fmt/core.h>

#include <algorithm>
#include <cstdint>
#include <optional>
#include <regex>
#include <string>
#include <string_view>
#include <variant>

namespace util::config {

std::optional<Error>
PortConstraint::checkConstraint(Constraint::ValueType const& port) const
{
uint32_t p = 0;
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
if (!checkType(port))
return Error{"Port must be an integer or string"};
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved

if (std::holds_alternative<std::string>(port)) {
p = static_cast<uint32_t>(std::stoi(std::get<std::string>(port)));
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
} else {
p = static_cast<uint32_t>(std::get<int64_t>(port));
}
if (p >= portMin && p <= portMax)
return std::nullopt;
return Error{"Port does not satisfy the constraint bounds"};
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
}

std::optional<Error>
ChannelNameConstraint::checkConstraint(Constraint::ValueType const& channel) const
{
if (!checkType(channel))
return Error{"Channel name must be a string"};

if (std::ranges::any_of(channels, [&channel](std::string_view name) {
return std::get<std::string>(channel) == name;
}))
return std::nullopt;
return Error{"Channel name must be one of General, WebServer, Backend, RPC, ETL, Subscriptions, Performance"};
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
}

std::optional<Error>
LogLevelNameConstraint::checkConstraint(Constraint::ValueType const& logger) const
{
if (!checkType(logger))
return Error{"log_level must be a string"};
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved

if (std::ranges::any_of(logLevels, [&logger](std::string_view name) {
return std::get<std::string>(logger) == name;
}))
return std::nullopt;
return Error{"log_level must be one of trace, debug, info, warning, error, fatal, count"};
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
}

std::optional<Error>
ValidIPConstraint::checkConstraint(Constraint::ValueType const& ip) const
{
if (!checkType(ip))
return Error{"ip must be a string"};

static std::regex const ipv4Regex(
R"(^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)"
);
if (std::regex_match(std::get<std::string>(ip), ipv4Regex))
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
return std::nullopt;
return Error{"ip is not a valid ip address"};
}

std::optional<Error>
CassandraName::checkConstraint(ValueType const& name) const
{
if (checkType(name) && std::get<std::string>(name) == "cassandra")
return std::nullopt;
return Error{"database.type must be string Cassandra"};
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
}

std::optional<Error>
LoadConstraint::checkConstraint(ValueType const& name) const
{
if (!checkType(name))
return Error{"cache.load must be a string"};

auto const load = std::get<std::string>(name);
if (load == "sync" || load == "async" || load == "none")
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
return std::nullopt;
return Error{"cache.load must be string sync, async, or none"};
}

std::optional<Error>
LogTagStyle::checkConstraint(ValueType const& tagName) const
{
if (!checkType(tagName))
return Error{"log_tag_style must be a string"};

if (std::ranges::any_of(logTags, [&tagName](std::string_view name) {
return std::get<std::string>(tagName) == name;
}))
return std::nullopt;
return Error{"log_tag_style must be string int, uint, null, none, or uuid"};
}

std::optional<Error>
APIVersionConstraint::checkConstraint(ValueType const& apiVersion) const
{
if (!checkType(apiVersion))
return Error{"api_version must be an integer"};

if (std::get<int64_t>(apiVersion) <= API_VERSION_MAX && std::get<int64_t>(apiVersion) >= API_VERSION_MIN)
PeterChen13579 marked this conversation as resolved.
Show resolved Hide resolved
return std::nullopt;
return Error{fmt::format("api_version must be between {} and {}", API_VERSION_MIN, API_VERSION_MAX)};
}

} // namespace util::config
Loading
Loading