pulid

package module
v0.0.0-...-168491b Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2023 License: MIT Imports: 7 Imported by: 0

README

pulid

Documentation

Index

Constants

View Source
const EncodedSize = 28

Variables

View Source
var (
	ErrPrefixLength = errors.New("pulid: bad prefix length")

	Nil = PULID{}
)

Functions

This section is empty.

Types

type PULID

type PULID [18]byte

PULID implements a PULID - a prefixed ULID.

func Make

func Make(prefix string) PULID

Make returns a PULID with the current time in Unix milliseconds and monotonically increasing entropy for the same millisecond. It is safe for concurrent use, leveraging a sync.Pool underneath for minimal contention. Panic if prefix is not of length 2.

func MustNew

func MustNew(prefix string, ms uint64, entropy io.Reader) PULID

MustNew is a convenience function equivalent to New that panics on failure instead of returning an error.

func MustNewDefault

func MustNewDefault(prefix string, t time.Time) PULID

MustNewDefault is a convenience function equivalent to MustNew with DefaultEntropy as the entropy. It may panic if the given time.Time is too large or too small. Also panic if prefix is not of length 2.

func MustParse

func MustParse(id string) PULID

MustParse is a convenience function equivalent to Parse that panics on failure instead of returning an error.

func MustParseStrict

func MustParseStrict(id string) PULID

MustParseStrict is a convenience function equivalent to ParseStrict that panics on failure instead of returning an error.

func New

func New(prefix string, ms uint64, entropy io.Reader) (PULID, error)

New returns a PULID with the given Unix milliseconds timestamp and an optional entropy source. Use the Timestamp function to convert a time.Time to Unix milliseconds.

ErrPrefixLength is returned when passing a prefix bigger or smaller than 2.

ErrBigTime is returned when passing a timestamp bigger than MaxTime. Reading from the entropy source may also return an error.

Safety for concurrent use is only dependent on the safety of the entropy source.

func Parse

func Parse(id string) (PULID, error)

Parse parses an encoded PULID, returning an error in case of failure.

ErrDataSize is returned if the len(ulid) is different from an encoded ULID's length. Invalid encodings produce undefined ULIDs. For a version that returns an error instead, see ParseStrict.

func ParseStrict

func ParseStrict(id string) (PULID, error)

ParseStrict parses an encoded PULID, returning an error in case of failure.

It is like Parse, but additionally validates that the parsed ULID consists only of valid base32 characters. It is slightly slower than Parse.

ErrDataSize is returned if the len(ulid) is different from an encoded ULID's length. Invalid encodings return ErrInvalidCharacters.

func (PULID) Bytes

func (id PULID) Bytes() []byte

Bytes returns bytes slice representation of PULID.

func (PULID) Compare

func (id PULID) Compare(other PULID) int

Compare returns an integer comparing id and other lexicographically. The result will be 0 if id==other, -1 if id < other, and +1 if id > other.

func (PULID) Entropy

func (id PULID) Entropy() []byte

Entropy returns the entropy from the ULID.

func (PULID) MarshalBinary

func (id PULID) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface by returning the ULID as a byte slice.

func (PULID) MarshalBinaryTo

func (id PULID) MarshalBinaryTo(dst []byte) error

MarshalBinaryTo writes the binary encoding of the ULID to the given buffer. ErrBufferSize is returned when the len(dst) != 16.

func (PULID) MarshalGQL

func (id PULID) MarshalGQL(w io.Writer)

MarshalGQL implements the graphql.Marshaler interface

func (PULID) MarshalText

func (id PULID) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface by returning the string encoded ULID.

func (PULID) MarshalTextTo

func (id PULID) MarshalTextTo(dst []byte) error

MarshalTextTo writes the ULID as a string to the given buffer. ErrBufferSize is returned when the len(dst) != 26.

func (PULID) Prefix

func (id PULID) Prefix() string

ULID returns ULID from the PULID

func (PULID) PrefixBytes

func (id PULID) PrefixBytes() []byte

ULID returns ULID from the PULID

func (*PULID) Scan

func (id *PULID) Scan(src interface{}) error

Scan implements the sql.Scanner interface. It supports scanning a string or byte slice.

func (*PULID) SetEntropy

func (id *PULID) SetEntropy(e []byte) error

SetEntropy sets the ULID entropy to the passed byte slice. ErrDataSize is returned if len(e) != 10.

func (*PULID) SetTime

func (id *PULID) SetTime(ms uint64) error

SetTime sets the time component of the ULID to the given Unix time in milliseconds.

func (PULID) String

func (id PULID) String() string

String returns a lexicographically sortable string encoded PULID (26 characters, non-standard base 32) e.g. PR01AN4Z07BY79KA1307SR9X4MV3. Format: pptttttttttteeeeeeeeeeeeeeee where p is prefix t is time and e is entropy.

func (PULID) Time

func (id PULID) Time() uint64

Time returns the Unix time in milliseconds encoded in the ULID. Use the top level Time function to convert the returned value to a time.Time.

func (PULID) Timestamp

func (id PULID) Timestamp() time.Time

Timestamp returns the time encoded in the ULID as a time.Time.

func (PULID) ULID

func (id PULID) ULID() ulid.ULID

ULID returns ULID from the PULID

func (*PULID) UnmarshalBinary

func (id *PULID) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface by copying the passed data and converting it to a ULID. ErrDataSize is returned if the data length is different from ULID length.

func (*PULID) UnmarshalGQL

func (id *PULID) UnmarshalGQL(v interface{}) error

UnmarshalGQL implements the graphql.Unmarshaler interface

func (*PULID) UnmarshalText

func (id *PULID) UnmarshalText(v []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface by parsing the data as string encoded ULID.

ErrDataSize is returned if the len(v) is different from an encoded ULID's length. Invalid encodings produce undefined ULIDs.

func (PULID) Value

func (id PULID) Value() (driver.Value, error)

Value implements the sql/driver.Valuer interface, returning the ULID as a

Jump to

Keyboard shortcuts

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