Commit 9f3f7385 authored by Dominik Charousset's avatar Dominik Charousset Committed by Dominik Charousset

Make UUID inspectable

Relates #1253.
parent b60ee497
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "caf/byte.hpp" #include "caf/byte.hpp"
#include "caf/detail/core_export.hpp" #include "caf/detail/core_export.hpp"
#include "caf/error.hpp"
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/string_view.hpp" #include "caf/string_view.hpp"
...@@ -156,4 +157,16 @@ CAF_CORE_EXPORT std::string to_string(const uuid& x); ...@@ -156,4 +157,16 @@ CAF_CORE_EXPORT std::string to_string(const uuid& x);
/// @relates uuid /// @relates uuid
CAF_CORE_EXPORT expected<uuid> make_uuid(string_view str); CAF_CORE_EXPORT expected<uuid> make_uuid(string_view str);
/// @relates uuid
template <class Inspector>
bool inspect(Inspector& f, uuid& x) {
if (f.has_human_readable_format()) {
auto get = [&x] { return to_string(x); };
auto set = [&x](std::string str) { return parse(str, x); };
return f.apply(get, set);
} else {
return f.apply(x.bytes());
}
}
} // namespace caf } // namespace caf
...@@ -6,7 +6,10 @@ ...@@ -6,7 +6,10 @@
#include "caf/uuid.hpp" #include "caf/uuid.hpp"
#include "caf/test/dsl.hpp" #include "core-test.hpp"
#include "caf/json_reader.hpp"
#include "caf/json_writer.hpp"
using namespace caf; using namespace caf;
...@@ -125,4 +128,37 @@ CAF_TEST(version 1 defines UUIDs that are based on time) { ...@@ -125,4 +128,37 @@ CAF_TEST(version 1 defines UUIDs that are based on time) {
} }
} }
SCENARIO("UUIDs are inspectable") {
auto id = "2ee4ded7-69c0-4dd6-876d-02e446b21784"_uuid;
GIVEN("a binary serializer") {
byte_buffer buf;
binary_serializer sink{nullptr, buf};
WHEN("applying an UUID to the serializer") {
CHECK(sink.apply(id));
THEN("a binary deserializer reproduces the UUID") {
binary_deserializer source{nullptr, buf};
uuid id_copy;
CHECK(source.apply(id_copy));
CHECK_EQ(id, id_copy);
}
}
}
GIVEN("a JSON writer") {
json_writer sink;
WHEN("applying an UUID to the writer") {
CHECK(sink.apply(id));
THEN("the writer renders the UUID as string") {
CHECK_EQ(sink.str(), R"("2ee4ded7-69c0-4dd6-876d-02e446b21784")");
}
AND("a JSON reader reproduces the UUID") {
json_reader source;
uuid id_copy;
CHECK(source.load(sink.str()));
CHECK(source.apply(id_copy));
CHECK_EQ(id, id_copy);
}
}
}
}
CAF_TEST_FIXTURE_SCOPE_END() CAF_TEST_FIXTURE_SCOPE_END()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment