uuid

package
v0.70.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2024 License: Apache-2.0 Imports: 8 Imported by: 5

Documentation

Overview

Package uuid provides a wrapper for handling UUID codes.

Index

Constants

This section is empty.

Variables

View Source
var (
	// 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 */}
	// IsNotZero confirms the UUID is not zero
	IsNotZero = versionRule{/* contains filtered or unexported fields */}
)

Functions

func NodeID

func NodeID() string

NodeID returns the hex representation of the current host bytes

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 indending to generate V1 UUIDs to get a node ID that won't change.

func Within

func Within(ttl time.Duration) validation.Rule

Within is a validation method that can be used to determine if the UUID corresponds do UUIDv1 standard and was timestamped within the acceptable time to live from now. If time checks are enabled, future UUIDs will not be allowed, this could be a problem.

Types

type UUID

type UUID struct {
	uuid.UUID
}

UUID defines our wrapper for dealing with UUIDs

func MakeV1 added in v0.28.0

func MakeV1() UUID

MakeV1 generates a version 1 UUID.

func MakeV3 added in v0.69.0

func MakeV3(space UUID, data []byte) UUID

MakeV3 generates a new UUIDv3 using the provided namespace and data. The behaviour 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 it's subject to collision attacks and other vulnerabilities. If security is a concern, use UUIDv5 instead.

func MakeV4 added in v0.28.0

func MakeV4() UUID

MakeV4 generates a new completely random UUIDv4.

func MakeV5 added in v0.69.0

func MakeV5(space UUID, data []byte) UUID

MakeV5 generates a new UUIDv5 using the provided namespace and data. The behaviour 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 perfomance is a concern, use UUIDv3 instead.

func MustParse added in v0.17.0

func MustParse(s string) UUID

MustParse will panic if the UUID does not look good.

func NewV1

func NewV1() *UUID

NewV1 generates a version 1 UUID.

func NewV3 added in v0.69.0

func NewV3(space UUID, data []byte) *UUID

NewV3 creates a new MD5 UUID using the provided namespace and data. See MakeV3 for more details.

func NewV4

func NewV4() *UUID

NewV4 creates a pointer a new completely random UUIDv4.

func NewV5 added in v0.69.0

func NewV5(space UUID, data []byte) *UUID

NewV5 creates a new SHA1 UUID using the provided namespace and data. See MakeV5 for more details.

func Normalize added in v0.67.10

func Normalize(u *UUID) *UUID

Normalize looks at the provided UUID and tries to return a consistent value, which may be nil. Only works with pointers to UUID.

func Parse

func Parse(s string) (UUID, error)

Parse decodes s into a UUID or provides an error.

func ShouldParse added in v0.51.0

func ShouldParse(s string) UUID

ShouldParse will return a UUID if the string is valid, otherwise it will provide a zero UUID.

func (*UUID) IsZero

func (u *UUID) IsZero() bool

IsZero returns true if the UUID is all zeros.

func (UUID) JSONSchema added in v0.17.0

func (UUID) JSONSchema() *jsonschema.Schema

JSONSchema returns the jsonschema schema object for the UUID.

func (UUID) Timestamp

func (u UUID) Timestamp() time.Time

Timestamp extracts the time. Anything other than a v1 UUID will provide zero time without an error, so ensure your error checks are performed previously.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL