Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Setup ¶
func Setup(init GeneratorInit)
Setup initializes the generator.
sleekid.Setup(sleekid.GeneratorInit{
ChecksumToken: 12345,
})
func Validate ¶
Validate checks if the given the timestamp and random part is valid.
sleekid.Validate(id)
func ValidateWithPrefix ¶
ValidateWithPrefix checks if the given id is valid with the specific prefix..
sleekid.ValidateWithPrefix("usr", id)
Types ¶
type GenerateOption ¶
type GenerateOption struct {
// RandomDigitsLength is the length of the random part of the id.
RandomDigitsLength int
}
func WithRandomBytes ¶
func WithRandomBytes(length int) *GenerateOption
WithRandomBytes is a helper function to set the RandomDigitsLength option.
type Generator ¶
type Generator interface {
// New generates a new id with the given prefix.
//
// gen := NewGenerator(GeneratorInit{...})
// id, err := gen.New("usr")
// id, err := gen.New("usr", WithRandomBytes(16))
New(prefix string, options ...*GenerateOption) (SleekId, error)
// Prefix returns the prefix of the given id.
//
// gen := NewGenerator(GeneratorInit{...})
// prefix := gen.Prefix(id)
Prefix(id SleekId) string
// Timestamp returns the unix time of the timestamp part.
//
// gen := NewGenerator(GeneratorInit{...})
// timestamp := gen.Timestamp(id)
Timestamp(id SleekId) time.Time
// Validate checks if the given the timestamp and random part is valid.
//
// gen := NewGenerator(GeneratorInit{...})
// valid := gen.Validate(id)
Validate(id SleekId) bool
// ValidateWithPrefix checks if the given id is valid with the specific prefix.
//
// gen := NewGenerator(GeneratorInit{...})
// valid := gen.ValidateWithPrefix("usr", id)
ValidateWithPrefix(prefix string, id SleekId) bool
}
func NewGenerator ¶
func NewGenerator(init GeneratorInit) Generator
type GeneratorInit ¶
type GeneratorInit struct {
// delimiter is the character used to separate the prefix from the rest of the id.
//
// Default is "_".
Delimiter rune
// checksumToken is used to verify the id. Don't expose it to the public.
//
// Default is 4567890. Change it on your production environment.
ChecksumToken uint64
// ChecksumLength is the length of the checksum part of the id.
// This will increase the precisition of the false detection rate.
//
// the probability of false detection is 1 - 1/62^ChecksumLength.
// 2 is enough for most cases. It's 99.97%.
//
// Default is 2.
ChecksumLength int
// RandomDigitsLength is the length of the random part of the id.
// Also you can customize this length when you call New() with WithRandomBytes().
//
// Default is 12.
RandomDigitsLength int
// TimestampLength is the length of the timestamp part of the id.
//
// Default is 5.
// must be 4 <= TimestampLength <= 6.
TimestampLength int
// TimestampOrder is the order of the timestamp part of the id that sleekid generates.
//
// Default is Alphabetical order.
TimestampOrder TimestampOrder
}
type SleekId ¶
type SleekId []byte
type TimestampOrder ¶ added in v2.1.0
type TimestampOrder int
const ( TimestampOrderAlphabetical TimestampOrder = 0 TimestampOrderASCII TimestampOrder = 1 )
Click to show internal directories.
Click to hide internal directories.
