shortuuid

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: MIT Imports: 7 Imported by: 0

README

shortuuid

Note: This library was ported from the original github.com/lithammer/shortuuid/v3 repo This library eschews the Google UUID library and uses the gofrs version

A Go library that generates concise, unambiguous, URL-safe UUIDs. Based on and compatible with the Python library shortuuid.

Often, one needs to use non-sequential IDs in places where users will see them, but the IDs must be as concise and easy to use as possible. shortuuid solves this problem by generating UUIDs using gofrs/uuid and then translating them to base57 using lowercase and uppercase letters and digits, and removing similar-looking characters such as l, 1, I, O and 0.

Usage

package main

import (
    "fmt"

    "github.com/17twenty/shortuuid"
)

func main() {
    u := shortuuid.New() // Cekw67uyMpBGZLRP2HFVbe
}

To use UUID v5 (instead of the default v4), use NewWithNamespace(name string) instead of New().

shortuuid.NewWithNamespace("http://example.com")

It's possible to use a custom alphabet as well, though it has to be 57 characters long.

alphabet := "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxy="
shortuuid.NewWithAlphabet(alphabet) // u=BFWRLr5dXbeWf==iasZi

Bring your own encoder! For example, base58 is popular among bitcoin.

package main

import (
    "fmt"
    "github.com/btcsuite/btcutil/base58"
    "github.com/17twenty/shortuuid"
    "github.com/gofrs/uuid"
)

type base58Encoder struct {}

func (enc base58Encoder) Encode(u uuid.UUID) string {
    return base58.Encode(u.Bytes())
}

func (enc base58Encoder) Decode(s string) (uuid.UUID, error) {
    return uuid.FromBytes(base58.Decode(s))
}

func main() {
    enc := base58Encoder{}
    fmt.Println(shortuuid.NewWithEncoder(enc)) // 6R7VqaQHbzC1xwA5UueGe6
}

License

MIT as the original

Documentation

Index

Constants

View Source
const DefaultAlphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

DefaultAlphabet is the default alphabet used.

Variables

View Source
var (
	NameSpaceDNS  = uuid.Must(uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8"))
	NameSpaceURL  = uuid.Must(uuid.FromString("6ba7b811-9dad-11d1-80b4-00c04fd430c8"))
	NameSpaceOID  = uuid.Must(uuid.FromString("6ba7b812-9dad-11d1-80b4-00c04fd430c8"))
	NameSpaceX500 = uuid.Must(uuid.FromString("6ba7b814-9dad-11d1-80b4-00c04fd430c8"))
)
View Source
var DefaultEncoder = &base57{newAlphabet(DefaultAlphabet)}

DefaultEncoder is the default encoder uses when generating new UUIDs, and is based on Base57.

Functions

func IsUUIDv4 added in v1.0.4

func IsUUIDv4(u uuid.UUID) bool

IsUUIDv4 returns true if the provided uuid matches the v5 schema

func New

func New() string

New returns a new UUIDv4, encoded with base57.

func NewWithAlphabet

func NewWithAlphabet(abc string) string

NewWithAlphabet returns a new UUIDv4, encoded with base57 using the alternative alphabet abc.

func NewWithEncoder added in v1.0.4

func NewWithEncoder(enc Encoder) string

NewWithEncoder returns a new UUIDv4, encoded with enc.

func NewWithNamespace added in v1.0.4

func NewWithNamespace(name string) string

NewWithNamespace returns a new UUIDv5 (or v4 if name is empty), encoded with base57.

Types

type Encoder added in v1.0.4

type Encoder interface {
	Encode(uuid.UUID) string
	Decode(string) (uuid.UUID, error)
}

Encoder is an interface for encoding/decoding UUIDs to strings.

Jump to

Keyboard shortcuts

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