uuid

package module
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2023 License: MIT Imports: 5 Imported by: 0

README

go-uuid

CI

A package providing UUID v1, v4 and OrderedUUID with utility interface and function.

What is an OrderedUUID?

It is a UUID v1 where the timestamp is positioned at the first part of the uuid. It is useful as a primary key or index in a database like MySQL. This way, it can be sorted by timestamp, and new elements should be added on top of the index.

Utility function?

Yes, the UUID struct has a Scan/Value function that follows the driver.Valuer / driver.Scanner interface. This allows you to scan your struct with a UUID right from the database! Moreover, it is implementing the encoding.TextUnmarshaler and the encoding.TextMarshaler for encoding and decoding features.

Supported UUID formats for decoding
   "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
   "6ba7b8109dad11d180b400c04fd430c8"
Supported UUID text representations
   uuid := canonical | hashlike
   plain := canonical | hashlike
   canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct
   hashlike := 12hexoct

Documentation

Index

Constants

View Source
const UUIDSize = 16

UUIDSize is the size of the uuid

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyUUID

type AnyUUID struct{}

AnyUUID For easy UUID matching for mocks and tests

func (AnyUUID) Match

func (a AnyUUID) Match(v driver.Value) bool

Match if the UUID is an array of 16 bytes

type UUID

type UUID struct {
	Bytes [UUIDSize]byte
	Valid bool
}

OrderedUUID is an UUIDv1 with some bit shifted for database index performance

func FromBytes

func FromBytes(input [16]byte) UUID

FromBytes returns an OrderedUUID parsed from the byte array.

func FromString

func FromString(input string) (u UUID, err error)

FromString returns an OrderedUUID parsed from string input. Input is expected in a form accepted by UnmarshalText.

func FromUUIDv1

func FromUUIDv1(uuid uuid.UUID) UUID

FromUUIDv1 populates Byte from uuidV1 and converts it

func NewOrdered

func NewOrdered() UUID

NewOrdered creates a new UUIDv1 and converts it to an OrderedUUID

v1 UUID: aaaabbbb-cccc-dddd-1234-567890123456
transposed: ddddcccc-aaaa-bbbb-1234-567890123456

func NewOrderedUUID

func NewOrderedUUID() UUID

DEPRECATED, use NewOrdered instead

func NewV1

func NewV1() UUID

func NewV4

func NewV4() UUID

func (UUID) MarshalText

func (u UUID) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String.

func (*UUID) Scan

func (u *UUID) Scan(src interface{}) error

Scan implements the sql.Scanner interface.

func (*UUID) String

func (u *UUID) String() string

String returns the string representation of the UUID

func (*UUID) UnmarshalText

func (u *UUID) UnmarshalText(text []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaler interface. Following formats are supported:

"6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"6ba7b8109dad11d180b400c04fd430c8"

Supported UUID text representation follows:

uuid := canonical | hashlike
plain := canonical | hashlike
canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct
hashlike := 12hexoct

func (UUID) Value

func (u UUID) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type UUIDArray

type UUIDArray []UUID

func (*UUIDArray) Scan

func (a *UUIDArray) Scan(val interface{}) error

func (UUIDArray) Value

func (a UUIDArray) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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