Documentation ¶
Overview ¶
Package uuidv8 provides utilities for generating, parsing, validating, and converting UUIDv8 (based on the UUIDv8 specification) and related components.
Index ¶
Constants ¶
const ( TimestampBits32 = 32 // Use 32-bit timestamp TimestampBits48 = 48 // Use 48-bit timestamp TimestampBits60 = 60 // Use 60-bit timestamp )
Supported timestamp bit sizes for UUIDv8.
Variables ¶
This section is empty.
Functions ¶
func IsValidUUIDv8 ¶
IsValidUUIDv8 validates if a given string is a valid UUIDv8.
Parameters: - uuid: A string representation of a UUID.
Returns: - A boolean indicating whether the UUID is valid.
- `true` if the UUID has the correct version and variant bits and is well-formed.
- `false` if the UUID is invalid or all zero.
func New ¶
New generates a UUIDv8 with default parameters.
Default behavior: - Timestamp: Current time in nanoseconds. - ClockSeq: Random 12-bit value. - Node: Random 6-byte node identifier.
Returns: - A string representation of the generated UUIDv8. - An error if any component generation fails.
func NewWithParams ¶
func NewWithParams(timestamp uint64, clockSeq uint16, node []byte, timestampBits int) (string, error)
NewWithParams generates a new UUIDv8 based on the provided timestamp, clock sequence, and node.
Parameters: - timestamp: A 32-, 48-, or 60-bit timestamp value (depending on `timestampBits`). - clockSeq: A 12-bit clock sequence value for sequencing UUIDs generated within the same timestamp. - node: A 6-byte slice representing a unique identifier (e.g., MAC address or random bytes). - timestampBits: The number of bits in the timestamp (32, 48, or 60).
Returns: - A string representation of the generated UUIDv8. - An error if the input parameters are invalid (e.g., incorrect node length or unsupported timestamp size).
Types ¶
type UUIDv8 ¶
type UUIDv8 struct { Timestamp uint64 // The timestamp component of the UUID. ClockSeq uint16 // The clock sequence component of the UUID. Node []byte // The node component of the UUID (typically 6 bytes). }
UUIDv8 represents a parsed UUIDv8 object.
Fields: - Timestamp: Encoded timestamp value (up to 60 bits). - ClockSeq: Clock sequence value (up to 12 bits). - Node: Node value, typically a 6-byte unique identifier.
func FromString ¶
FromString parses a UUIDv8 string into its components.
Parameters: - uuid: A string representation of a UUIDv8.
Returns: - A pointer to a UUIDv8 struct containing the parsed components (timestamp, clockSeq, node). - An error if the UUID is invalid or cannot be parsed.
func FromStringOrNil ¶
FromStringOrNil parses a UUIDv8 string into its components, returning nil if invalid or all zero.
Parameters: - uuid: A string representation of a UUIDv8.
Returns: - A pointer to a UUIDv8 struct if the UUID is valid. - Nil if the UUID is invalid or represents an all-zero UUID.
func (*UUIDv8) MarshalJSON ¶
MarshalJSON serializes a UUIDv8 object into its JSON representation.
Returns: - A JSON-encoded byte slice of the UUID string. - An error if the serialization fails.
func (*UUIDv8) Scan ¶ added in v1.1.0
Scan implements the [sql.Scanner] interface for database reads.
func (*UUIDv8) UnmarshalJSON ¶
UnmarshalJSON deserializes a JSON-encoded UUIDv8 string into a UUIDv8 object.
Parameters: - data: A JSON-encoded byte slice containing the UUID string.
Returns: - An error if the deserialization fails or if the UUID string is invalid.