utils

package
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: MIT Imports: 38 Imported by: 9

Documentation

Index

Constants

View Source
const (
	StrStr   = 0
	StrInt   = 1
	StrFloat = 2
)
View Source
const (
	PrecModeDecimalPlace = 2 // 保留小数点后指定位
	PrecModeSignifDigits = 3 // 保留全部有效数字个数
	PrecModeTickSize     = 4 // 返回给定的数字的整数倍
)
View Source
const (
	JsonNumDefault = 0 // equal to JsonNumFloat
	JsonNumFloat   = 0 // parse number in json to float64
	JsonNumStr     = 1 // keep number in json as json.Number type
	JsonNumAuto    = 2 // auto parse json.Number to int64/float64 in []interface{} map[string]interface{}
)
View Source
const (
	SecsMin  = 60
	SecsHour = SecsMin * 60
	SecsDay  = SecsHour * 24
	SecsWeek = SecsDay * 7
	SecsMon  = SecsDay * 30
	SecsQtr  = SecsMon * 3
	SecsYear = SecsDay * 365
)
View Source
const (
	UriEncodeSafe = "~()*!.'"
)

Variables

View Source
var (
	ErrInvalidPrecision   = errors.New("invalid precision")
	ErrNegPrecForTickSize = errors.New("negative precision for tick size")
)

Functions

func AlignTfMSecs added in v0.2.6

func AlignTfMSecs(timeMSecs int64, tfMSecs int64) int64

AlignTfMSecs Convert the given 13 millisecond timestamp to the header start timestamp for the specified time period 将给定的13位毫秒级时间戳,转为指定时间周期下,的头部开始时间戳

func AlignTfMSecsOffset added in v0.2.6

func AlignTfMSecsOffset(timeMSecs, tfMSecs, offset int64) int64

func AlignTfSecs added in v0.2.6

func AlignTfSecs(timeSecs int64, tfSecs int) int64

AlignTfSecs Convert the given 10 second timestamp to the header start timestamp for the specified time period 将给定的10位秒级时间戳,转为指定时间周期下,的头部开始时间戳

func AlignTfSecsOffset added in v0.2.6

func AlignTfSecsOffset(timeSecs int64, tfSecs int, offset int) int64

AlignTfSecsOffset Convert the given 10 second timestamp to the header start timestamp for the specified time period, using the specified offset 将给定的10位秒级时间戳,转为指定时间周期下,的头部开始时间戳,使用指定偏移

func ArrContains

func ArrContains[T comparable](s []T, e T) bool

func ArrSum added in v0.2.1

func ArrSum(s []float64) float64

func ByteToStruct

func ByteToStruct[T any](byteChan <-chan []byte, outChan chan<- T, numType int)

ByteToStruct 将[]byte类型的chan通道,转为指定类型通道

func DecToPrec

func DecToPrec(num string, countMode int, precision string, isRound, padZero bool) (string, error)

DecToPrec 将数字转换为指定精度返回

num: 需要处理的数字字符串
countMode: PrecModeDecimalPlace 保留小数点后指定位;
PrecModeSignifDigits 保留全部有效数字个数
PrecModeTickSize 返回给定的数字的整数倍
precision: 结合countMode使用,正整数,负整数,正浮点数
isRound: 是否四舍五入,默认截断
padZero: 是否尾部填充0到指定长度

func Eddsa

func Eddsa(request []byte, secret []byte) (string, error)

func EncodeToLatin1

func EncodeToLatin1(input string) ([]byte, error)

func EncodeURIComponent

func EncodeURIComponent(str string, safe string) string

func EqualIn

func EqualIn(a, b, thres float64) bool

EqualIn 判断两个float是否在一定范围内近似相等

func EqualNearly

func EqualNearly(a, b float64) bool

EqualNearly 判断两个float是否近似相等,解决浮点精读导致不等

func GetMapFloat

func GetMapFloat(data map[string]interface{}, key string) float64

func GetMapVal

func GetMapVal[T any](items map[string]interface{}, key string, defVal T) T

GetMapVal 从map中获取指定类型的值。只支持简单类型,不支持slice,map,array,struct等

func GetTfAlignOrigin added in v0.2.6

func GetTfAlignOrigin(secs int) (string, int)

func HMAC

func HMAC(request []byte, secret []byte, algorithm func() hash.Hash, digest string) string

func ISO8601

func ISO8601(millis int64) string

func IsNil

func IsNil(i interface{}) bool

IsNil 判断是否为nil

golang中类型和值是分开存储的,如果一个指针有类型,值为nil,直接判断==nil会返回false

func KeysOfMap added in v0.2.3

func KeysOfMap[M ~map[K]V, K comparable, V any](m M) []K

func MD5 added in v0.2.6

func MD5(data []byte) string

func MapValStr

func MapValStr(input map[string]interface{}) map[string]string

func Marshal added in v0.2.9

func Marshal(v any) ([]byte, error)

func MarshalString added in v0.2.9

func MarshalString(v any) (string, error)

func OmitMapKeys

func OmitMapKeys(items map[string]interface{}, keys ...string)

func ParseJsonNumber added in v0.2.10

func ParseJsonNumber(data interface{}) error

ParseJsonNumber

parse json.Number to int64/float64 in *[]interface{} *map[string]interface{}

func ParseTimeFrame

func ParseTimeFrame(timeframe string) (int, *errs.Error)

func ParseTimeRanges added in v0.2.1

func ParseTimeRanges(items []string, loc *time.Location) ([][2]int64, *errs.Error)

func PopMapVal

func PopMapVal[T any](items map[string]interface{}, key string, defVal T) T

PopMapVal 从map中获取指定类型的值并删除。只支持简单类型,不支持slice,map,array,struct等

func PrecFloat64

func PrecFloat64(num float64, prec float64, isRound bool, mode int) (float64, error)

PrecFloat64 对给定浮点数取近似值,精确到指定位数

func PrecFloat64Str

func PrecFloat64Str(num float64, prec float64, isRound bool, mode int) (string, error)

func PrecisionFromString

func PrecisionFromString(input string) float64

PrecisionFromString 1e-4 -> 4 0.000001 -> 6 100 -> 0

func ReadCacheFile added in v0.2.1

func ReadCacheFile(key string) (string, *errs.Error)

func ReadFile

func ReadFile(path string) ([]byte, error)

func ReadJsonFile

func ReadJsonFile(path string, obj interface{}, numType int) error

ReadJsonFile

numType: JsonNumDefault(JsonNumFloat), JsonNumStr, JsonNumAuto

func RegTfSecs added in v0.2.6

func RegTfSecs(items map[string]int)

func SafeMapVal

func SafeMapVal[T any](items map[string]string, key string, defVal T) (result T, err error)

SafeMapVal 从字典中读取给定键的值,并自动转换为需要的类型,如果出错则返回默认值

func SafeParams

func SafeParams(params map[string]interface{}) map[string]interface{}

func SecsToTF added in v0.2.6

func SecsToTF(tfSecs int) string

SecsToTF Convert the seconds of a time period into a time period 将时间周期的秒数,转为时间周期

func SetFieldBy

func SetFieldBy[T any](field *T, items map[string]interface{}, key string, defVal T)

func Signature

func Signature(data string, secret string, method string, hashName string, digest string) (string, *errs.Error)

Signature method: rsa, eddsa, hmac

hashName:
	rsa: sha256/sha384/sha512
	eddsa: 不需要
	hmac:sha256/sha384/sha512
digest: hmac: base64/hex

func SnakeToCamel added in v0.2.4

func SnakeToCamel(input string) string

func TFToSecs added in v0.2.6

func TFToSecs(timeFrame string) int

TFToSecs Convert the time cycle to seconds Supporting units: s, m, h, d, M, Q, Y 将时间周期转为秒 支持单位:s, m, h, d, M, Q, Y

func UUID

func UUID(length int) string

func Unmarshal added in v0.2.0

func Unmarshal(data []byte, out interface{}, numType int) error

Unmarshal decode json

numType: JsonNumDefault(JsonNumFloat), JsonNumStr, JsonNumAuto

func UnmarshalString added in v0.2.0

func UnmarshalString(text string, out interface{}, numType int) error

UnmarshalString decode json (big int as float64)

numType: JsonNumDefault(JsonNumFloat), JsonNumStr, JsonNumAuto

func UrlEncodeMap

func UrlEncodeMap(params map[string]interface{}, escape bool) string

UrlEncodeMap 将map编码为url查询字符串 escape: 是否对键和值进行编码

func ValsOfMap added in v0.2.3

func ValsOfMap[M ~map[K]V, K comparable, V any](m M) []V

func WriteCacheFile added in v0.2.1

func WriteCacheFile(key, content string, expSecs int) *errs.Error

func WriteFile

func WriteFile(path string, data []byte) error

func WriteJsonFile

func WriteJsonFile(path string, data interface{}) error

func YMD

func YMD(timestamp int64, separator string, fullYear bool) string

YMD 将13位时间戳转为日期

separator 年月日的间隔字符 fullYear 年是否使用4个数字

Types

type StrType added in v0.2.1

type StrType struct {
	Val  string
	Type int
}

func SplitParts added in v0.2.1

func SplitParts(text string) []*StrType

type TFOrigin added in v0.2.6

type TFOrigin struct {
	TFSecs     int
	OffsetSecs int
	Origin     string
}

Jump to

Keyboard shortcuts

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