util

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: MIT Imports: 31 Imported by: 22

README

util

infra.Go util.

Documentation

Index

Constants

View Source
const (
	// Version is the version number of the library
	Version string = "1.0.0"

	// DefaultAlphabet is the default alphabet used by go-hashids
	DefaultAlphabet string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
)
View Source
const (
	NoDuration = -99999
)

Variables

View Source
var (
	ErrUnavailable = errors.New("unavailable")
)
View Source
var GlobalExecutor = NewExecutor()

GlobalExecutor has the life cycle of the program itself any goroutine want to be shutdown before main exit can be started from this executor GlobalExecutor expects the main function to call stop it does not magically knows the main function exits

View Source
var HandlePanic = func(recovered interface{}, funcName string) {
	log.Println(fmt.Sprintf("%s panic: %v", funcName, recovered))
	log.Println(string(debug.Stack()))
}

HandlePanic logs goroutine panic by default

Functions

func AllinStrings

func AllinStrings(ss []string, ins []string) bool

func AnyToString

func AnyToString(val Any) string

func DeepMapping

func DeepMapping(src Map, dests ...Map) Map

DeepMapping 深度复制Map对象

func ExampleExecutor_Go

func ExampleExecutor_Go()

func ExampleExecutor_Go_panic

func ExampleExecutor_Go_panic()

func ExampleExecutor_StopAndWaitForever

func ExampleExecutor_StopAndWaitForever()

func Extension

func Extension(name string) string

func HMACSign

func HMACSign(data string, key string) (string, error)

func HMACVerify

func HMACVerify(data, sign string, key string) error

func InStrings

func InStrings(s string, ins []string) bool

func Ip2Num

func Ip2Num(ip string) int64

func MD5

func MD5(str string) string

MD5

func MD5File

func MD5File(file string) string

MD5File

func MD5FileBase64

func MD5FileBase64(file string) string

func MapBool added in v0.0.2

func MapBool(data Map, keys ...string) bool

func MapFloat added in v0.0.2

func MapFloat(data Map, keys ...string) float64

func MapInt added in v0.0.2

func MapInt(data Map, keys ...string) int64

func MapString added in v0.0.2

func MapString(data Map, keys ...string) string

MapString 从 map 读字串

func Md5

func Md5(str string) string

md5加密

func Md5File

func Md5File(file string) string

md5加密文件

func MergeString

func MergeString(args ...string) string

func NameWithoutExt

func NameWithoutExt(p string) string

BaseName 获取路径中的文件名,不包含扩展名

func Num2Ip

func Num2Ip(ipInt int64) string

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

func ParseDurationConfig

func ParseDurationConfig(config Map, field string) time.Duration

func ParseSize

func ParseSize(s string) int64

func ParseTOML

func ParseTOML(s string) (Map, error)

ParseTOML 解析toml文本得到配置

func SHA1

func SHA1(str string) string

sha1加密

func SHA1File

func SHA1File(file string) string

sha1加密文件

func SHA1FileBase64

func SHA1FileBase64(file string) string

func SHA256

func SHA256(str string) string

SHA256

func SHA256File

func SHA256File(file string) string

sha256加密文件

func SHA256FileBase64

func SHA256FileBase64(file string) string

func Sha1

func Sha1(str string) string

sha1加密

func Sha1BaseFile

func Sha1BaseFile(file string) string

func Sha1File

func Sha1File(file string) string

sha1加密文件

func Sha256

func Sha256(str string) string

sha256加密

func Sha256BaseFile

func Sha256BaseFile(file string) string

func Sha256File

func Sha256File(file string) string

sha256加密文件

func Split

func Split(s string) []string

func UUID

func UUID() string

GenerateUUID is used to generate a random UUID

Types

type Executor

type Executor struct {
	HandlePanic func(recovered interface{}, funcName string)
	// contains filtered or unexported fields
}

Executor is a executor without limits on counts of alive goroutines it tracks the goroutine started by it, and can cancel them when shutdown

func NewExecutor

func NewExecutor() *Executor

NewExecutor creates a new Executor, Executor can not be created by &Executor{} HandlePanic can be set with a callback to override global HandlePanic

func (*Executor) Go

func (executor *Executor) Go(handler func(ctx context.Context))

Go starts a new goroutine and tracks its lifecycle. Panic will be recovered and logged automatically, except for StopSignal

func (*Executor) Stop

func (executor *Executor) Stop()

Stop cancel all goroutines started by this executor without wait

func (*Executor) StopAndWait

func (executor *Executor) StopAndWait(ctx context.Context)

StopAndWait cancel all goroutines started by this executor and wait. Wait can be cancelled by the context passed in.

func (*Executor) StopAndWaitForever

func (executor *Executor) StopAndWaitForever()

StopAndWaitForever cancel all goroutines started by this executor and wait until all goroutines exited

type FastID

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

func NewFastID

func NewFastID(timeBits, nodeBits, stepBits uint, timeStart int64) *FastID

func NewFastIDWithNode

func NewFastIDWithNode(timeBits, nodeBits, stepBits uint, timeStart, nodeId int64) *FastID

func (*FastID) GetSequence

func (c *FastID) GetSequence(id int64) int64

func (*FastID) GetTime

func (c *FastID) GetTime(id int64) int64

func (*FastID) NextID

func (c *FastID) NextID() int64

type HashID

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

HashID contains everything needed to encode/decode hashids

func New

func New() (*HashID, error)

New creates a new HashID

func NewWithData

func NewWithData(data *HashIDData) (*HashID, error)

NewWithData creates a new HashID with the provided HashIDData

func (*HashID) Decode

func (h *HashID) Decode(hash string) []int

DEPRECATED: Use DecodeWithError instead Decode unhashes the string passed to an array of int. It is symmetric with Encode if the Alphabet and Salt are the same ones which were used to hash. MinLength has no effect on Decode.

func (*HashID) DecodeHex

func (h *HashID) DecodeHex(hash string) (string, error)

DecodeHex unhashes the string passed to a hexadecimal string. It is symmetric with EncodeHex if the Alphabet and Salt are the same ones which were used to hash.

Each hex nibble is decoded from an integer in range [16, 31].

func (*HashID) DecodeInt64

func (h *HashID) DecodeInt64(hash string) []int64

DEPRECATED: Use DecodeInt64WithError instead DecodeInt64 unhashes the string passed to an array of int64. It is symmetric with EncodeInt64 if the Alphabet and Salt are the same ones which were used to hash. MinLength has no effect on DecodeInt64.

func (*HashID) DecodeInt64WithError

func (h *HashID) DecodeInt64WithError(hash string) ([]int64, error)

DecodeInt64 unhashes the string passed to an array of int64. It is symmetric with EncodeInt64 if the Alphabet and Salt are the same ones which were used to hash. MinLength has no effect on DecodeInt64.

func (*HashID) DecodeWithError

func (h *HashID) DecodeWithError(hash string) ([]int, error)

Decode unhashes the string passed to an array of int. It is symmetric with Encode if the Alphabet and Salt are the same ones which were used to hash. MinLength has no effect on Decode.

func (*HashID) Encode

func (h *HashID) Encode(numbers []int) (string, error)

Encode hashes an array of int to a string containing at least MinLength characters taken from the Alphabet. Use Decode using the same Alphabet and Salt to get back the array of int.

func (*HashID) EncodeHex

func (h *HashID) EncodeHex(hex string) (string, error)

EncodeHex hashes a hexadecimal string to a string containing at least MinLength characters taken from the Alphabet. A hexadecimal string should not contain the 0x prefix. Use DecodeHex using the same Alphabet and Salt to get back the hexadecimal string.

Each hex nibble is encoded as an integer in range [16, 31].

func (*HashID) EncodeInt64

func (h *HashID) EncodeInt64(numbers []int64) (string, error)

EncodeInt64 hashes an array of int64 to a string containing at least MinLength characters taken from the Alphabet. Use DecodeInt64 using the same Alphabet and Salt to get back the array of int64.

type HashIDData

type HashIDData struct {
	// Alphabet is the alphabet used to generate new ids
	Alphabet string

	// MinLength is the minimum length of a generated id
	MinLength int

	// Salt is the secret used to make the generated id harder to guess
	Salt string
}

HashIDData contains the information needed to generate hashids

func NewData

func NewData() *HashIDData

NewData creates a new HashIDData with the DefaultAlphabet already set.

type HashRing

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

func NewHashRing

func NewHashRing(weights map[string]int, spotsArgs ...int) *HashRing

func (*HashRing) Append

func (h *HashRing) Append(nodeKey string, weight int)

func (*HashRing) Locate

func (h *HashRing) Locate(s string) string

func (*HashRing) Remove

func (h *HashRing) Remove(nodeKey string)

func (*HashRing) Update

func (h *HashRing) Update(nodeKey string, weight int)

type Runner

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

func NewRunner

func NewRunner() *Runner

func (*Runner) End

func (s *Runner) End()

func (*Runner) Run

func (s *Runner) Run(f func())

func (*Runner) Stop

func (s *Runner) Stop() chan error

func (*Runner) Wait

func (s *Runner) Wait()

type TOMLBuffer

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

func (*TOMLBuffer) Close

func (buf *TOMLBuffer) Close() error

func (*TOMLBuffer) Read

func (buf *TOMLBuffer) Read(p []byte) (n int, err error)

func (*TOMLBuffer) Seek

func (buf *TOMLBuffer) Seek(offset int64, whence int) (int64, error)

type TOMLBuilder

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

func NewTOMLBuilder

func NewTOMLBuilder() *TOMLBuilder

func (*TOMLBuilder) Append

func (this *TOMLBuilder) Append(key string, vals ...string)

func (*TOMLBuilder) Build

func (this *TOMLBuilder) Build() string

func (*TOMLBuilder) Files

func (this *TOMLBuilder) Files() map[string][]TOMLFile

func (*TOMLBuilder) Forms

func (this *TOMLBuilder) Forms() map[string][]string

func (*TOMLBuilder) Store

func (this *TOMLBuilder) Store(key, name, mime string, buf io.ReadSeekCloser)

type TOMLFile

type TOMLFile struct {
	Name   string
	MIME   string
	Buffer io.ReadSeekCloser
}

Jump to

Keyboard shortcuts

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