Documentation ¶
Overview ¶
Package uuid provides a wrapper for handling UUID codes.
Index ¶
- Variables
- func NodeID() string
- func Normalize(u *UUID)
- func SetRandomNodeID()
- func Within(ttl time.Duration) validation.Rule
- type Identify
- type UUID
- func MakeV1() UUIDdeprecated
- func MakeV3(space UUID, data []byte) UUIDdeprecated
- func MakeV4() UUIDdeprecated
- func MakeV5(space UUID, data []byte) UUIDdeprecated
- func MustParse(s string) UUID
- func NewV1() *UUIDdeprecated
- func NewV3(space UUID, data []byte) *UUIDdeprecated
- func NewV4() *UUIDdeprecated
- func NewV5(space UUID, data []byte) *UUIDdeprecated
- func Parse(s string) (UUID, error)
- func ParseBase64(s string) (UUID, error)
- func ShouldParse(s string) UUID
- func V1() UUID
- func V3(space UUID, data []byte) UUID
- func V4() UUID
- func V5(space UUID, data []byte) UUID
- func V6() UUID
- func V7() UUID
- type Version
Constants ¶
This section is empty.
Variables ¶
var ( // Valid confirms the UUID is valid Valid = versionRule{} // IsV1 confirms the UUID is version 1 IsV1 = versionRule{/* contains filtered or unexported fields */} // IsV3 confirms the UUID is version 3 IsV3 = versionRule{/* contains filtered or unexported fields */} // IsV4 confirms the UUID is version 4 IsV4 = versionRule{/* contains filtered or unexported fields */} // IsV5 confirms the UUID is version 5 IsV5 = versionRule{/* contains filtered or unexported fields */} // IsV6 confirms the UUID is version 6 IsV6 = versionRule{/* contains filtered or unexported fields */} // IsV7 confirms the UUID is version 7 IsV7 = versionRule{/* contains filtered or unexported fields */} // HasTimestamp confirms the UUID is based on a timestamp version HasTimestamp = versionRule{/* contains filtered or unexported fields */} // Timeless confirms the UUID is not based on a timestamp version Timeless = versionRule{/* contains filtered or unexported fields */} // IsNotZero confirms the UUID is not zero IsNotZero = versionRule{/* contains filtered or unexported fields */} )
Functions ¶
func Normalize ¶ added in v0.67.10
func Normalize(u *UUID)
Normalize will ensure that zero value UUIDs will be empty strings instead of zeros.
func SetRandomNodeID ¶
func SetRandomNodeID()
SetRandomNodeID is used to generate a random host ID to be used in V1 UUIDs instead of the MAC address. This is stored in the uuid library as a global constant, so should be called just once when starting the application if you're intending to generate V1 UUIDs to get a node ID that won't change.
Types ¶
type Identify ¶ added in v0.73.0
type Identify struct {
UUID UUID `json:"uuid,omitempty" jsonschema:"title=UUID,description=Universally Unique Identifier."`
}
Identify defines a struct that can be used to identify a document by a UUID.
func IdentifyParse ¶ added in v0.73.0
IdentifyParse will parse the provided string as a UUID in the UUID field, or panic. This is mainly meant to be used in tests.
func IdentifyV1 ¶ added in v0.73.0
func IdentifyV1() Identify
IdentifyV1 is a helper method to generate a version 1 uuid ready to embed.
func IdentifyV3 ¶ added in v0.73.0
IdentifyV3 is a helper method to generate a version 3 uuid ready to embed.
func IdentifyV4 ¶ added in v0.73.0
func IdentifyV4() Identify
IdentifyV4 is a helper method to generate a version 4 uuid ready to embed.
func IdentifyV5 ¶ added in v0.73.0
IdentifyV5 is a helper method to generate a version 5 uuid ready to embed.
func IdentifyV6 ¶ added in v0.74.0
func IdentifyV6() Identify
IdentifyV6 is a helper method to generate a version 6 uuid ready to embed.
func IdentifyV7 ¶ added in v0.74.0
func IdentifyV7() Identify
IdentifyV7 is a helper method to generate a version 7 uuid ready to embed.
type UUID ¶
type UUID string
UUID defines a string wrapper for dealing with UUIDs using the google uuid package for parsing and specific method support. This implementation has been optimized for convenience and JSON conversion as opposed to performance.
Note that this package is not registered inside its own schema. We instead rely on the `"format"` parameter already included in JSON Schema.
Constants for empty and zero states.
func MakeV3
deprecated
added in
v0.69.0
MakeV3 generates a new UUIDv3 using the provided namespace and data. The behavior is deterministic, that is, the same inputs will always generate the same UUID. This is handy to transform any other types of IDs into UUIDs, among other uses.
In UUIDv3, the data is hashed using MD5 which is a performant algorithm, but is subject to collision attacks and other vulnerabilities. If security is a concern, use UUIDv5 instead.
Deprecated: use V3() instead.
func MakeV5
deprecated
added in
v0.69.0
MakeV5 generates a new UUIDv5 using the provided namespace and data. The behavior is deterministic, that is, the same inputs will always generate the same UUID. This is handy to transform any other types of IDs into UUIDs, among other uses.
In UUIDv5, the data is hashed using SHA1, a secure algorithm but slower than MD5. If you don't need the security of SHA1 and performance is a concern, use UUIDv3 instead.
Deprecated: use V5() instead.
func ParseBase64 ¶ added in v0.111.0
ParseBase64 will attempt to decode a Base64 string into a UUID. If the string is already a regular UUID, it will be parsed and returned using the regular Parse method.
func ShouldParse ¶ added in v0.51.0
ShouldParse will return a UUID if the string is valid, otherwise it will provide a zero UUID.
func V1 ¶ added in v0.73.0
func V1() UUID
V1 generates a version 1 UUID. We strongly recommend using V7 now as an alternative as it provides the same functionality with a more secure random node ID.
func V3 ¶ added in v0.73.0
V3 generates a new UUIDv3 using the provided namespace and data. The behavior is deterministic, that is, the same inputs will always generate the same UUID. This is handy to transform any other types of IDs into UUIDs, among other uses.
In UUIDv3, the data is hashed using MD5 which is a performant algorithm, but is subject to collision attacks and other vulnerabilities. If security is a concern, use UUIDv5 instead.
func V5 ¶ added in v0.73.0
V5 generates a new UUIDv5 using the provided namespace and data. The behavior is deterministic, that is, the same inputs will always generate the same UUID. This is handy to transform any other types of IDs into UUIDs, among other uses.
In UUIDv5, the data is hashed using SHA1, a secure algorithm but slower than MD5. If you don't need the security of SHA1 and performance is a concern, use UUIDv3 instead.
func V6 ¶ added in v0.74.0
func V6() UUID
V6 generates a version 6 UUID, a drop-in replaced for V1 UUIDs that uses random data instead of node. It maintains a similar structure to V1 UUIDs with the timestamp, so no ordering is maintained.
func V7 ¶ added in v0.74.0
func V7() UUID
V7 generates a new UUIDv7, a replacement for V1 or V6 UUIDs which combines the Unix timestamp with millisecond precision and random data. An important difference with other versions is that order is maintained, making this a great option for primary keys in databases.
func (UUID) Base64 ¶ added in v0.111.0
Base64 returns a compact URL-safe Base64 string of the UUID as opposed to the regular hex representation. This can be used for prettier and shorter URLs and is completely reversible.
func (UUID) JSONSchemaExtend ¶ added in v0.73.0
func (UUID) JSONSchemaExtend(s *jsonschema.Schema)
JSONSchemaExtend ensures the schema contains the additional UUID format wherever it is used.
func (UUID) Timestamp ¶
Timestamp extracts the time. Anything other than a version 1, 6, or 7 UUID will provide zero time without an error, so ensure your error checks are performed previously.
func (*UUID) UnmarshalText ¶ added in v0.73.0
UnmarshalText will ensure the UUID is always a valid UUID when unmarshalling and just return an empty value if incorrect. TODO: Remove this and instead depend on validation to provide more readable errors.