Documentation
¶
Overview ¶
Package xuid provides an extended UUID implementation that adds a type prefix while maintaining compatibility with standard UUIDs.
XUIDs consist of a standard UUID with an optional type prefix (up to 5 characters), encoded in base32 rather than base16 for better readability and shorter string representation. They are designed to be used as identifiers with built-in type information.
Index ¶
- Variables
- func Must[T any](x T, err error) T
- type XUID
- func FromKey(key string) (*XUID, error)
- func FromKeyPrefix(key, prefix string) (*XUID, error)
- func FromUUID(u uuid.UUID, prefix string) (*XUID, error)
- func MustParse(s string) *XUID
- func MustParseUUID(inputUuid, prefix string) *XUID
- func New(prefix string) *XUID
- func NewRandom(prefix string) (*XUID, error)
- func Parse(s string) (*XUID, error)
- func ParsePrefix(s, prefix string) (*XUID, error)
- func ParseUUID(inputUuid, prefix string) (*XUID, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrBadPrefix is returned when a XUID's prefix doesn't match the expected value // This is typically used by ParsePrefix to validate that an ID belongs to a specific entity type ErrBadPrefix = errors.New("xuid: bad prefix") )
Package errors that can be returned by the XUID library functions
Functions ¶
Types ¶
type XUID ¶
type XUID struct { // Prefix is a string of up to 5 characters identifying the type of entity Prefix string // UUID is the standard universally unique identifier that provides // the uniqueness guarantee UUID uuid.UUID }
XUID represents an extended UUID with a type prefix. It consists of a standard UUID and an optional prefix string that identifies the type of entity this ID represents.
The prefix is limited to 5 characters maximum for string representation. When encoded to string, XUIDs use base32 encoding rather than base16 (hex) to produce more compact strings while maintaining the same information.
func FromKey ¶ added in v0.1.6
FromKey generates a deterministic XUID based on the provided key string. It always produces the same XUID for the same key value.
The generated XUID always has the prefix "utref" (utility reference). This is useful for creating reference objects with predictable IDs.
func FromKeyPrefix ¶ added in v0.1.6
FromKeyPrefix generates a deterministic XUID based on both a key and a prefix. It always produces the same XUID for the same combination of key and prefix.
This is useful for objects that need consistent IDs across different environments. The prefix is used both for generating the UUID and as the type prefix in the XUID.
func FromUUID ¶
FromUUID creates a new XUID from an existing UUID and a prefix. This is useful when you want to convert a standard UUID to a XUID with type information.
func MustParse ¶
MustParse parses a string into a XUID, panicking if parsing fails. This is useful for constants or when the XUID string is known to be valid.
func MustParseUUID ¶
MustParseUUID works like ParseUUID but panics if parsing fails. This is useful for constants or when the UUID string is known to be valid.
Takes a UUID-formatted string and an optional prefix to assign to the XUID.
func New ¶
New creates a new random XUID with the given prefix. It's a shorthand for Must(NewRandom(prefix)) and will panic if the random generator fails for some reason.
This is the most common method for creating new XUIDs.
func NewRandom ¶
NewRandom generates a new random XUID with the given prefix. It uses the underlying uuid.NewRandom() function to generate a version 4 (random) UUID and assigns the provided prefix.
func Parse ¶
Parse parses a string representation and returns the resulting XUID. It can handle XUID formatted strings as well as standard UUIDs. For XUIDs, it supports both prefixed and non-prefixed formats.
If the input string doesn't conform to XUID format, Parse will attempt to interpret it as a standard UUID and assign an empty prefix.
Returns the parsed XUID and any error encountered.
func ParsePrefix ¶ added in v0.1.8
ParsePrefix parses a XUID string and verifies that the prefix matches the provided one. Returns a parsed XUID if successful, or an error if either: - The input cannot be parsed as a XUID - The prefix doesn't match the expected value
This is useful for type-safe XUID parsing where the caller expects a specific entity type.
func ParseUUID ¶
ParseUUID converts a standard UUID string into a XUID. It takes a UUID-formatted string and an optional prefix to assign to the XUID. If the prefix is empty, the XUID will have no type information.
This is useful for converting existing UUIDs to XUIDs or for working with existing systems that use standard UUIDs.
Returns a XUID pointer and any error encountered during parsing.
func (XUID) Equals ¶ added in v0.1.5
Equals compares two XUIDs and returns true if they are equal, meaning they have the same prefix and UUID.
func (XUID) MarshalJSON ¶ added in v0.1.3
MarshalJSON implements the json.Marshaler interface for XUID. It converts the XUID to its string representation (prefix-aaaaaa-aaaa-aaaa-aaaa-aaaaaaaa) and then marshals that string to JSON.
This makes XUIDs appear as strings in JSON output, which is more readable and works better with other systems that expect string IDs.
func (*XUID) Scan ¶
Scan implements the sql.Scanner interface for XUID. It allows XUIDs to be scanned directly from database query results.
The scan supports the following types: - string: Parses the string as a XUID - sql.RawBytes: Converts to string and parses as a XUID
This functionality enables XUIDs to be used directly with database/sql operations without manual type conversion.
func (XUID) String ¶
String formats the XUID as a string with the format: prefix-aaaaaa-aaaa-aaaa-aaaa-aaaaaaaa where 'prefix' is the type prefix (up to 5 characters) and the remaining parts are the base32-encoded UUID with hyphens for readability. The result is always lowercase and contains no padding characters.
func (*XUID) ToUUID ¶
ToUUID returns the string representation of the underlying UUID in standard UUID format (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). This is useful when you need the standard UUID representation rather than the XUID format.
func (*XUID) UnmarshalJSON ¶ added in v0.1.3
UnmarshalJSON implements the json.Unmarshaler interface for XUID. It allows XUIDs to be directly unmarshaled from JSON string representations.
The JSON value should be a string in valid XUID format or convertible to a valid XUID format through the Parse function.