diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index 50a96b07..f0044088 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -2,6 +2,7 @@ #define RFL_VALIDATOR_HPP_ #include +#include #include #include #include @@ -84,6 +85,11 @@ struct Validator { return *this; } + /// Equality operator other Validators. + bool operator==(const Validator& _other) const { + return value() == _other.value(); + } + /// Exposes the underlying value. T& value() { return value_; } @@ -98,6 +104,28 @@ struct Validator { T value_; }; +template +inline auto operator<=>(const Validator& _v1, + const Validator& _v2) { + return _v1.value() <=> _v2.value(); +} + +template +inline auto operator<=>(const Validator& _v, const T& _t) { + return _v.value() <=> _t; +} + } // namespace rfl +namespace std { + +template +struct hash> { + size_t operator()(const rfl::Validator& _v) const { + return hash()(_v.value()); + } +}; + +} // namespace std + #endif diff --git a/include/rfl/patterns.hpp b/include/rfl/patterns.hpp index 3dfccef2..8dcbff1b 100644 --- a/include/rfl/patterns.hpp +++ b/include/rfl/patterns.hpp @@ -6,6 +6,9 @@ namespace rfl { +/// This pattern only matches alphanumeric characters. +using AlphaNumeric = Pattern; + /// This pattern will match valid Base64 encoded strings with or without /// padding. using Base64Encoded = Pattern; diff --git a/tests/json/test_alphanumeric_map.hpp b/tests/json/test_alphanumeric_map.hpp new file mode 100644 index 00000000..00129208 --- /dev/null +++ b/tests/json/test_alphanumeric_map.hpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_alphanumeric_map() { + std::cout << "test_alphanumeric_map" << std::endl; + + std::map> homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})"); +} diff --git a/tests/json/test_alphanumeric_unordered_map.hpp b/tests/json/test_alphanumeric_unordered_map.hpp new file mode 100644 index 00000000..81a2a67d --- /dev/null +++ b/tests/json/test_alphanumeric_unordered_map.hpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_alphanumeric_unordered_map() { + std::cout << "test_alphanumeric_unordered_map" << std::endl; + + using Map = + std::unordered_map>; + Map homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("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(json_string); + std::cout << "OK" << std::endl << std::endl; +} diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 27a5549a..2cbc0c13 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -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();