util

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

Snowflake Generator

package snowflakeexample

import (
	"fmt"

	"clodeo.tech/platform/go-universe/pkg/util"
)

func InitSnowflakeExample() {
	pod, err := util.NewPOD(&util.SnowflakeOpts{
		Epoch: 1288834974657, //Set in config
		POD:   1,            //Set in config
	})

	if err != nil {
		//Handler error should be fatal or else
		fmt.Errorf(err.Error())
	}

	//Generate POD id
	id := pod.Generate()

	fmt.Printf("Int64    : %#v", id.Int64())    //31677837479936
	fmt.Printf("String   : %#v", id.String())   //"31677837479936"
	fmt.Printf("Base2    : %#v", id.Base2())    //"111001100111110010010010000000001000000000000"
	fmt.Printf("Base32   : %#v", id.Base32())   //"h36jryryy"
	fmt.Printf("Base36   : %#v", id.Base36())   //"b88lijda8"
	fmt.Printf("Base58   : %#v", id.Base58())   //"fm892iv3"
	fmt.Printf("Base64   : %#v", id.Base64())   //"MzE2Nzc4Mzc0Nzk5MzY="
	fmt.Printf("Bytes    : %#v", id.Bytes())    //[]byte{0x33, 0x31, 0x36, 0x37, 0x37, 0x38, 0x33, 0x37, 0x34, 0x37, 0x39, 0x39, 0x33, 0x36}
	fmt.Printf("IntBytes : %#v", id.IntBytes()) //[8]uint8{0x0, 0x0, 0x1c, 0xcf, 0x92, 0x40, 0x10, 0x0}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC in milliseconds
	// You may customize this to set a different epoch for your application.
	Epoch int64 = 1288834974657 //Default Epoch Time

	// PODBits holds the number of bits to use for Node
	// Remember, you have a total 22 bits to share between Node/Step
	PODBits uint8 = 10

	// StepBits holds the number of bits to use for Step
	// Remember, you have a total 22 bits to share between POD/Step
	StepBits uint8 = 12
)
View Source
var ErrInvalidBase32 = errors.New("invalid base32")

ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte

View Source
var ErrInvalidBase58 = errors.New("invalid base58")

ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte

Functions

func ParseJsonStructToBytesBuffer

func ParseJsonStructToBytesBuffer(req interface{}) (*bytes.Buffer, error)

func QueryParamsValueToInt32Array

func QueryParamsValueToInt32Array(s, separator string, skipError bool) ([]int32, error)

func QueryParamsValueToStringArray

func QueryParamsValueToStringArray(s, separator string) []string

Types

type JSONSyntaxError

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

A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.

func (JSONSyntaxError) Error

func (j JSONSyntaxError) Error() string

type POD

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

A POD struct holds the basic information needed for a snowflake generator node

func NewPOD

func NewPOD(o *SnowflakeOpts) (*POD, error)

NewPOD returns a new snowflake pod that can be used to generate snowflake IDs

func (*POD) Generate

func (n *POD) Generate() SnowflakeID

Generate creates and returns a unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple PODS running with the same pod ID

type SnowflakeID

type SnowflakeID int64

An SnowflakeID is a custom type used for a snowflake ID. This is used so we can attach methods onto the ID.

func ParseBase2

func ParseBase2(id string) (SnowflakeID, error)

ParseBase2 converts a Base2 string into a snowflake ID

func ParseBase32

func ParseBase32(b []byte) (SnowflakeID, error)

ParseBase32 parses a base32 []byte into a snowflake ID NOTE: There are many different base32 implementations so careful when doing any interoperation.

func ParseBase36

func ParseBase36(id string) (SnowflakeID, error)

ParseBase36 converts a Base36 string into a snowflake ID

func ParseBase58

func ParseBase58(b []byte) (SnowflakeID, error)

ParseBase58 parses a base58 []byte into a snowflake ID

func ParseBase64

func ParseBase64(id string) (SnowflakeID, error)

ParseBase64 converts a base64 string into a snowflake ID

func ParseBytes

func ParseBytes(id []byte) (SnowflakeID, error)

ParseBytes converts a byte slice into a snowflake ID

func ParseInt64

func ParseInt64(id int64) SnowflakeID

ParseInt64 converts an int64 into a snowflake ID

func ParseIntBytes

func ParseIntBytes(id [8]byte) SnowflakeID

ParseIntBytes converts an array of bytes encoded as big endian integer as a snowflake ID

func ParseString

func ParseString(id string) (SnowflakeID, error)

ParseString converts a string into a snowflake ID

func (SnowflakeID) Base2

func (f SnowflakeID) Base2() string

Base2 returns a string base2 of the snowflake ID

func (SnowflakeID) Base32

func (f SnowflakeID) Base32() string

Base32 uses the z-base-32 character set but encodes and decodes similar to base58, allowing it to create an even smaller result string. NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func (SnowflakeID) Base36

func (f SnowflakeID) Base36() string

Base36 returns a base36 string of the snowflake ID

func (SnowflakeID) Base58

func (f SnowflakeID) Base58() string

Base58 returns a base58 string of the snowflake ID

func (SnowflakeID) Base64

func (f SnowflakeID) Base64() string

Base64 returns a base64 string of the snowflake ID

func (SnowflakeID) Bytes

func (f SnowflakeID) Bytes() []byte

Bytes returns a byte slice of the snowflake ID

func (SnowflakeID) Int64

func (f SnowflakeID) Int64() int64

Int64 returns an int64 of the snowflake ID

func (SnowflakeID) IntBytes

func (f SnowflakeID) IntBytes() [8]byte

IntBytes returns an array of bytes of the snowflake ID, encoded as a big endian integer.

func (SnowflakeID) MarshalJSON

func (f SnowflakeID) MarshalJSON() ([]byte, error)

MarshalJSON returns a json byte array string of the snowflake ID.

func (SnowflakeID) Node

func (f SnowflakeID) Node() int64

Node returns an int64 of the snowflake ID node number DEPRECATED: the below function will be removed in a future release.

func (SnowflakeID) Step

func (f SnowflakeID) Step() int64

Step returns an int64 of the snowflake step (or sequence) number DEPRECATED: the below function will be removed in a future release.

func (SnowflakeID) String

func (f SnowflakeID) String() string

String returns a string of the snowflake ID

func (SnowflakeID) Time

func (f SnowflakeID) Time() int64

Time returns an int64 unix timestamp in milliseconds of the snowflake ID time DEPRECATED: the below function will be removed in a future release.

func (*SnowflakeID) UnmarshalJSON

func (f *SnowflakeID) UnmarshalJSON(b []byte) error

UnmarshalJSON converts a json byte array of a snowflake ID into an ID type.

type SnowflakeOpts

type SnowflakeOpts struct {
	Epoch int64 `json:"epoch"`
	POD   int64 `json:"pod"`
}

Jump to

Keyboard shortcuts

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