Skip to content

Commit

Permalink
Refactor UUID generation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandan Kumar committed Dec 20, 2024
1 parent 6d37bca commit a698bf3
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 165 deletions.
84 changes: 59 additions & 25 deletions include/faker-cxx/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@

namespace faker::string
{
enum class Uuid
{
V1, // Version 1: UUIDs using a timestamp and monotonic counter.
V3, // Version 3: UUIDs based on the MD5 hash of some data.
V4, // Version 4: UUIDs with random data.
V5, // Version 5: UUIDs based on the SHA1 hash of some data.
V6, // Version 6: UUIDs using a timestamp and monotonic counter (sortable).
V7, // Version 7: UUIDs using a Unix timestamp (sortable).
V8 // Version 8: UUIDs using user-defined data.
};

enum class StringCasing
{
Expand Down Expand Up @@ -80,27 +70,71 @@ FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>&
FAKER_CXX_EXPORT std::string generateAtLeastString(const GuaranteeMap& guarantee);

/**
* @brief Generates an Universally Unique Identifier, defaults to V4.
* @brief Generates a UUID version 1 string.
*
* UUID v1 is generated using the current timestamp, a randomly generated clock sequence,
* and a randomly generated node identifier. The timestamp is offset by the UUID epoch.
*
* @returns UUID v1 string in the standard 8-4-4-4-12 format.
*
* @code
* FAKER_CXX_EXPORT std::string uuid = uuidV1(); // "550e8400-e29b-41d4-a716-446655440000"
* @endcode
*/
FAKER_CXX_EXPORT std::string uuidV1();

/**
* @brief Generates a UUID version 3 string.
*
* UUID v3 is generated by creating a random 16-byte hash and modifying specific bytes
* to conform to the UUID version 3 and variant specifications.
*
* @returns UUID v3 string in the standard 8-4-4-4-12 format.
*
* @code
* FAKER_CXX_EXPORT std::string uuid = uuidV3(); // "d9b2d63d-a233-3de7-8c4b-fb6e96c46e04"
* @endcode
*/
FAKER_CXX_EXPORT std::string uuidV3();

/**
* @brief Generates a UUID version 4 string.
*
* UUID v4 is randomly generated. Specific bytes are modified to ensure compliance
* with the UUID version 4 and variant standards.
*
* @returns UUID v4 string in the standard 8-4-4-4-12 format.
*
* @code
* FAKER_CXX_EXPORT std::string uuid = uuidV4(); // "550e8400-e29b-41d4-a716-446655440000"
* @endcode
*/
FAKER_CXX_EXPORT std::string uuidV4();

/**
* @brief Generates a UUID version 5 string.
*
* UUID v5 is generated using a namespace UUID and a name string. The SHA-1 hash of the
* namespace and name is used to produce the UUID. Specific bytes are modified to conform
* to the UUID version 5 and variant specifications. If no namespace UUID is provided,
* a default value will be used.
*
* @param gen A random number generator (type RandomGenerator)
* @param name The name string used in the UUID generation.
* @param namespaceUuid (Optional) The namespace UUID used in the UUID generation.
* Defaults to "bfd98c8e-48c0-46af-bf44-255d24883f8f".
*
* @returns UUID.
* @returns UUID v5 string in the standard 8-4-4-4-12 format.
*
* @code
* faker::string::uuid() // "27666229-cedb-4a45-8018-98b1e1d921e2" // V4
* faker::string::uuid(Uuid::V1) // "04f916a0-af32-11ef-9cd2-0242ac120002"
* faker::string::uuid(Uuid::V3) // "a3bb189e-8bf9-3888-9912-ace4e6543002"
* faker::string::uuid(Uuid::V4) // "27666229-cedb-4a45-8018-98b1e1d921e2"
* faker::string::uuid(Uuid::V5) // "27666229-cedb-4a45-8018-98b1e1d921e2"
* faker::string::uuid(Uuid::V5,"Spiderman","522b1d28-0d88-44ed-9a93-56540d57950c") // "a03b6916-3844-543d-85ad-f5a7c467e54b"
* faker::string::uuid(Uuid::V5,"Spiderman") // "4a4e00d6-f666-55fc-8d19-476f872a77b4"
* faker::string::uuid(Uuid::V6) // "27666229-cedb-4a45-8018-98b1e1d921e2"
* faker::string::uuid(Uuid::V7) // "27666229-cedb-4a45-8018-98b1e1d921e2"
* faker::string::uuid(Uuid::V8) // "27666229-cedb-4a45-8018-98b1e1d921e2"
* FAKER_CXX_EXPORT std::string uuid1 = uuidV5("example");
* // Uses default namespaceUuid: "bfd98c8e-48c0-46af-bf44-255d24883f8f"
*
* FAKER_CXX_EXPORT std::string uuid2 = uuidV5("example", "550e8400-e29b-41d4-a716-446655440000");
* // Uses provided namespaceUuid
* @endcode
*/
FAKER_CXX_EXPORT std::string uuid(Uuid uuid = Uuid::V4, std::string name = "",
std::string namespaceUuid = "bfd98c8e-48c0-46af-bf44-255d24883f8f");
FAKER_CXX_EXPORT std::string uuidV5(std::string name,
std::string namespaceUuid = "bfd98c8e-48c0-46af-bf44-255d24883f8f");

/**
* @brief Generates an Universally Unique Lexicographically Sortable Identifier.
Expand Down
Loading

0 comments on commit a698bf3

Please sign in to comment.