Made sure that validators can be used as keys

This commit is contained in:
Patrick Urbanke
2023-11-18 22:22:29 +01:00
parent cfcc388480
commit 35904c584d
5 changed files with 81 additions and 0 deletions
+28
View File
@@ -2,6 +2,7 @@
#define RFL_VALIDATOR_HPP_
#include <concepts>
#include <functional>
#include <optional>
#include <regex>
#include <string>
@@ -84,6 +85,11 @@ struct Validator {
return *this;
}
/// Equality operator other Validators.
bool operator==(const Validator<T, V>& _other) const {
return value() == _other.value();
}
/// Exposes the underlying value.
T& value() { return value_; }
@@ -98,6 +104,28 @@ struct Validator {
T value_;
};
template <class T, class V>
inline auto operator<=>(const Validator<T, V>& _v1,
const Validator<T, V>& _v2) {
return _v1.value() <=> _v2.value();
}
template <class T, class V>
inline auto operator<=>(const Validator<T, V>& _v, const T& _t) {
return _v.value() <=> _t;
}
} // namespace rfl
namespace std {
template <class T, class V>
struct hash<rfl::Validator<T, V>> {
size_t operator()(const rfl::Validator<T, V>& _v) const {
return hash<T>()(_v.value());
}
};
} // namespace std
#endif
+3
View File
@@ -6,6 +6,9 @@
namespace rfl {
/// This pattern only matches alphanumeric characters.
using AlphaNumeric = Pattern<R"(^[a-zA-Z0-9]*$)", "AlphaNumeric">;
/// This pattern will match valid Base64 encoded strings with or without
/// padding.
using Base64Encoded = Pattern<R"(^[A-Za-z0-9+/]*(={0,2})?$)", "Base64Encoded">;
+20
View File
@@ -0,0 +1,20 @@
#include <iostream>
#include <map>
#include <memory>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include "write_and_read.hpp"
void test_alphanumeric_map() {
std::cout << "test_alphanumeric_map" << std::endl;
std::map<rfl::AlphaNumeric, std::unique_ptr<std::string>> homer;
homer.insert(
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
homer.insert(
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})");
}
@@ -0,0 +1,26 @@
#include <iostream>
#include <memory>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <unordered_map>
#include "write_and_read.hpp"
void test_alphanumeric_unordered_map() {
std::cout << "test_alphanumeric_unordered_map" << std::endl;
using Map =
std::unordered_map<rfl::AlphaNumeric, std::unique_ptr<std::string>>;
Map homer;
homer.insert(
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
homer.insert(
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
// Unordered maps are unpredictable. We therefore only make sure that this
// compiles.
const auto json_string = rfl::json::write(homer);
const auto homer2 = rfl::json::read<Map>(json_string);
std::cout << "OK" << std::endl << std::endl;
}
+4
View File
@@ -1,4 +1,6 @@
#include "test_all_of.hpp"
#include "test_alphanumeric_map.hpp"
#include "test_alphanumeric_unordered_map.hpp"
#include "test_anonymous_fields.hpp"
#include "test_any_of.hpp"
#include "test_array.hpp"
@@ -80,6 +82,8 @@ int main() {
test_unordered_multiset();
test_string_map();
test_string_unordered_map();
test_alphanumeric_map();
test_alphanumeric_unordered_map();
test_literal_map();
test_literal_unordered_map();
test_error_messages();