ulid

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package ulid provides a Universally Unique Lexicographically Sortable Identifier (ULID) implementation in Go.

Index

Examples

Constants

View Source
const (
	// Encodingis Crockford's Base32
	// https://www.crockford.com/base32.html
	Encoding = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"

	// Size is the length of a ULID in bytes.
	Size = len(ULID{})

	// EncodedSize is the length of a ULID in chars.
	EncodedSize = (Size*8 + 4) / 5
)

Variables

View Source
var (
	// ErrTimeOverflow is returned if a timestamp >= max time is provided to MonotonicReader.
	ErrTimeOverflow = errors.New("ulid: timestamp overflows max time")

	// ErrNegativeTime is returned if a timestamp predating the previous is provided to MonotonicReader.
	ErrNegativeTime = errors.New("ulid: timestamp predates previous")

	// ErrMonotonicOverflow is returned when incrementing previous ULID's entropy bytes would result
	// in an entropy overflow. The solution is to call .Next() again with a later timestamp.
	ErrMonotonicOverflow = errors.New("ulid: monotonic entropy overflow")
)
View Source
var (
	// ErrInvalidLength is returned when the length of the ULID is invalid.
	ErrInvalidLength = errors.New("ulid: invalid length")

	// ErrInvalidChar is returned when the char is invalid.
	ErrInvalidChar = errors.New("ulid: invalid character")

	// ErrInvalidULID is returned when the ULID is invalid.
	ErrInvalidULID = errors.New("ulid: invalid ULID")

	// ErrDataSize is returned when the data size is invalid.
	ErrDataSize = errors.New("ulid: invalid data size")
)

Functions

func MaxTime

func MaxTime() int64

MaxTime returns the max time component of the ULID.

func Now

func Now() int64

Now returns a ULID with the given time.

func ReverseTime

func ReverseTime(rms int64) int64

ReverseTime returns the reverse time component of the ULID.

func SetSource

func SetSource(src io.Reader) error

SetSource updates the global MonotonicReader's source. Note that the default MonotonicReader source is cryptographically safe (uses "crypto/rand.Reader").

Types

type MonotonicReader

type MonotonicReader struct {
	// contains filtered or unexported fields
}

MonotonicReader wraps a reader to provide montonically increasing ULID values.

func NewMonotonicReader

func NewMonotonicReader(src io.Reader) (*MonotonicReader, error)

NewMonotonicReader returns a new instance for given entropy source and read buffer size. Note that 'src' MUST yield random bytes or else monotonic reads are not guaranteed to terminate, since there may not be enough entropy to compute a monotonic increment.

func (*MonotonicReader) Next

func (r *MonotonicReader) Next(ts int64, dst *ULID) error

Next calculates next available ULID for given Timestamp.

func (*MonotonicReader) NextReverse

func (r *MonotonicReader) NextReverse(ts int64, dst *ULID) error

NextReverse calculates next available ULID for given Timestamp.

type ULID

type ULID [16]byte

ULID is a 128-bit Universally Unique Lexicographically Sortable Identifier.

Example
package main

import (
	"fmt"

	"github.com/katallaxie/pkg/ulid"
)

func main() {
	fmt.Println(ulid.MustNew())
}
Output:

func Max

func Max() ULID

Max returns the max ULID.

func MustNew

func MustNew() ULID

MustNew returns a new ULID.

func MustNewReverse

func MustNewReverse() ULID

MustNewReverse returns a new ULID with reverse time.

func New

func New() (ULID, error)

New returns a new ULID.

func NewReverse

func NewReverse() (ULID, error)

NewReverse returns a new ULID with reverse time.

func Parse

func Parse(data []byte) (ULID, error)

Parse parses a ULID from a byte slice.

func ParseString

func ParseString(s string) (ULID, error)

ParseString parses a ULID from a string.

func (ULID) AppendFormat

func (u ULID) AppendFormat(dst []byte) []byte

AppendFormat append text encoded ULID to 'dst'.

func (ULID) Bytes

func (u ULID) Bytes() []byte

Bytes returns text encoded bytes of receiving ULID.

func (ULID) MarshalBinary

func (u ULID) MarshalBinary() ([]byte, error)

MarshalBinary encodes a ULID into binary form.

func (ULID) MarshalBinaryTo

func (u ULID) MarshalBinaryTo(data []byte) error

MarshalBinaryTo encodes a ULID into binary form.

func (ULID) MarshalText

func (u ULID) MarshalText() ([]byte, error)

MarshalText encodes a ULID into text form by implementing encoding.TextMarshaler.

func (ULID) String

func (u ULID) String() string

String returns the string representation of the ULID.

func (ULID) Time

func (u ULID) Time() int64

Time returns the time component of the ULID.

func (*ULID) UnmarshalBinary

func (u *ULID) UnmarshalBinary(b []byte) error

UnmarshalBinary decodes a ULID from binary form.

func (*ULID) UnmarshalText

func (u *ULID) UnmarshalText(b []byte) error

UnmarshalText decodes a ULID from text form.

Jump to

Keyboard shortcuts

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