Documentation ¶
Index ¶
- Constants
- Variables
- func NullableIDCompare(a, b NullableID) int
- type ErrInvalidVersion
- type ID
- func IDFrom[T IDSource](val T) ID
- func IDFromAny(val any) (ID, error)
- func IDFromBytes(b []byte) (ID, error)
- func IDFromBytesOrNil(s []byte) ID
- func IDFromPtr(ptr *ID, defaultVal ID) ID
- func IDFromString(s string) (ID, error)
- func IDFromStringOrNil(input string) ID
- func IDMust[T IDSource](val T) ID
- func IDMustFromString(input string) ID
- func IDv1() (id ID)
- func IDv2(domain byte) (id ID)
- func IDv3(ns ID, name string) ID
- func IDv4() (id ID)
- func IDv5(ns ID, name string) ID
- func IDv7() (id ID)
- func (id ID) Base64() string
- func (id ID) Bytes() []byte
- func (id ID) GoString() string
- func (id ID) Hex() string
- func (id ID) IsNil() bool
- func (id ID) IsNotNil() bool
- func (id ID) IsZero() bool
- func (id ID) Less(rhs ID) bool
- func (id ID) MarshalBinary() (data []byte, err error)
- func (id ID) MarshalText() (text []byte, err error)
- func (id ID) Nullable() NullableID
- func (id ID) PrettyPrint(w io.Writer)
- func (id *ID) Scan(src any) error
- func (id *ID) SetVariant()
- func (id *ID) SetVersion(v byte)
- func (id ID) String() string
- func (id ID) StringBytes() []byte
- func (id ID) StringUpper() string
- func (id *ID) UnmarshalBinary(data []byte) (err error)
- func (id *ID) UnmarshalText(text []byte) (err error)
- func (id ID) Valid() bool
- func (id ID) Validate() error
- func (id ID) Value() (driver.Value, error)
- func (id ID) Variant() uint
- func (id ID) Version() uint
- type IDMutex
- type IDSet
- func (s IDSet) Add(id ID)
- func (s IDSet) AddIDs(ids IDs)
- func (s IDSet) AddSet(other IDSet)
- func (set IDSet) AddSlice(s IDSlice)
- func (s IDSet) AsSet() IDSet
- func (s IDSet) AsSlice() IDSlice
- func (s IDSet) AsSortedSlice() IDSlice
- func (s IDSet) Clear()
- func (s IDSet) Clone() IDSet
- func (s IDSet) Contains(id ID) bool
- func (s IDSet) Delete(id ID)
- func (s IDSet) DeleteSet(other IDSet)
- func (s IDSet) DeleteSlice(sl IDSlice)
- func (s IDSet) Diff(other IDSet) IDSet
- func (s IDSet) Equal(other IDSet) bool
- func (s IDSet) ForEach(callback func(ID) error) error
- func (s IDSet) GetOne() ID
- func (s IDSet) IsEmpty() bool
- func (s IDSet) IsNull() bool
- func (s IDSet) Len() int
- func (s IDSet) MarshalJSON() ([]byte, error)
- func (s IDSet) MarshalText() (text []byte, err error)
- func (s IDSet) PrettyPrint(w io.Writer)
- func (s *IDSet) Scan(value any) error
- func (s IDSet) String() string
- func (s *IDSet) UnmarshalJSON(data []byte) error
- func (s *IDSet) UnmarshalText(text []byte) error
- func (s IDSet) Value() (driver.Value, error)
- type IDSlice
- func (s IDSlice) AsSet() IDSet
- func (s IDSlice) AsSlice() IDSlice
- func (s IDSlice) Clone() IDSlice
- func (s IDSlice) Contains(id ID) bool
- func (s IDSlice) ContainsAny(other IDSlice) bool
- func (s IDSlice) ContainsAnyFromSet(set IDSet) bool
- func (s IDSlice) Equal(other IDSlice) bool
- func (s IDSlice) ForEach(callback func(ID) error) error
- func (s IDSlice) IndexOf(id ID) int
- func (s IDSlice) Len() int
- func (s IDSlice) Less(i, j int) bool
- func (s IDSlice) MarshalJSON() ([]byte, error)
- func (s IDSlice) MarshalText() (text []byte, err error)
- func (s IDSlice) PrettyPrint(w io.Writer)
- func (s *IDSlice) RemoveAll(id ID) (count int)
- func (s *IDSlice) RemoveAt(index int)
- func (s *IDSlice) RemoveFirst(id ID) int
- func (s *IDSlice) Scan(value any) (err error)
- func (s IDSlice) Sort()
- func (s IDSlice) SortedClone() IDSlice
- func (s IDSlice) String() string
- func (s IDSlice) Strings() []string
- func (s IDSlice) Swap(i, j int)
- func (s *IDSlice) UnmarshalJSON(data []byte) error
- func (s *IDSlice) UnmarshalText(text []byte) error
- func (s IDSlice) Value() (driver.Value, error)
- type IDSource
- type IDs
- type NullableID
- func NullableIDFromAny(val any) (NullableID, error)
- func NullableIDFromBytes(s []byte) (NullableID, error)
- func NullableIDFromPtr(ptr *ID) NullableID
- func NullableIDFromString(s string) (NullableID, error)
- func NullableIDFromStringOrNull(s string) NullableID
- func NullableIDMust[T IDSource](val T) NullableID
- func (n NullableID) Base64() string
- func (n NullableID) Get() ID
- func (n NullableID) GetOr(defaultID ID) ID
- func (n NullableID) GetOrNil() ID
- func (n NullableID) GoString() string
- func (n NullableID) Hex() string
- func (n NullableID) IsNotNull() bool
- func (n NullableID) IsNull() bool
- func (n NullableID) MarshalBinary() (data []byte, err error)
- func (n NullableID) MarshalJSON() ([]byte, error)
- func (n NullableID) MarshalText() ([]byte, error)
- func (n NullableID) PrettyPrint(w io.Writer)
- func (n NullableID) Ptr() *ID
- func (n *NullableID) Scan(src any) error
- func (n *NullableID) Set(id ID)
- func (n *NullableID) SetNull()
- func (n NullableID) String() string
- func (n NullableID) StringBytes() []byte
- func (n NullableID) StringOr(nullStr string) string
- func (n NullableID) StringUpper() string
- func (n *NullableID) UnmarshalBinary(data []byte) (err error)
- func (n *NullableID) UnmarshalJSON(data []byte) error
- func (n *NullableID) UnmarshalText(text []byte) (err error)
- func (n NullableID) Valid() bool
- func (n NullableID) Validate() error
- func (n NullableID) Value() (driver.Value, error)
- func (n NullableID) Variant() uint
- func (n NullableID) Version() uint
Constants ¶
const ( IDVariantNCS = iota IDVariantRFC4122 IDVariantMicrosoft IDVariantInvalid )
UUID layout variants.
const ( IDDomainPerson = iota IDDomainGroup IDDomainOrg )
UUID DCE domains.
const ( ErrNilID errs.Sentinel = "Nil UUID" ErrInvalidVariant errs.Sentinel = "invalid UUID variant" )
const IDRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
Variables ¶
var ( NamespaceDNS = IDMustFromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8") NamespaceURL = IDMustFromString("6ba7b811-9dad-11d1-80b4-00c04fd430c8") NamespaceOID = IDMustFromString("6ba7b812-9dad-11d1-80b4-00c04fd430c8") NamespaceX500 = IDMustFromString("6ba7b814-9dad-11d1-80b4-00c04fd430c8") )
Predefined namespace UUIDs.
Functions ¶
func NullableIDCompare ¶
func NullableIDCompare(a, b NullableID) int
NullableIDCompare returns bytes.Compare result of a and b.
Types ¶
type ErrInvalidVersion ¶
type ErrInvalidVersion uint
func (ErrInvalidVersion) Error ¶
func (e ErrInvalidVersion) Error() string
type ID ¶
type ID [16]byte
ID is a UUID representation compliant with specification described in RFC 4122.
var IDNil ID
The nil UUID is special form of UUID that is specified to have all 128 bits set to zero: "00000000-0000-0000-0000-000000000000"
func IDFrom ¶
IDFrom converts val to an ID or returns IDNil if no conversion is not possible. The returned ID is not validated.
func IDFromAny ¶
IDFromAny converts val to an ID or returns an error if the conversion is not possible or the ID is not valid. Returns IDNil, ErrNilID when val is nil.
func IDFromBytes ¶
IDFromBytes parses a byte slice as UUID. If the slice has a length of 16, it will be interpred as a binary UUID, if the length is 22, 32, or 36, it will be parsed as string.
func IDFromBytesOrNil ¶
IDFromBytesOrNil parses a byte slice as UUID. Same behavior as IDFromBytes, but returns a Nil UUID on error.
func IDFromString ¶
IDFromString parses a string as ID. The string is expected in a form accepted by UnmarshalText.
func IDFromStringOrNil ¶
NullableIDFromStringOrNull parses a string as UUID, or returns the Nil UUID in case of a parsing error.
func IDMust ¶
IDMust converts val to an ID or panics if the conversion is not possible or the ID is not valid.
func IDMustFromString ¶
IDMustFromString parses a string as ID. Panics if there is an error.
func IDv7 ¶
func IDv7() (id ID)
IDv7 returns an ID with the first 48 bits containing a sortable timestamp and random data after the version and variant information.
func (ID) Base64 ¶
Base64 returns the unpadded base64 URL encoding of the UUID. The returned string is always 22 characters long.
func (ID) GoString ¶
GoString returns a pseudo Go literal for the ID in the format:
uu.IDFrom(`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`)
func (ID) Hex ¶
Hex returns the hex representation without dashes of the UUID The returned string is always 32 characters long.
func (ID) Less ¶
Less returns true if the 128 bit unsigned integer value of the id is less than the passed rhs.
func (ID) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (ID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String.
func (ID) PrettyPrint ¶
PrettyPrint the ID in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. Implements the pretty.Printable interface.
func (*ID) Scan ¶
Scan implements the sql.Scanner interface. A 16-byte slice is handled by UnmarshalBinary, while a longer byte slice or a string is handled by UnmarshalText.
func (*ID) SetVariant ¶
func (id *ID) SetVariant()
SetVariant sets variant bits as described in RFC 4122.
func (ID) String ¶
String returns the canonical string representation of the UUID:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
String implements the fmt.Stringer interface.
func (ID) StringBytes ¶
StringBytes returns the canonical string representation of the UUID as byte slice: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
func (ID) StringUpper ¶
StringUpper returns the upper case version of the canonical string format:
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
func (*ID) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return error if the slice isn't 16 bytes long, but does not check the validity of the UUID.
func (*ID) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. Following formats are supported: `6ba7b8109dad11d180b400c04fd430c8` `6ba7b810-9dad-11d1-80b4-00c04fd430c8` `"6ba7b810-9dad-11d1-80b4-00c04fd430c8"` `{6ba7b810-9dad-11d1-80b4-00c04fd430c8}` `urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c` Surrounding double quotes will be removed before parsing.
func (ID) Valid ¶
Valid returns if Variant and Version of this UUID are supported. A Nil UUID is not valid.
func (ID) Validate ¶
Validate returns an error if the Variant and Version of this UUID are not supported. A Nil UUID is not valid.
type IDMutex ¶
type IDMutex struct {
// contains filtered or unexported fields
}
IDMutex manages a unique mutex for every locked UUID key. The mutex for a key exists as long as there are any locks waiting to be unlocked. This is equivalent to declaring a mutex variable for every key, except that the key and the number of mutexes are dynamic.
func NewIDMutexWithCallbacks ¶
NewIDMutexWithCallbacks returns a new IDMutex where the passed functions are called back as first operation from Lock and Unlock before any any locking or unlocking. Usable for debugging and logging.
type IDSet ¶
type IDSet map[ID]struct{}
IDSet is a set of uu.IDs. It is a map[ID]struct{} underneath. Implements the database/sql.Scanner and database/sql/driver.Valuer interfaces with the nil map value used as SQL NULL
func IDSetFromString ¶
IDSetFromString parses a string created with IDSet.String()
func IDSetMust ¶
IDSetMust converts the passed values to an IDSet or panics if that's not possible or an ID is not valid. Returns nil if zero values are passed.
func MakeIDSetFromStrings ¶
MakeIDSetFromStrings returns an IDSet with strs parsed as IDs
func MakeIDSetMustFromStrings ¶
MakeIDSetMustFromStrings returns an IDSet with the passed strings as IDs or panics if there was an error.
func (IDSet) AsSortedSlice ¶
AsSortedSlice returns the IDs of the set as sorted IDSlice.
func (IDSet) Contains ¶
Contains returns true if the set contains the passed id. It is valid to call this method on a nil IDSet.
func (IDSet) DeleteSlice ¶
func (IDSet) ForEach ¶
ForEach calls the passed function for each ID. Any error from the callback function is returned by ForEach immediatly. Returning a sentinel error is a way to stop the loop with a known cause that might not be a real error.
func (IDSet) GetOne ¶
GetOne returns one ID in undefined order from the set or IDNil if the set is empty. Most useful to get the only ID in a set of size one.
func (IDSet) IsNull ¶
IsNull implements the nullable.Nullable interface by returning true if the set is nil.
func (IDSet) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler
func (IDSet) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface
func (IDSet) PrettyPrint ¶
PrettyPrint using s.AsSortedSlice().PrettyPrint(w). Implements the pretty.Printable interface.
func (*IDSet) Scan ¶
Scan implements the database/sql.Scanner interface with the nil map value used as SQL NULL. Id does assign a new IDSet to *set instead of modifying the existing map, so it can be used with uninitialized IDSet variable.
func (*IDSet) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler Id does assign a new IDSet to *set instead of modifying the existing map, so it can be used with uninitialized IDSet variable.
func (*IDSet) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface
type IDSlice ¶
type IDSlice []ID
IDSlice is a slice of uu.IDs. It is a []ID underneath. Implements the database/sql.Scanner and database/sql/driver.Valuer interfaces. Implements the encoding/json.Marshaler and Unmarshaler interfaces. with the nil slice value used as SQL NULL and JSON null.
func IDSliceFromString ¶
IDSliceFromString parses a string created with IDSlice.String()
func IDSliceFromStrings ¶
IDSliceFromStrings parses an IDSlice from strings
func IDSliceMust ¶
IDSliceMust converts the passed values to an IDSlice or panics if that's not possible or an ID is not valid. Returns nil if zero values are passed.
func IDSliceMustFromStrings ¶
IDSliceMustFromStrings parses an IDSlice from strings and panics in case of an error.
func (IDSlice) ContainsAny ¶
func (IDSlice) ContainsAnyFromSet ¶
func (IDSlice) ForEach ¶
ForEach calls the passed function for each ID. Any error from the callback function is returned by ForEach immediatly. Returning a sentinel error is a way to stop the loop with a known cause that might not be a real error.
func (IDSlice) IndexOf ¶
IndexOf returns the index of the first occurrence of id in the slice, or -1 if id was not found.
func (IDSlice) Len ¶
Len is the number of elements in the collection. One of the methods to implement sort.Interface.
func (IDSlice) Less ¶
Less reports whether the element with index i should sort before the element with index j. One of the methods to implement sort.Interface.
func (IDSlice) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler
func (IDSlice) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface
func (IDSlice) PrettyPrint ¶
PrettyPrint using IDSlice.String(). Implements the pretty.Printable interface.
func (*IDSlice) RemoveAll ¶
RemoveAll removes the all occurrences of id from the slice and returns the count of removals.
func (*IDSlice) RemoveAt ¶
RemoveAt removes the slice element at the given index. Will panic if the index is out of range.
func (*IDSlice) RemoveFirst ¶
RemoveFirst removes the first occurrence of id from the slice and returns its index or -1 if id was not found in the slice.
func (*IDSlice) Scan ¶
Scan implements the database/sql.Scanner interface with the nil map value used as SQL NULL. Does *s = make(Slice) if *s == nil so it can be used with an not initialized Slice variable
func (IDSlice) SortedClone ¶
SortedClone returns a sorted clone of the slice.
func (IDSlice) Swap ¶
Swap swaps the elements with indexes i and j. One of the methods to implement sort.Interface.
func (*IDSlice) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler
func (*IDSlice) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface by calling IDSliceFromString.
type IDs ¶
type IDs interface { fmt.Stringer pretty.Printable driver.Valuer json.Marshaler // Len returns the length of the ID collection. Len() int // AsSet returns the contained IDs as IDSet. AsSet() IDSet // AsSlice returns the contained IDs as IDSlice. AsSlice() IDSlice // ForEach calls the passed function for each ID. // Any error from the callback function is returned // by ForEach immediatly. // Returning a sentinel error is a way to stop the loop // with a known cause that might not be a real error. ForEach(func(ID) error) error }
IDs is an interface implemented by IDSet and IDSlice as abstract collection of IDs. It is intended for passing IDs as input arguments, but does not support scanning or unmarshalling.
type NullableID ¶
type NullableID [16]byte
NullableID is a UUID where the Nil UUID "00000000-0000-0000-0000-000000000000" is interpreted as the null values for SQL and JSON.
var IDNull NullableID
func NullableIDFromAny ¶
func NullableIDFromAny(val any) (NullableID, error)
NullableIDFromAny converts val to an ID or returns an error if the conversion is not possible or the ID is not valid. Returns IDNull and no error when val is nil. The Nil UUID "00000000-0000-0000-0000-000000000000" is interpreted as NULL.
func NullableIDFromBytes ¶
func NullableIDFromBytes(s []byte) (NullableID, error)
NullableIDFromBytes parses a byte slice as UUID. The Nil UUID "00000000-0000-0000-0000-000000000000" is interpreted as NULL.
func NullableIDFromPtr ¶
func NullableIDFromPtr(ptr *ID) NullableID
NullableIDFromPtr returns the passed ID as NullableID if the ptr is not nil, or returns IDNull in case of a nil ptr. The Nil UUID "00000000-0000-0000-0000-000000000000" is interpreted as NULL.
func NullableIDFromString ¶
func NullableIDFromString(s string) (NullableID, error)
NullableIDFromString parses a string as NullableID. The Nil UUID "00000000-0000-0000-0000-000000000000" is interpreted as NULL.
func NullableIDFromStringOrNull ¶
func NullableIDFromStringOrNull(s string) NullableID
NullableIDFromStringOrNull parses a string as UUID, or returns IDNull in case of a parsing error. The Nil UUID "00000000-0000-0000-0000-000000000000" is interpreted as NULL.
func NullableIDMust ¶
func NullableIDMust[T IDSource](val T) NullableID
NullableIDMust converts val to an ID or panics if that's not possible or the ID is not valid. Supported types are string, []byte, [16]byte, ID, NullableID, and nil. The Nil UUID "00000000-0000-0000-0000-000000000000" is interpreted as NULL.
func (NullableID) Base64 ¶
func (n NullableID) Base64() string
Base64 returns the unpadded base64 URL encoding of the UUID. The returned string is always 22 characters long.
func (NullableID) Get ¶
func (n NullableID) Get() ID
Get returns the non nullable ID value or panics if the NullableID is null. Note: check with IsNull before using Get!
func (NullableID) GetOr ¶
func (n NullableID) GetOr(defaultID ID) ID
GetOr returns the non nullable ID value or defaultID if the NullableID is null.
func (NullableID) GetOrNil ¶
func (n NullableID) GetOrNil() ID
GetOrNil returns the non nullable ID value or the Nil UUID if n is null. Use Get to ensure getting a non Nil UUID or panic.
func (NullableID) GoString ¶
func (n NullableID) GoString() string
GoString returns a pseudo Go literal for the ID in the format:
uu.NullableIDMust(`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`)
func (NullableID) Hex ¶
func (n NullableID) Hex() string
Hex returns the hex representation without dashes of the UUID The returned string is always 32 characters long.
func (NullableID) IsNotNull ¶
func (n NullableID) IsNotNull() bool
IsNotNull returns true if the NullableID is not null.
func (NullableID) IsNull ¶
func (n NullableID) IsNull() bool
IsNull returns true if the NullableID is null. IsNull implements the nullable.Nullable interface.
func (NullableID) MarshalBinary ¶
func (n NullableID) MarshalBinary() (data []byte, err error)
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (NullableID) MarshalJSON ¶
func (n NullableID) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (NullableID) MarshalText ¶
func (n NullableID) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler. It will encode a blank string when this String is null.
func (NullableID) PrettyPrint ¶
func (n NullableID) PrettyPrint(w io.Writer)
PrettyPrint the NullableID in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx or as NULL. Implements the pretty.Printable interface.
func (NullableID) Ptr ¶
func (n NullableID) Ptr() *ID
Ptr returns a pointer to this NullableID's value, or a nil pointer if this NullableID is null.
func (*NullableID) Scan ¶
func (n *NullableID) Scan(src any) error
Scan implements the sql.Scanner interface.
func (NullableID) String ¶
func (n NullableID) String() string
String returns the ID as string or "NULL"
func (NullableID) StringBytes ¶
func (n NullableID) StringBytes() []byte
StringBytes returns the canonical string representation of the UUID as byte slice: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
func (NullableID) StringOr ¶
func (n NullableID) StringOr(nullStr string) string
StringOr returns the ID as string or the passed nullStr
func (NullableID) StringUpper ¶
func (n NullableID) StringUpper() string
StringUpper returns the upper case version of the canonical string format, or "NULL".
func (*NullableID) UnmarshalBinary ¶
func (n *NullableID) UnmarshalBinary(data []byte) (err error)
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return error if the slice isn't 16 bytes long, but does not check the validity of the UUID.
func (*NullableID) UnmarshalJSON ¶
func (n *NullableID) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler. It supports string and null input. Blank string input does not produce a null ID. It also supports unmarshalling a sql.NullString.
func (*NullableID) UnmarshalText ¶
func (n *NullableID) UnmarshalText(text []byte) (err error)
UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null String if the input is a blank string.
func (NullableID) Valid ¶
func (n NullableID) Valid() bool
Valid returns if Variant and Version of this UUID are supported. A Nil UUID is also valid.
func (NullableID) Validate ¶
func (n NullableID) Validate() error
Validate returns an error if the Variant and Version of this UUID are not supported. A Nil UUID is also valid.
func (NullableID) Value ¶
func (n NullableID) Value() (driver.Value, error)
Value implements the driver.Valuer interface.
func (NullableID) Variant ¶
func (n NullableID) Variant() uint
Variant returns an ID layout variant or IDVariantInvalid if unknown.
func (NullableID) Version ¶
func (n NullableID) Version() uint
Version returns algorithm version used to generate UUID.