Skip to content

Commit

Permalink
RCPP-53 Add a realm::uuid to be constructed with std::array<uint8_t, 16>
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire committed Mar 4, 2024
1 parent 552ddd1 commit d33da72
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ X.Y.Z Release notes (YYYY-MM-DD)
* Add `managed<realm::mixed>::set_link` for setting a link in a mixed proeprty type.
* Add compile time checking to prevent misuse of managed property types.
* Add `managed<std::vector<>>::as_results()` to allow the ability to derive a `realm::results<>` collection from a managed vector.
* Allow a `realm::uuid` to be constructed with `std::array<uint8_t, 16>`.

### Breaking Changes
* None
Expand Down
6 changes: 5 additions & 1 deletion src/cpprealm/internal/bridge/uuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ namespace realm::internal::bridge {
m_uuid = UUID::UUIDBytes();
}

uuid::uuid(const std::string &v) {
uuid::uuid(const std::string& v) {
m_uuid = UUID(v).to_bytes();
}

uuid::uuid(const std::array<uint8_t, 16>& v) {
m_uuid = v;
}

uuid::uuid(const ::realm::uuid &v)
{
m_uuid = UUID(v.to_string()).to_bytes();
Expand Down
1 change: 1 addition & 0 deletions src/cpprealm/internal/bridge/uuid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace realm::internal::bridge {
~uuid() = default;
uuid(const UUID&); //NOLINT(google-explicit-constructor);
explicit uuid(const std::string&);
uuid(const std::array<uint8_t, 16>&);
uuid(const struct ::realm::uuid&); //NOLINT(google-explicit-constructor);
operator UUID() const final; //NOLINT(google-explicit-constructor);
operator ::realm::uuid() const; //NOLINT(google-explicit-constructor);
Expand Down
5 changes: 5 additions & 0 deletions src/cpprealm/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ namespace realm {
uuid::uuid(const std::string &v)
: m_uuid(v) {
}

uuid::uuid(const std::array<uint8_t, 16>& bytes)
: m_uuid(bytes) {
}

uuid::uuid(const internal::bridge::uuid &v)
: m_uuid(v) {
}
Expand Down
1 change: 1 addition & 0 deletions src/cpprealm/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace realm {

struct uuid {
explicit uuid(const std::string &);
uuid(const std::array<uint8_t, 16>&);
uuid() = default;
[[nodiscard]] std::string to_string() const;
[[nodiscard]] std::string to_base64() const;
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ add_executable(cpprealm_db_tests
db/performance_tests.cpp
db/numeric_tests.cpp
db/set_tests.cpp
db/frozen_tests.cpp)
db/frozen_tests.cpp
db/uuid_tests.cpp)

if(MSVC)
set_property(TARGET cpprealm_sync_tests PROPERTY
Expand Down
55 changes: 55 additions & 0 deletions tests/db/uuid_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "../main.hpp"
#include "test_objects.hpp"

#include "realm/uuid.hpp"

using namespace realm;

TEST_CASE("uuid", "[uuid]") {
realm_path path;
realm::db_config config;
config.set_path(path);
SECTION("unmanaged_managed_uuid") {
auto realm = db(std::move(config));
std::string uuid_str = "e621e1f8-c36c-495a-93fc-0c247a3e6e5f";
auto core_uuid = UUID("e621e1f8-c36c-495a-93fc-0c247a3e6e5f");

auto uuid1 = realm::uuid();
CHECK(uuid1.to_string() == "00000000-0000-0000-0000-000000000000");
auto uuid2 = realm::uuid(uuid_str);
CHECK(uuid2.to_string() == "e621e1f8-c36c-495a-93fc-0c247a3e6e5f");
auto uuid3 = realm::uuid(core_uuid.to_bytes());
CHECK(uuid3.to_string() == "e621e1f8-c36c-495a-93fc-0c247a3e6e5f");
CHECK(uuid3.to_bytes() == core_uuid.to_bytes());

CHECK(uuid2 == uuid3);
CHECK(uuid2 != uuid1);

auto object = AllTypesObject();
object.uuid_col = uuid2;
object.opt_uuid_col = uuid2;
object.list_uuid_col.push_back(uuid2);
object.map_uuid_col = {
{"a", uuid2}
};

CHECK(object.uuid_col == uuid2);
CHECK(*object.opt_uuid_col == uuid2);
CHECK(object.list_uuid_col[0] == uuid2);
CHECK(uuid2 == object.map_uuid_col["a"]);

auto managed_obj = realm.write([&] {
return realm.add(std::move(object));
});

CHECK(managed_obj.uuid_col == uuid2);
CHECK(*managed_obj.opt_uuid_col == uuid2);
CHECK(managed_obj.list_uuid_col[0] == uuid2);
CHECK(managed_obj.map_uuid_col["a"] == uuid2);

CHECK(managed_obj.uuid_col != uuid1);
CHECK(*managed_obj.opt_uuid_col != uuid1);
CHECK(managed_obj.list_uuid_col[0] != uuid1);
CHECK(managed_obj.map_uuid_col["a"] != uuid1);
}
}

0 comments on commit d33da72

Please sign in to comment.