-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add constraint + parse json into Config Second part of refactoring Clio Config; First PR found [here](#1544) Steps that are left to implement: - Replacing all the places where we fetch config values (by using config.valueOr/MaybeValue) to instead get it from Config Definition - Generate markdown file using Clio Config Description
- Loading branch information
1 parent
0282504
commit af4fde9
Showing
26 changed files
with
2,056 additions
and
333 deletions.
There are no files selected for viewing
This file contains 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 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,84 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
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 "util/newconfig/Error.hpp" | ||
#include "util/newconfig/Types.hpp" | ||
|
||
#include <cstddef> | ||
#include <optional> | ||
#include <string_view> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace util::config { | ||
|
||
Array::Array(ConfigValue arg) : itemPattern_{std::move(arg)} | ||
{ | ||
} | ||
|
||
std::optional<Error> | ||
Array::addValue(Value value, std::optional<std::string_view> key) | ||
{ | ||
auto const& configValPattern = itemPattern_; | ||
auto const constraint = configValPattern.getConstraint(); | ||
|
||
auto newElem = constraint.has_value() ? ConfigValue{configValPattern.type()}.withConstraint(constraint->get()) | ||
: ConfigValue{configValPattern.type()}; | ||
if (auto const maybeError = newElem.setValue(value, key); maybeError.has_value()) | ||
return maybeError; | ||
elements_.emplace_back(std::move(newElem)); | ||
return std::nullopt; | ||
} | ||
|
||
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]; | ||
} | ||
|
||
ConfigValue const& | ||
Array::getArrayPattern() const | ||
{ | ||
return itemPattern_; | ||
} | ||
|
||
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 |
This file contains 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 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,105 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
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/Error.hpp" | ||
#include "util/newconfig/Types.hpp" | ||
|
||
#include <fmt/core.h> | ||
|
||
#include <cstdint> | ||
#include <optional> | ||
#include <regex> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <variant> | ||
|
||
namespace util::config { | ||
|
||
std::optional<Error> | ||
PortConstraint::checkTypeImpl(Value const& port) const | ||
{ | ||
if (!(std::holds_alternative<int64_t>(port) || std::holds_alternative<std::string>(port))) | ||
return Error{"Port must be a string or integer"}; | ||
return std::nullopt; | ||
} | ||
|
||
std::optional<Error> | ||
PortConstraint::checkValueImpl(Value const& port) const | ||
{ | ||
uint32_t p = 0; | ||
if (std::holds_alternative<std::string>(port)) { | ||
try { | ||
p = static_cast<uint32_t>(std::stoi(std::get<std::string>(port))); | ||
} catch (std::invalid_argument const& e) { | ||
return Error{"Port string must be an integer."}; | ||
} | ||
} 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"}; | ||
} | ||
|
||
std::optional<Error> | ||
ValidIPConstraint::checkTypeImpl(Value const& ip) const | ||
{ | ||
if (!std::holds_alternative<std::string>(ip)) | ||
return Error{"Ip value must be a string"}; | ||
return std::nullopt; | ||
} | ||
|
||
std::optional<Error> | ||
ValidIPConstraint::checkValueImpl(Value const& ip) const | ||
{ | ||
if (std::get<std::string>(ip) == "localhost") | ||
return std::nullopt; | ||
|
||
static std::regex const ipv4( | ||
R"(^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$)" | ||
); | ||
|
||
static std::regex const ip_url( | ||
R"(^((http|https):\/\/)?((([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6})|(((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])))(:\d{1,5})?(\/[^\s]*)?$)" | ||
); | ||
if (std::regex_match(std::get<std::string>(ip), ipv4) || std::regex_match(std::get<std::string>(ip), ip_url)) | ||
return std::nullopt; | ||
|
||
return Error{"Ip is not a valid ip address"}; | ||
} | ||
|
||
std::optional<Error> | ||
PositiveDouble::checkTypeImpl(Value const& num) const | ||
{ | ||
if (!(std::holds_alternative<double>(num) || std::holds_alternative<int64_t>(num))) | ||
return Error{"Double number must be of type int or double"}; | ||
return std::nullopt; | ||
} | ||
|
||
std::optional<Error> | ||
PositiveDouble::checkValueImpl(Value const& num) const | ||
{ | ||
if (std::get<double>(num) >= 0) | ||
return std::nullopt; | ||
return Error{"Double number must be greater than 0"}; | ||
} | ||
|
||
} // namespace util::config |
Oops, something went wrong.