ut

package module
v0.0.0-...-2e45435 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: BSD-3-Clause Imports: 38 Imported by: 7

README

ut为个人常用golang工具类和方法的集合

Documentation

Index

Constants

View Source
const DefaultUidTag = "uid"
View Source
const OpRegex = "regex"

Variables

View Source
var AllowOps = []string{"eq", "gt", "gte", "lt", "lte", "ne", "in", "nin", OpRegex}
View Source
var MaxRetrySmallError = errors.New("重试次数需要大于1")
View Source
var (
	NotEnableGlobalSearch = errors.New("未启用全局搜索")
)

Functions

func CookieStrReplace

func CookieStrReplace(s, k, v string) string

CookieStrReplace 对字符串cookie任何key进行替换改写 返回变更后的cookie字符串

func FindCookieValue

func FindCookieValue(cookieStr string, key string) (string, bool)

FindCookieValue 从字符串cookie中找到任一Key

func GetDiffStruct

func GetDiffStruct(oldData interface{}, body interface{}, bodyMap map[string]interface{}) (map[string]interface{}, error)

GetDiffStruct 存在与bodyMap中的元素 判断与old和当前body值不同则写入diff bodyMap中的key是 json tag 而不是字段名

func GetEndTimeOfToday

func GetEndTimeOfToday() time.Time

GetEndTimeOfToday 获取今日23点59分59秒

func GetEnv

func GetEnv(key, fallback string) string

func GetFirstDateOfMonth

func GetFirstDateOfMonth() time.Time

GetFirstDateOfMonth 获取本月第一天0点

func GetFirstDateOfWeek

func GetFirstDateOfWeek() time.Time

GetFirstDateOfWeek 获取本周周一0点

func GetStartTimeOfToday

func GetStartTimeOfToday() time.Time

GetStartTimeOfToday 获取今日0点

func IrisErr

func IrisErr(ctx iris.Context, input any)

func IrisErrReturn

func IrisErrReturn(ctx iris.Context, input any, statusCode int, businessCode int)

func IsZeroOfUnderlyingType

func IsZeroOfUnderlyingType(x interface{}) bool

func ListStringToInterface

func ListStringToInterface(t []string) []interface{}

func MBuildFkUnwind

func MBuildFkUnwind(collection, localField, remoteField, remark string) []bson.D

MBuildFkUnwind 构建通用解构外键

func MBuildFkUnwindOfEmptyReturn

func MBuildFkUnwindOfEmptyReturn(collection, localField, remoteField, remark string) []bson.D

MBuildFkUnwindOfEmptyReturn preserveNullAndEmptyArrays 默认为false 不存在时则不返回 文档 https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/

func MBuilderFk

func MBuilderFk(collection, localField, remoteField, remark string) bson.D

MBuilderFk 构建通用外键

func MCreateIndex

func MCreateIndex(ctx context.Context, db *mongo.Collection, indexs ...mongo.IndexModel) error

MCreateIndex 快捷创建索引

func MGen2dSphere

func MGen2dSphere(k string) mongo.IndexModel

MGen2dSphere mongo 生成2dSphere索引

func MGenNormal

func MGenNormal(k string) mongo.IndexModel

MGenNormal mongo 生成普通索引

func MGenUnique

func MGenUnique(k string, sparse bool) mongo.IndexModel

MGenUnique mongo 生成唯一索引

func MTN

func MTN(instance interface{}) string

MTN 模型获取表名 使用 instanceToTableName 方法

func MUserFk

func MUserFk(local, remark string) bson.D

func MUserInfoFk

func MUserInfoFk(localField ...string) []bson.D

func MustFindCookieValue

func MustFindCookieValue(cookieStr string, key string) string

MustFindCookieValue 未找到则返回空字符串

func OverrideTemplateWithCookies

func OverrideTemplateWithCookies(templateStr string, templateMap map[string]string, notInTemplateInsert bool) []*http.Cookie

OverrideTemplateWithCookies 多字段覆盖模板cookie中的字段返回 http.Cookie

func ParseCookieStrToCookies

func ParseCookieStrToCookies(rawStr, sep string) []*http.Cookie

ParseCookieStrToCookies 解析指定分隔符的字符串cookie为 http.Cookie

func ParseCookiesToStr

func ParseCookiesToStr(cookies []*http.Cookie, sep string) string

ParseCookiesToStr 将HTTP Cookie切片编码为Cookie字符串

func ParseMapToCookies

func ParseMapToCookies(inputMap map[string]string) []*http.Cookie

ParseMapToCookies 解析map格式的cookie为 http.Cookie

func QueryToMongoPipeline

func QueryToMongoPipeline(query *QueryFull) []bson.D

func RandomInt

func RandomInt(start, end int) int

RandomInt 区间整数随机

func RandomStr

func RandomStr(n int) string

RandomStr 随机N位字符串(英文)

func RangeRandomIntSet

func RangeRandomIntSet(start int64, end int64, count int64) []int64

RangeRandomIntSet 范围随机正整数但不重复

func RemoveDuplicatesUnordered

func RemoveDuplicatesUnordered(elements []string) []string

RemoveDuplicatesUnordered 删除重复字符串但不保证排序

func RetryFunc

func RetryFunc(f func() error, maxRetryCount uint, interval time.Duration) error

RetryFunc 最大重试次数执行方法 函数返回error也会继续重试

func STN

func STN(modelName string) string

STN 模型字符串转表名 使用 strToTableName 方法

func SaveSliceToFiles

func SaveSliceToFiles[T any](filePath string, values []T, headers ...string) error

func SplitArrayByFixedSize

func SplitArrayByFixedSize[T any](arr []T, fixedBulkSize int) [][]T

SplitArrayByFixedSize 根据限制分配数组 例如fixedBulkSize为10 则会把arr分为10分

func SplitArrayByThreshold

func SplitArrayByThreshold[T any](arr []T, threshold int) [][]T

SplitArrayByThreshold 根据固定值分配成数组 例如threshold为5000 则把arr按照5000一个数组进行切分

func StrToB58

func StrToB58(s string) string

func StrToB62

func StrToB62(s string) string

func StrToInt

func StrToInt(s string) int64

func StrToInt64

func StrToInt64(s string) int64

func StructCopy

func StructCopy(origin, newData interface{}) error

StructCopy 相同名称的struct field 进行copy

func ToDiffBson

func ToDiffBson[T any](original, current T, skips []string, keeps []string) (bson.M, error)

ToDiffBson 两个struct找出不同项 支持skip格式为 a.b.c 只会对比值不同的 两个struct必须相同

func ToUpdateBson

func ToUpdateBson(input any, skips []string, keeps []string) (bson.M, error)

ToUpdateBson struct更新内容给转换为bson.M skip支持格式 a.b.c 代表跳过这个 只会更新存在的 有值的 若原本有值要清除 请使用diff

func TypeChange

func TypeChange(input any, wantType string) (any, error)

TypeChange 工具转换

Types

type BasePage

type BasePage struct {
	Page     int64 `json:"page,omitempty"`
	PageSize int64 `json:"page_size,omitempty"`
}

type BaseQuery

type BaseQuery struct {
	*BasePage
	*BaseSort
}

type BaseSort

type BaseSort struct {
	SortDesc []string `json:"sort_desc,omitempty"`
	SortAsc  []string `json:"sort_asc,omitempty"`
}

type ConcurrentMap

type ConcurrentMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewConcurrentMap

func NewConcurrentMap[K comparable, V any]() *ConcurrentMap[K, V]

func (*ConcurrentMap[K, V]) Delete

func (c *ConcurrentMap[K, V]) Delete(key K)

func (*ConcurrentMap[K, V]) Get

func (c *ConcurrentMap[K, V]) Get(key K) (V, bool)

func (*ConcurrentMap[K, V]) GetMap

func (c *ConcurrentMap[K, V]) GetMap() map[K]V

func (*ConcurrentMap[K, V]) Set

func (c *ConcurrentMap[K, V]) Set(key K, value V)

type FuncCalcTime

type FuncCalcTime struct {
	Name string

	Stage     []timeItem
	LinePrint bool
	// contains filtered or unexported fields
}

func NewFST

func NewFST(name string) FuncCalcTime

NewFST 方法计时器

func (*FuncCalcTime) Add

func (c *FuncCalcTime) Add(t string, msg ...interface{})

func (*FuncCalcTime) ChangeLinePrint

func (c *FuncCalcTime) ChangeLinePrint(show bool)

func (*FuncCalcTime) Print

func (c *FuncCalcTime) Print(endIndexs ...int)

Print i为截止下标

type GeoItem

type GeoItem struct {
	Field   string  `json:"field,omitempty"`    // 获取的字段
	ToField string  `json:"to_field,omitempty"` // 距离字段名
	Lng     float64 `json:"lng,omitempty"`      // 范围 -180 到 180
	Lat     float64 `json:"lat,omitempty"`      // 范围 -90 到 90
	GeoMax  int64   `json:"geo_max,omitempty"`  // 单位为米
	GeoMin  int64   `json:"geo_min,omitempty"`  // 单位为米
}

type Kov

type Kov struct {
	Key   string `json:"key,omitempty" bson:"key,omitempty"` // 格式是 "abc.die.ede"
	Op    string `json:"op,omitempty" bson:"op,omitempty"`   // 格式为英文 看 AllowOps
	Value any    `json:"value,omitempty" bson:"value,omitempty" jsonschema:"type=string"`
}

type MongoFacetResult

type MongoFacetResult struct {
	Count int64 `json:"count,omitempty"`
	Data  any   `json:"data"`
}

type Pk

type Pk struct {
	LocalKey      string `json:"local_key,omitempty"`
	RemoteModelId string `json:"remote_model_id,omitempty"`
	RemoteKey     string `json:"remote_key,omitempty"`
	Alias         string `json:"alias,omitempty"`
	EmptyReturn   bool   `json:"empty_return,omitempty"`
	Unwind        bool   `json:"unwind,omitempty"`
}

type PruneCtxQuery

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

func NewPruneCtxQuery

func NewPruneCtxQuery() *PruneCtxQuery

func (*PruneCtxQuery) PruneParse

func (p *PruneCtxQuery) PruneParse(params map[string]string, searchFields []string, geoKey string) (*QueryFull, error)

func (*PruneCtxQuery) PruneParsePage

func (p *PruneCtxQuery) PruneParsePage() (*BaseQuery, error)

PruneParsePage 解析出 page page_size sort

func (*PruneCtxQuery) PruneParseQuery

func (p *PruneCtxQuery) PruneParseQuery(searchFields []string, geoKey string) (*QueryParse, error)

PruneParseQuery 解析出 _last geo search

func (*PruneCtxQuery) PruneParseUrlParams

func (p *PruneCtxQuery) PruneParseUrlParams() (and []*Kov, or []*Kov, err error)

PruneParseUrlParams 纯解析url上的参数

func (*PruneCtxQuery) SetParams

func (p *PruneCtxQuery) SetParams(params map[string]string)

type QueryFull

type QueryFull struct {
	*QueryParse
	*BaseQuery
	Pks      []*Pk `json:"pks,omitempty"`
	GetCount bool  `json:"get_count,omitempty"` // 获取过滤总数
}

type QueryParse

type QueryParse struct {
	And  []*Kov   `json:"and,omitempty"`
	Or   []*Kov   `json:"or,omitempty"`
	Geos *GeoItem `json:"geos,omitempty"`
}

func (*QueryParse) HaveKey

func (c *QueryParse) HaveKey(target string, keyName string) bool

func (*QueryParse) InsertOrReplaces

func (c *QueryParse) InsertOrReplaces(target string, data ...*Kov)

type RedisWork

type RedisWork[T any] struct {
	PlatformName      string                                  // 平台名
	PlatformEng       string                                  // 平台英文名
	Name              string                                  // 任务名称
	Description       string                                  // 更多描述
	BulkIds           []string                                // id列表
	BulkStart         int64                                   // id起始值
	BulkEnd           int64                                   // id截止值
	DefaultStartCount int64                                   // 默认启动数
	DefaultEndCount   int64                                   // 默认截止数值
	NowCount          atomic.Int64                            // 当前已执行个数
	FileName          string                                  // 保存文件名
	Concurrency       uint                                    // 线程数量
	Results           []T                                     // 结果保存
	Running           bool                                    // 当前执行状态
	FetchCall         func(id string) ([]T, error)            // 获取单个
	FetchError        func(id string, err error) ([]T, error) // 获取错误
	ForceStop         bool                                    // 是否被强制停止
	RangeReverse      bool                                    // 范围旋转 默认从小到大 如果设置为true 则是从大到小
	InjectData        map[string]any                          //
	OnSuccess         func(self *RedisWork[T]) error          // 当成功时执行
	OnStartup         func(self *RedisWork[T]) error          // 启动时
	OnRangeStart      func(scopes []string, self *RedisWork[T]) []string
	OnRangSuccess     func(self *RedisWork[T], results []T, resultMap map[string][]T) error
	OnSingleSuccess   func(self *RedisWork[T], results []T, target string) error
	ItemDelayStart    time.Duration // 每获取一个的间隔时间起始点
	ItemDelayEnd      time.Duration // 每获取一个的间隔时间终止点
	RangeDelayStart   time.Duration
	RangeDelayEnd     time.Duration
	Stat              RedisWorkStat
	// contains filtered or unexported fields
}

func NewRedisWork

func NewRedisWork[T any](name string, redisClient rueidiscompat.Cmdable) *RedisWork[T]

func (*RedisWork[T]) Db

func (c *RedisWork[T]) Db() rueidiscompat.Cmdable

func (*RedisWork[T]) GetFirst

func (c *RedisWork[T]) GetFirst() string

func (*RedisWork[T]) GetItemDelay

func (c *RedisWork[T]) GetItemDelay(start time.Duration, end time.Duration) time.Duration

func (*RedisWork[T]) GetLast

func (c *RedisWork[T]) GetLast() string

func (*RedisWork[T]) GetNowCount

func (c *RedisWork[T]) GetNowCount() int64

func (*RedisWork[T]) RedisKey

func (c *RedisWork[T]) RedisKey() string

func (*RedisWork[T]) Run

func (c *RedisWork[T]) Run() error

func (*RedisWork[T]) RunItemDelay

func (c *RedisWork[T]) RunItemDelay()

func (*RedisWork[T]) RunRangeDelay

func (c *RedisWork[T]) RunRangeDelay()

func (*RedisWork[T]) RunRedisItem

func (c *RedisWork[T]) RunRedisItem(redisKey string) error

RunRedisItem 按批次处理单个 会等待批次结束 性能稍弱

func (*RedisWork[T]) RunRedisItemParallel

func (c *RedisWork[T]) RunRedisItemParallel(redisKey string) error

RunRedisItemParallel 多线程并行版 性能高

func (*RedisWork[T]) RunRedisSetRange

func (c *RedisWork[T]) RunRedisSetRange(redisKey string) error

RunRedisSetRange 按范围批次处理 会等待批次结束 性能若

func (*RedisWork[T]) RunSemaphore

func (c *RedisWork[T]) RunSemaphore() error

func (*RedisWork[T]) SetCall

func (c *RedisWork[T]) SetCall(call func(id string) ([]T, error))

func (*RedisWork[T]) SetDb

func (c *RedisWork[T]) SetDb(db rueidiscompat.Cmdable)

func (*RedisWork[T]) SetOnStartup

func (c *RedisWork[T]) SetOnStartup(OnStartup func(self *RedisWork[T]) error)

func (*RedisWork[T]) SetOnSuccess

func (c *RedisWork[T]) SetOnSuccess(OnSuccess func(self *RedisWork[T]) error)

func (*RedisWork[T]) SetRedisKey

func (c *RedisWork[T]) SetRedisKey(redisKey string)

func (*RedisWork[T]) StartTime

func (c *RedisWork[T]) StartTime() time.Time

type RedisWorkStat

type RedisWorkStat struct {
	AllCount     int64
	RangerCount  int64
	BulkCount    int64
	SuccessCount int64
	FailCount    int64
}

type StructConverter

type StructConverter struct {
	CustomIsZero map[reflect.Type]func(reflect.Value, reflect.StructField) bool
	SpecialTypes []reflect.Type // 特殊结构体类型列表
}

StructConverter 作用在于struct中如果需要更新的时候 不要使用bson.M来手动设置字段名 这样又没有编辑器提示 字段变更时也很难同步 可以采取这种方式 特点是会自动去除0值的字段最后只保留有值的产出bson.M 那在某些情况下 会从db中取出存储的哪一行 然后跟当前的进行对比 这时候就使用 diff 的模式 对比两个struct中的不同项进行变更 还有某些情况下 需要从body中取出更新项 需要使用 pipe里的diff算法

func NewStructConverter

func NewStructConverter() *StructConverter

func (*StructConverter) AddCustomIsZero

func (sc *StructConverter) AddCustomIsZero(value interface{}, fn func(reflect.Value, reflect.StructField) bool)

func (*StructConverter) DiffToBsonM

func (sc *StructConverter) DiffToBsonM(original, current interface{}, skips []string, keeps []string) (bson.M, error)

DiffToBsonM 缺点在于如果有内联的struct 如果原始数据中没有定义上层 则无法正确update 比如{city:{name:"新名称"}} 如果原始数据中没有 city字段则无法通过{city.name:"新名称"}进行更新?

func (*StructConverter) InitSpecialTypes

func (sc *StructConverter) InitSpecialTypes()

func (*StructConverter) IsZero

func (sc *StructConverter) IsZero(v reflect.Value, field reflect.StructField) bool

func (*StructConverter) StructToBsonM

func (sc *StructConverter) StructToBsonM(v interface{}, skips []string, keeps []string) (bson.M, error)

type ThreadSafeArray

type ThreadSafeArray[T any] struct {
	// contains filtered or unexported fields
}

ThreadSafeArray 线程安全的数组

func (*ThreadSafeArray[T]) Append

func (t *ThreadSafeArray[T]) Append(value ...T)

func (*ThreadSafeArray[T]) Count

func (t *ThreadSafeArray[T]) Count() int

func (*ThreadSafeArray[T]) Get

func (t *ThreadSafeArray[T]) Get(index int) T

func (*ThreadSafeArray[T]) GetAll

func (t *ThreadSafeArray[T]) GetAll() []T

func (*ThreadSafeArray[T]) Pop

func (t *ThreadSafeArray[T]) Pop(count int) []T

type TrieMatch

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

func NewTrieMatch

func NewTrieMatch() *TrieMatch

func (*TrieMatch) Add

func (t *TrieMatch) Add(pattern string, method string, raw any) error

Add 新增对应路线与方法 相同路径 不同方法都会存在 相同方法会直接覆盖

func (*TrieMatch) Get

func (t *TrieMatch) Get(pattern string) *TrieNode

Get 获取 支持正则

func (*TrieMatch) Match

func (t *TrieMatch) Match(pattern string, method string) (*TrieNode, error)

Match 支持动态参数匹配

func (*TrieMatch) Prefix

func (t *TrieMatch) Prefix() string

func (*TrieMatch) PruneGet

func (t *TrieMatch) PruneGet(pattern string) *TrieNode

PruneGet 仅匹配纯粹输入 不会解析动态参数 eg: /a/:board

func (*TrieMatch) Remove

func (t *TrieMatch) Remove(pattern string)

Remove 仅删除最后一个匹配项 eg : /a/b/c -> /a/b

func (*TrieMatch) RemoveMethod

func (t *TrieMatch) RemoveMethod(pattern, method string)

RemoveMethod 删除匹配项对应的方法 eg: /a/b/c [GET,POST] --> /a/b/c [GET] 但是如果路径下仅此一个方法 则会执行删除路径 eg: /a/b/c [GET] - GET --> /a/b

func (*TrieMatch) SetPrefix

func (t *TrieMatch) SetPrefix(prefix string)

func (*TrieMatch) UpdateRaw

func (t *TrieMatch) UpdateRaw(pattern, method string, raw any)

type TrieNode

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

func (*TrieNode) GetMatchParams

func (c *TrieNode) GetMatchParams() map[string]string

GetMatchParams 获取匹配的参数 :board 但不会校验格式

func (*TrieNode) GetVal

func (c *TrieNode) GetVal(method string) any

func (*TrieNode) IsValidMethod

func (c *TrieNode) IsValidMethod(method string) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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