util

package
v1.0.26 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 37 Imported by: 0

Documentation

Overview

Package shortid enables the generation of short, unique, non-sequential and by default URL friendly Ids. The package is heavily inspired by the node.js https://github.com/dylang/shortid library.

Id Length

The standard Id length is 9 symbols when generated at a rate of 1 Id per millisecond, occasionally it reaches 11 (at the rate of a few thousand Ids per millisecond) and very-very rarely it can go beyond that during continuous generation at full throttle on high-performant hardware. A test generating 500k Ids at full throttle on conventional hardware generated the following Ids at the head and the tail (length > 9 is expected for this test):

-NDveu-9Q
iNove6iQ9J
NVDve6-9Q
VVDvc6i99J
NVovc6-QQy
VVoveui9QC
...
tFmGc6iQQs
KpTvcui99k
KFTGcuiQ9p
KFmGeu-Q9O
tFTvcu-QQt
tpTveu-99u

Life span

The package guarantees the generation of unique Ids with zero collisions for 34 years (1/1/2016-1/1/2050) using the same worker Id within a single (although concurrent) application if application restarts take longer than 1 millisecond. The package supports up to 32 works, all providing unique sequences.

Implementation details

Although heavily inspired by the node.js shortid library this is not a simple Go port. In addition it

  • is safe to concurrency;
  • does not require any yearly version/epoch resets;
  • provides stable Id size over a long period at the rate of 1ms;
  • guarantees no collisions (due to guaranteed fixed size of Ids between milliseconds and because multiple requests within the same ms lead to longer Ids with the prefix unique to the ms);
  • supports 32 over 16 workers.

The algorithm uses less randomness than the original node.js implementation, which permits to extend the life span as well as reduce and guarantee the length. In general terms, each Id has the following 3 pieces of information encoded: the millisecond (first 8 symbols), the worker Id (9th symbol), running concurrent counter within the same millisecond, only if required, over all remaining symbols. The element of randomness per symbol is 1/2 for the worker and the millisecond and 0 for the counter. Here 0 means no randomness, i.e. every value is encoded using a 64-base alphabet; 1/2 means one of two matching symbols of the supplied alphabet, 1/4 one of four matching symbols. The original algorithm of the node.js module uses 1/4 throughout.

All methods accepting the parameters that govern the randomness are exported and can be used to directly implement an algorithm with e.g. more randomness, but with longer Ids and shorter life spans.

Index

Examples

Constants

View Source
const DefaultABC = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"

DefaultABC is the default URL-friendly alphabet.

View Source
const Version = 1.1

Version defined the library version.

Variables

View Source
var IsAlphanumeric = regexp.MustCompile(`^[0-9a-zA-Z]+$`).MatchString

Functions

func All

func All[T any](set []T) (subsets [][]T)

All returns all combinations for a given string array. This is essentially a powerset of the given set except that the empty set is disregarded.

Example
combinations := All([]string{"A", "B", "C"})
fmt.Println(combinations)
Output:

[[A] [B] [A B] [C] [A C] [B C] [A B C]]

func Combinations

func Combinations[T any](set []T, n int) (subsets [][]T)

Combinations returns combinations of n elements for a given string array. For n < 1, it equals to All and returns all combinations.

func ConvertBigIntFromString

func ConvertBigIntFromString(v0, v1 string) (n0 *big.Int, n1 *big.Int, err error)

func ConvertFloat64ToTokenAmount

func ConvertFloat64ToTokenAmount(amount float64, decimals int) *big.Int

func ConvertHexToDecimalInStringFormat

func ConvertHexToDecimalInStringFormat(hexString string) string

func ConvertTokenAmountToFloat64

func ConvertTokenAmountToFloat64(amt string, tokenDecimal int32) float64

func CreateTransactionOpts

func CreateTransactionOpts(client *ethclient.Client, key *ecdsa.PrivateKey, chainId uint64, caller common.Address) (opts *bind.TransactOpts, err error)

func DecodeInputData

func DecodeInputData(abiStr string, inputData string) map[string]interface{}

func EncodeAddress

func EncodeAddress(addr common.Address) []byte

func EncodeBigInt

func EncodeBigInt(b *big.Int) []byte

func EncodeBytesString

func EncodeBytesString(v string) []byte

func EncodePacked

func EncodePacked(input ...[]byte) []byte

func EncodeUint256

func EncodeUint256(v string) []byte

func EncodeUint256Array

func EncodeUint256Array(arr []string) []byte

func GenerateIncreaseID

func GenerateIncreaseID() (int64, error)

func GenerateShortId

func GenerateShortId() (string, error)

Generate generates an Id using the default generator.

func GetEthCallPostData

func GetEthCallPostData(to string, data string) string

func GetHTTPClient

func GetHTTPClient() *http.Client

func GetRandomNumber

func GetRandomNumber(min, max int) (int, error)

gen min<= a <= max

func HTTPReq

func HTTPReq(method string, url string, httpClient *http.Client, content []byte, headers map[string]string) (body []byte, err error)

HTTPReq ..

func InTransaction

func InTransaction(db *xorm.Engine, callback dbTransactionFunc) error

func InitLog

func InitLog(logPath, logLever string)

func IsAnAddress

func IsAnAddress(address string) bool

func IsValidTxHash

func IsValidTxHash(txHash string) bool

func Logger

func Logger() *zap.Logger

func MustGenerateShortId

func MustGenerateShortId() string

MustGenerate acts just like Generate, but panics instead of returning errors.

func Pad

func Pad(s string, n int, r rune) (string, error)

Pad left-pads s with spaces, to length n. If n is smaller than s, Pad is a no-op.

func PadChar

func PadChar(s string, n int, r rune) (string, error)

PadChar left-pads s with the rune r, to length n. If n is smaller than s, PadChar is a no-op.

func PadLeft

func PadLeft(str, pad string, length int) string

func PrivateToAddress

func PrivateToAddress(key string) (string, error)

func RemoveIndex

func RemoveIndex[T any](s []T, index int) []T

func SetDefault

func SetDefault(sid *Shortid)

SetDefault overwrites the default generator.

func TxWaitToSync

func TxWaitToSync(ctx context.Context, client *ethclient.Client, tx *types.Transaction) (*types.Receipt, bool, error)

Types

type Abc

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

Abc represents a shuffled alphabet used to generate the Ids and provides methods to encode data.

func MustNewAbc

func MustNewAbc(alphabet string, seed uint64) Abc

MustNewAbc acts just like NewAbc, but panics instead of returning errors.

func NewAbc

func NewAbc(alphabet string, seed uint64) (Abc, error)

NewAbc constructs a new instance of shuffled alphabet to be used for Id representation.

func (Abc) Alphabet

func (abc Abc) Alphabet() string

Alphabet returns the alphabet used as an immutable string.

func (*Abc) Encode

func (abc *Abc) Encode(val, nsymbols, digits uint) ([]rune, error)

Encode encodes a given value into a slice of runes of length nsymbols. In case nsymbols==0, the length of the result is automatically computed from data. Even if fewer symbols is required to encode the data than nsymbols, all positions are used encoding 0 where required to guarantee uniqueness in case further data is added to the sequence. The value of digits [4,6] represents represents n in 2^n, which defines how much randomness flows into the algorithm: 4 -- every value can be represented by 4 symbols in the alphabet (permitting at most 16 values), 5 -- every value can be represented by 2 symbols in the alphabet (permitting at most 32 values), 6 -- every value is represented by exactly 1 symbol with no randomness (permitting 64 values).

func (*Abc) MustEncode

func (abc *Abc) MustEncode(val, size, digits uint) []rune

MustEncode acts just like Encode, but panics instead of returning errors.

func (Abc) String

func (abc Abc) String() string

String returns a string representation of the Abc instance.

type CycledStringArray

type CycledStringArray struct {
	Strs   []string
	CurIdx int
}

func CreateCycledStringArray

func CreateCycledStringArray(strs []string) *CycledStringArray

func (*CycledStringArray) GetCurrentString

func (csa *CycledStringArray) GetCurrentString() string

type EthCallResult

type EthCallResult struct {
	Jsonrpc string `json:"jsonrpc"`
	Result  string `json:"result"`
	ID      int    `json:"id"`
}

func ReadContract

func ReadContract(url string, postdata string) (*EthCallResult, error)

type Shortid

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

Shortid type represents a short Id generator working with a given alphabet.

func GetDefault

func GetDefault() *Shortid

GetDefault retrieves the default short Id generator initialised with the default alphabet, worker=0 and seed=1. The default can be overwritten using SetDefault.

func MustNew

func MustNew(worker uint8, alphabet string, seed uint64) *Shortid

MustNew acts just like New, but panics instead of returning errors.

func New

func New(worker uint8, alphabet string, seed uint64) (*Shortid, error)

New constructs an instance of the short Id generator for the given worker number [0,31], alphabet (64 unique symbols) and seed value (to shuffle the alphabet). The worker number should be different for multiple or distributed processes generating Ids into the same data space. The seed, on contrary, should be identical.

func (*Shortid) Abc

func (sid *Shortid) Abc() Abc

Abc returns the instance of alphabet used for representing the Ids.

func (*Shortid) Epoch

func (sid *Shortid) Epoch() time.Time

Epoch returns the value of epoch used as the beginning of millisecond counting (normally 2016-01-01 00:00:00 local time)

func (*Shortid) Generate

func (sid *Shortid) Generate() (string, error)

Generate generates a new short Id.

func (*Shortid) GenerateInternal

func (sid *Shortid) GenerateInternal(tm *time.Time, epoch time.Time) (string, error)

GenerateInternal should only be used for testing purposes.

func (*Shortid) MustGenerate

func (sid *Shortid) MustGenerate() string

MustGenerate acts just like Generate, but panics instead of returning errors.

func (*Shortid) String

func (sid *Shortid) String() string

String returns a string representation of the short Id generator.

func (*Shortid) Worker

func (sid *Shortid) Worker() uint

Worker returns the value of worker for this short Id generator.

Jump to

Keyboard shortcuts

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