diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 95f516750..8e7d96c07 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -4,6 +4,9 @@ ### Features Added +- Added `Azure::Core::Uuid::AsArray()` and `Azure::Core::Uuid::FromArray()` to enable reading or writing from an existing UUID. +This is useful when the UUID was generated outside the Azure SDK, or needs to be used from a component outside the Azure SDK. + ### Breaking Changes ### Bugs Fixed diff --git a/sdk/core/azure-core/inc/azure/core/uuid.hpp b/sdk/core/azure-core/inc/azure/core/uuid.hpp index fbf73004e..aca80c3d8 100644 --- a/sdk/core/azure-core/inc/azure/core/uuid.hpp +++ b/sdk/core/azure-core/inc/azure/core/uuid.hpp @@ -10,6 +10,7 @@ #include "azure/core/platform.hpp" +#include #include #include @@ -22,7 +23,7 @@ namespace Azure { namespace Core { private: static constexpr size_t UuidSize = 16; - uint8_t m_uuid[UuidSize]; + std::array m_uuid; // The UUID reserved variants. static constexpr uint8_t ReservedNCS = 0x80; static constexpr uint8_t ReservedRFC4122 = 0x40; @@ -30,7 +31,7 @@ namespace Azure { namespace Core { static constexpr uint8_t ReservedFuture = 0x00; private: - Uuid(uint8_t const uuid[UuidSize]) { std::memcpy(m_uuid, uuid, UuidSize); } + Uuid(uint8_t const uuid[UuidSize]) { std::memcpy(m_uuid.data(), uuid, UuidSize); } public: /** @@ -39,10 +40,23 @@ namespace Azure { namespace Core { */ std::string ToString(); + /** + * @brief Returns the binary value of the Uuid for consumption by clients who need non-string + * representation of the Uuid + * @returns An array with the binary representation of the Uuid. + */ + std::array const& AsArray() const { return m_uuid; } + /** * @brief Creates a new random UUID. * */ static Uuid CreateUuid(); + + /** + * @brief Construct a Uuid from an existing UUID represented as an array of bytes. + * @details Creates a Uuid from a UUID created in an external scope. + */ + static Uuid FromArray(std::array const& uuid); }; }} // namespace Azure::Core diff --git a/sdk/core/azure-core/src/uuid.cpp b/sdk/core/azure-core/src/uuid.cpp index e0cfecf5a..f474f24a5 100644 --- a/sdk/core/azure-core/src/uuid.cpp +++ b/sdk/core/azure-core/src/uuid.cpp @@ -70,11 +70,17 @@ namespace Azure { namespace Core { // SetVariant to ReservedRFC4122 uuid[8] = (uuid[8] | ReservedRFC4122) & 0x7F; - constexpr uint8_t version = 4; + constexpr uint8_t version = 4; // Version 4: Pseudo-random number uuid[6] = (uuid[6] & 0xF) | (version << 4); return Uuid(uuid); } + Uuid Uuid::FromArray(std::array const& uuid) + { + Uuid rv{uuid.data()}; + return rv; + } + }} // namespace Azure::Core diff --git a/sdk/core/azure-core/test/ut/uuid_test.cpp b/sdk/core/azure-core/test/ut/uuid_test.cpp index 1b522087f..ab89466a5 100644 --- a/sdk/core/azure-core/test/ut/uuid_test.cpp +++ b/sdk/core/azure-core/test/ut/uuid_test.cpp @@ -14,6 +14,14 @@ TEST(Uuid, Basic) EXPECT_EQ(uuid.ToString().length(), 36); } +TEST(Uuid, Transparent) +{ + auto uuid1 = Uuid::CreateUuid(); + auto arrayUuid1(uuid1.AsArray()); + auto uuid2 = Azure::Core::Uuid::FromArray(arrayUuid1); + EXPECT_EQ(uuid1.ToString(), uuid2.ToString()); +} + TEST(Uuid, Randomness) { const int size = 100000;