utils

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: Apache-2.0 Imports: 28 Imported by: 31

Documentation

Index

Constants

View Source
const (
	EMPTYSTR = " \t\n\r"
)

Variables

View Source
var (
	PrivateIP1Start = Ip2Int("10.0.0.0")
	PrivateIP1End   = Ip2Int("10.255.255.255")
	PrivateIP2Start = Ip2Int("172.16.0.0")
	PrivateIP2End   = Ip2Int("172.31.255.255")
	PrivateIP3Start = Ip2Int("192.168.0.0")
	PrivateIP3End   = Ip2Int("192.168.255.255")

	HostLocalIPStart = Ip2Int("127.0.0.0")
	HostLocalIPEnd   = Ip2Int("127.255.255.255")

	LinkLocalIPStart = Ip2Int("169.254.0.0")
	LinkLocalIPEnd   = Ip2Int("169.254.255.255")
)
View Source
var TRUE_STRS = []string{"1", "true", "on", "yes"}

Functions

func All

func All(pred Pred_t, items ...interface{}) (bool, interface{})

func Any

func Any(pred Pred_t, items ...interface{}) (bool, interface{})

func ArgsStringToArray

func ArgsStringToArray(s string) []string

func CamelSplit

func CamelSplit(str string, sep string) string

func CamelSplitTokens

func CamelSplitTokens(str string) []string

func Capitalize

func Capitalize(str string) string

func ComposeURL

func ComposeURL(paths ...string) string

func ConvertError

func ConvertError(obj interface{}, toType string) error

func DecodeMeta

func DecodeMeta(str string) string

func DescryptAESBase64

func DescryptAESBase64(key, secret string) (string, error)

func DescryptAESBase64Url

func DescryptAESBase64Url(key, secret string) (string, error)

func Distinct

func Distinct(list []string) []string

func DistinctJoin

func DistinctJoin(list []string, separator string) string

func DumpAllGoroutineStack

func DumpAllGoroutineStack(w io.Writer)

func EncryptAESBase64

func EncryptAESBase64(key, msg string) (string, error)

func EncryptAESBase64Url

func EncryptAESBase64Url(key, msg string) (string, error)

func FindWords

func FindWords(str []byte, offset int) []string

func FloatRound

func FloatRound(num float64, precision int) float64

func GenRequestId

func GenRequestId(bytes int) string

func GetAddrPort

func GetAddrPort(addrPort string) (string, int)

func GetBytes

func GetBytes(sizeStr string) (int64, error)

func GetOutboundIP

func GetOutboundIP() net.IP

func GetSize

func GetSize(sizeStr, defaultSize string, base int64) (size int64, err error)

GetSize parse size string to int defaultSize be used when sizeStr not end with defaultSize sizeStr: 1024, 1M, 1m, 1K etc. defaultSize: g, m, k, b etc. base: base multiple unit, 1024

func GetSizeBytes

func GetSizeBytes(sizeStr, defaultSize string) (int64, error)

func GetSizeGB

func GetSizeGB(sizeStr, defaultSize string) (int64, error)

func GetSizeKB

func GetSizeKB(sizeStr, defaultSize string) (int64, error)

func GetSizeMB

func GetSizeMB(sizeStr, defaultSize string) (int64, error)

func GroupBy

func GroupBy(items interface{}, ks selectFunc) (map[string][]interface{}, error)

func HasPrefix

func HasPrefix(s string, prefix string) bool

func HasSuffix

func HasSuffix(s string, suffix string) bool

func InArray

func InArray(v interface{}, in interface{}) (ok bool, i int)

func InArray2

func InArray2[T int | uint | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64 | float32 | float64 | string](val T, array []T) (ok bool, i int)

func InStringArray

func InStringArray(val string, array []string) (ok bool, i int)

func Ip2Int

func Ip2Int(ipString string) uint32

func IpRangeCount

func IpRangeCount(ipStart, ipEnd string) int

func IsAscii

func IsAscii(str string) bool

func IsExitAddress

func IsExitAddress(ip string) bool

func IsHostLocal

func IsHostLocal(ip uint32) bool

func IsInArray

func IsInArray[T int | uint | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64 | float32 | float64 | string](val T, array []T) bool

func IsInStringArray

func IsInStringArray(val string, array []string) bool

func IsLinkLocal

func IsLinkLocal(ip uint32) bool

func IsLocalStorage

func IsLocalStorage(storageType string) bool

func IsMatch

func IsMatch(s string, pattern string) bool

func IsMatchCompactMacAddr

func IsMatchCompactMacAddr(s string) bool

func IsMatchFloat

func IsMatchFloat(s string) bool

func IsMatchIP4

func IsMatchIP4(s string) bool

func IsMatchIP6

func IsMatchIP6(s string) bool

func IsMatchInteger

func IsMatchInteger(s string) bool

func IsMatchMacAddr

func IsMatchMacAddr(s string) bool

func IsMatchSize

func IsMatchSize(s string) bool

func IsMatchUUID

func IsMatchUUID(s string) bool

func IsPrivate

func IsPrivate(ip uint32) bool

func Kebab2Camel

func Kebab2Camel(kebab string, sep string) string

func Max

func Max(a int64, b int64) int64

func Min

func Min(a int64, b int64) int64

func RsaDecryptBase64

func RsaDecryptBase64(privateKey []byte, secret string) (string, error)

Base64封装的 RSA 密文解密

func RsaEncryptBase64

func RsaEncryptBase64(publicKey []byte, origData string) (string, error)

Base64封装的 RSA 密文解密

func RsaSign

func RsaSign(privateKey []byte, message string) (string, error)

RSA 签名

func RsaUnsign

func RsaUnsign(publicKey []byte, message, sign string) error

RSA 签名验证

func SavePid

func SavePid(pidfile string) error

func SelectDistinct

func SelectDistinct(items []interface{}, ks selectFunc) ([]string, error)

func SplitCSV

func SplitCSV(csv string) []string

func SubDict

func SubDict(dict map[string][]interface{}, keys ...string) (map[string][]interface{}, error)

func TagMap

func TagMap(tag reflect.StructTag) map[string]string

func TagPop

func TagPop(m map[string]string, key string) (map[string]string, string, bool)

func ToBool

func ToBool(str string) bool

func ToDict

func ToDict(objs interface{}, ks selectFunc) (map[string]interface{}, error)

func ToDuration

func ToDuration(i interface{}) time.Duration

func ToDurationE

func ToDurationE(i interface{}) (d time.Duration, err error)

func ToFloat64

func ToFloat64(i interface{}) float64

ToFloat64 casts an interface to a float64 type.

func ToFloat64E

func ToFloat64E(i interface{}) (float64, error)

ToFloat64E casts an interface to a float64 type.

func ToInt64

func ToInt64(i interface{}) int64

ToInt64 casts an interface to an int64 type.

func ToInt64E

func ToInt64E(i interface{}) (int64, error)

ToInt64E casts an interface to an int64 type.

func ToStatDict2

func ToStatDict2(items []StatItem2) (map[string]interface{}, error)

func ToStatDict3

func ToStatDict3(items []StatItem3) (map[string]map[string]interface{}, error)

func TransSQLAchemyURL

func TransSQLAchemyURL(pySQLSrc string) (dialect, ret string, err error)

func Truncate

func Truncate(s string, length int) string

func TruncateString

func TruncateString(v interface{}, maxLen int) string

func Unquote

func Unquote(str string) string

func WaitTimeOut

func WaitTimeOut(wg *WaitGroupWrapper, timeout time.Duration) bool

Types

type FibonacciRetrier

type FibonacciRetrier struct {
	// T0 is the 1st item in fibonacci series
	T0 time.Duration
	// T1 is the 2nd item in fibonacci series
	T1        time.Duration
	MaxTries  int
	MaxElapse time.Duration
	RetryFunc FibonacciRetryFunc
	// contains filtered or unexported fields
}

FibonacciRetrier calls RetryFunc until it returns done, or waits in fibonacci way until MaxTries, MaxElapse are exceeded whichever happens first

func NewFibonacciRetrierMaxElapse

func NewFibonacciRetrierMaxElapse(maxElapse time.Duration, retryFunc FibonacciRetryFunc) *FibonacciRetrier

NewFibonacciRetrierMaxTries returnes a retrier that tries for at most maxElapse duration with the first 2 items being 1 second

func NewFibonacciRetrierMaxTries

func NewFibonacciRetrierMaxTries(maxTries int, retryFunc FibonacciRetryFunc) *FibonacciRetrier

NewFibonacciRetrierMaxTries returnes a retrier that tries at most maxTries times with the first 2 items being 1 second

func (*FibonacciRetrier) Elapsed

func (fibr *FibonacciRetrier) Elapsed() time.Duration

Elapsed returns the duration the retrier has run

func (*FibonacciRetrier) Start

func (fibr *FibonacciRetrier) Start(ctx context.Context) (done bool, err error)

Start initiates the call and wait sequence

If RetryFunc returns with done being true. err will also be what RetryFunc returns

Otherwise Start will return with done being false and err of type FibonacciRetrierError with last err returned by RetryFunc wrapped in, or ctx.Err() if it's done

func (*FibonacciRetrier) Tried

func (fibr *FibonacciRetrier) Tried() int

Tries returns number of times RetryFunc has been called, excluding the current executing one

type FibonacciRetrierError

type FibonacciRetrierError struct {
	Type FibonacciRetrierErrorType
	// Fibr is a copy of the original retrier.
	Fibr FibonacciRetrier
	// Err is the original error returned by retry func.  It can be nil.
	Err error
}

FibonacciRetrierError is the type of error retrier returns when MaxTries or MaxElapse are exceeded.

func (FibonacciRetrierError) Error

func (err FibonacciRetrierError) Error() string

type FibonacciRetrierErrorType

type FibonacciRetrierErrorType int

FibonacciRetrierErrorType is the error type that could be returned by retrier

const (
	FibonacciRetrierErrorMaxTriesExceeded FibonacciRetrierErrorType = iota
	FibonacciRetrierErrorMaxElapseExceeded
)

type FibonacciRetryFunc

type FibonacciRetryFunc func(retrier FibonacciRetrier) (done bool, err error)

FibonacciRetryFunc is the type of func retrier calls.

type Pred_t

type Pred_t func(interface{}) bool

type StatItem2

type StatItem2 interface {
	First() string
	Second() interface{}
}

type StatItem3

type StatItem3 interface {
	First() string
	Second() string
	Third() interface{}
}

type WaitGroupWrapper

type WaitGroupWrapper struct {
	sync.WaitGroup
}

func (*WaitGroupWrapper) Wrap

func (w *WaitGroupWrapper) Wrap(cb func())

Jump to

Keyboard shortcuts

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