super

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToString added in v0.0.31

func BoolToString(value bool) string

BoolToString bool 转换为字符串

func ByteToString added in v0.0.31

func ByteToString(value byte) string

ByteToString byte 转换为字符串

func BytesToString added in v0.0.20

func BytesToString(b []byte) string

BytesToString 以零拷贝的方式将字节切片转换为字符串

func ConditionalRetryByExponentialBackoff added in v0.3.1

func ConditionalRetryByExponentialBackoff(f func() error, cond func() bool, maxRetries int, baseDelay, maxDelay time.Duration, multiplier, randomization float64) error

ConditionalRetryByExponentialBackoff 该函数与 RetryByExponentialBackoff 类似,但是可以被中断

  • cond 为中断条件,当 cond 返回 false 时,将会中断重试

该函数通常用于在重试过程中,需要中断重试的场景,例如:

  • 用户请求开始游戏,由于网络等情况,进入重试状态。此时用户再次发送开始游戏请求,此时需要中断之前的重试,避免重复进入游戏

func Convert added in v0.0.22

func Convert[A, B any](src A) B

Convert 以零拷贝的方式将一个对象转换为另一个对象

  • 两个对象字段必须完全一致
  • 该函数可以绕过私有字段的访问限制

func Float32ToString added in v0.0.31

func Float32ToString(value float32) string

Float32ToString float32 转换为字符串

func Float64ToString added in v0.0.31

func Float64ToString(value float64) string

Float64ToString float64 转换为字符串

func GetError added in v0.1.6

func GetError(err error) (int, error)

GetError 通过错误引用获取错误码和真实错误信息,如果错误不存在则返回 0,如果错误引用不存在则返回原本的错误

func GoFormat added in v0.0.16

func GoFormat(filePath string)

GoFormat go 代码格式化

func Handle added in v0.0.8

func Handle(f func())

Handle 执行 f 函数,如果 f 为 nil,则不执行

func HandleErr added in v0.0.8

func HandleErr(f func() error) error

HandleErr 执行 f 函数,如果 f 为 nil,则不执行

func HandleV added in v0.0.8

func HandleV[V any](v V, f func(v V))

HandleV 执行 f 函数,如果 f 为 nil,则不执行

func Hostname added in v0.3.0

func Hostname() string

Hostname 获取主机名

func If

func If[T any](expression bool, t T, f T) T

func Int16ToString added in v0.0.31

func Int16ToString(value int16) string

Int16ToString int16 转换为字符串

func Int32ToString added in v0.0.31

func Int32ToString(value int32) string

Int32ToString int32 转换为字符串

func Int64ToString added in v0.0.31

func Int64ToString(value int64) string

Int64ToString int64 转换为字符串

func Int8ToString added in v0.0.31

func Int8ToString(value int8) string

Int8ToString int8 转换为字符串

func IntToString added in v0.0.31

func IntToString(value int) string

IntToString 整数转换为字符串

func IsNumber added in v0.1.2

func IsNumber(v any) bool

IsNumber 判断是否为数字

func LaunchTime added in v0.2.7

func LaunchTime() time.Time

LaunchTime 获取程序启动时间

func MarshalIndentJSON added in v0.0.21

func MarshalIndentJSON(v interface{}, prefix, indent string) []byte

MarshalIndentJSON 将对象转换为 json

func MarshalJSON added in v0.0.21

func MarshalJSON(v interface{}) []byte

MarshalJSON 将对象转换为 json

  • 当转换失败时,将返回 json 格式的空对象

func MarshalJSONE added in v0.1.1

func MarshalJSONE(v interface{}) ([]byte, error)

MarshalJSONE 将对象转换为 json

  • 当转换失败时,将返回错误信息

func MarshalToTargetWithJSON added in v0.2.3

func MarshalToTargetWithJSON(src, dest interface{}) error

MarshalToTargetWithJSON 将对象转换为目标对象

func RegError added in v0.0.18

func RegError(code int, message string) error

RegError 通过错误码注册错误,返回错误的引用

func RegErrorRef added in v0.0.20

func RegErrorRef(code int, message string, ref error) error

RegErrorRef 通过错误码注册错误,返回错误的引用

  • 引用将会被重定向到注册的错误信息

func Retry added in v0.0.8

func Retry(count int, interval time.Duration, f func() error) error

Retry 根据提供的 count 次数尝试执行 f 函数,如果 f 函数返回错误,则在 interval 后重试,直到成功或者达到 count 次数

func RetryAsync added in v0.0.8

func RetryAsync(count int, interval time.Duration, f func() error, callback func(err error))

RetryAsync 与 Retry 类似,但是是异步执行

  • 传入的 callback 函数会在执行完毕后被调用,如果执行成功,则 err 为 nil,否则为错误信息
  • 如果 callback 为 nil,则不会在执行完毕后被调用

func RetryByExponentialBackoff added in v0.2.9

func RetryByExponentialBackoff(f func() error, maxRetries int, baseDelay, maxDelay time.Duration, multiplier, randomization float64) error

RetryByExponentialBackoff 根据指数退避算法尝试执行 f 函数

  • maxRetries:最大重试次数
  • baseDelay:基础延迟
  • maxDelay:最大延迟
  • multiplier:延迟时间的乘数,通常为 2
  • randomization:延迟时间的随机化因子,通常为 0.5

func RetryByRule added in v0.2.9

func RetryByRule(f func() error, rule func(count int) time.Duration) error

RetryByRule 根据提供的规则尝试执行 f 函数,如果 f 函数返回错误,则根据 rule 的返回值进行重试

  • rule 将包含一个入参,表示第几次重试,返回值表示下一次重试的时间间隔,当返回值为 0 时,表示不再重试
  • rule 的 count 将在 f 首次失败后变为 1,因此 rule 的入参将从 1 开始

func RetryForever added in v0.2.2

func RetryForever(interval time.Duration, f func() error)

RetryForever 根据提供的 interval 时间间隔尝试执行 f 函数,如果 f 函数返回错误,则在 interval 后重试,直到成功

func RuneToString added in v0.0.31

func RuneToString(value rune) string

RuneToString rune 转换为字符串

func SliceToString added in v0.0.31

func SliceToString(value []string) string

SliceToString 切片转换为字符串

func StringToBool added in v0.0.31

func StringToBool(value string) bool

StringToBool 字符串转换为 bool

func StringToByte added in v0.0.31

func StringToByte(value string) byte

StringToByte 字符串转换为 byte

func StringToBytes added in v0.0.20

func StringToBytes(s string) []byte

StringToBytes 以零拷贝的方式将字符串转换为字节切片

func StringToFloat32 added in v0.0.31

func StringToFloat32(value string) float32

StringToFloat32 字符串转换为 float32

func StringToFloat64 added in v0.0.31

func StringToFloat64(value string) float64

StringToFloat64 字符串转换为 float64

func StringToInt added in v0.0.16

func StringToInt(value string) int

StringToInt 字符串转换为整数

func StringToInt16 added in v0.0.31

func StringToInt16(value string) int16

StringToInt16 字符串转换为 int16

func StringToInt32 added in v0.0.31

func StringToInt32(value string) int32

StringToInt32 字符串转换为 int32

func StringToInt64 added in v0.0.31

func StringToInt64(value string) int64

StringToInt64 字符串转换为 int64

func StringToInt8 added in v0.0.31

func StringToInt8(value string) int8

StringToInt8 字符串转换为 int8

func StringToRune added in v0.0.31

func StringToRune(value string) rune

StringToRune 字符串转换为 rune

func StringToSlice added in v0.0.31

func StringToSlice(value string) []string

StringToSlice 字符串转换为切片

func StringToUint added in v0.0.31

func StringToUint(value string) uint

StringToUint 字符串转换为 uint

func StringToUint16 added in v0.0.31

func StringToUint16(value string) uint16

StringToUint16 字符串转换为 uint16

func StringToUint32 added in v0.0.31

func StringToUint32(value string) uint32

StringToUint32 字符串转换为 uint32

func StringToUint64 added in v0.0.31

func StringToUint64(value string) uint64

StringToUint64 字符串转换为 uint64

func StringToUint8 added in v0.0.31

func StringToUint8(value string) uint8

StringToUint8 字符串转换为 uint8

func Uint16ToString added in v0.0.31

func Uint16ToString(value uint16) string

Uint16ToString uint16 转换为字符串

func Uint32ToString added in v0.0.31

func Uint32ToString(value uint32) string

Uint32ToString uint32 转换为字符串

func Uint64ToString added in v0.0.31

func Uint64ToString(value uint64) string

Uint64ToString uint64 转换为字符串

func Uint8ToString added in v0.0.31

func Uint8ToString(value uint8) string

Uint8ToString uint8 转换为字符串

func UintToString added in v0.0.31

func UintToString(value uint) string

UintToString uint 转换为字符串

func UnmarshalJSON added in v0.0.21

func UnmarshalJSON(data []byte, v interface{}) error

UnmarshalJSON 将 json 转换为对象

Types

type LossCounter added in v0.2.7

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

func StartLossCounter added in v0.2.7

func StartLossCounter() *LossCounter

StartLossCounter 开始损耗计数

func (*LossCounter) GetLoss added in v0.2.7

func (slf *LossCounter) GetLoss(handler func(step int, name string, loss time.Duration))

GetLoss 获取损耗

func (*LossCounter) Record added in v0.2.7

func (slf *LossCounter) Record(name string)

Record 记录一次损耗

func (*LossCounter) String added in v0.3.0

func (slf *LossCounter) String() string

type Matcher added in v0.0.16

type Matcher[Value, Result any] struct {
	// contains filtered or unexported fields
}

Matcher 匹配器

func Match added in v0.0.16

func Match[Value, Result any](value Value) *Matcher[Value, Result]

Match 匹配

func (*Matcher[Value, Result]) Case added in v0.0.16

func (slf *Matcher[Value, Result]) Case(value Value, result Result) *Matcher[Value, Result]

Case 匹配

func (*Matcher[Value, Result]) Default added in v0.0.16

func (slf *Matcher[Value, Result]) Default(value Result) Result

Default 默认

type Permission added in v0.2.6

type Permission[Code generic.Integer, EntityID comparable] struct {
	// contains filtered or unexported fields
}

func NewPermission added in v0.2.6

func NewPermission[Code generic.Integer, EntityID comparable]() *Permission[Code, EntityID]

NewPermission 创建权限

func (*Permission[Code, EntityID]) AddPermission added in v0.2.6

func (slf *Permission[Code, EntityID]) AddPermission(entityId EntityID, permission ...Code)

AddPermission 添加权限

func (*Permission[Code, EntityID]) HasPermission added in v0.2.6

func (slf *Permission[Code, EntityID]) HasPermission(entityId EntityID, permission Code) bool

HasPermission 是否有权限

func (*Permission[Code, EntityID]) RemovePermission added in v0.2.6

func (slf *Permission[Code, EntityID]) RemovePermission(entityId EntityID, permission ...Code)

RemovePermission 移除权限

func (*Permission[Code, EntityID]) SetPermission added in v0.2.6

func (slf *Permission[Code, EntityID]) SetPermission(entityId EntityID, permission ...Code)

SetPermission 设置权限

type StackGo added in v0.0.13

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

StackGo 用于获取上一个协程调用的堆栈信息

  • 应当最先运行 Wait 函数,然后在其他协程中调用 Stack 函数或者 GiveUp 函数
  • 适用于跨协程同步通讯,例如单线程的消息处理统计耗时打印堆栈信息

func NewStackGo added in v0.0.13

func NewStackGo() *StackGo

NewStackGo 返回一个用于获取上一个协程调用的堆栈信息的收集器

func (*StackGo) GiveUp added in v0.0.13

func (slf *StackGo) GiveUp()

GiveUp 放弃收集消息堆栈

  • 在调用 Wait 函数后调用该函数,将会放弃收集消息堆栈并且释放资源
  • 在调用 GiveUp 函数后调用 Stack 函数,将会 panic

func (*StackGo) Stack added in v0.0.13

func (slf *StackGo) Stack() []byte

Stack 获取消息堆栈

  • 在调用 Wait 函数后调用该函数,将会返回上一个协程的堆栈信息
  • 在调用 GiveUp 函数后调用该函数,将会 panic

func (*StackGo) Wait added in v0.0.13

func (slf *StackGo) Wait()

Wait 等待收集消息堆栈

  • 在调用 Wait 函数后,当前协程将会被挂起,直到调用 Stack 或 GiveUp 函数

type VerifyHandle added in v0.0.23

type VerifyHandle[V any] struct {
	// contains filtered or unexported fields
}

VerifyHandle 校验句柄

func Verify added in v0.0.23

func Verify[V any](handle func(V)) *VerifyHandle[V]

Verify 对特定表达式进行校验,当表达式不成立时,将执行 handle

Example
package main

import (
	"errors"
	"fmt"
	"github.com/kercylan98/minotaur/utils/super"
)

func main() {
	var getId = func() int { return 1 }
	var n *super.VerifyHandle[int]

	super.Verify(func(err error) {
		fmt.Println(err)
	}).Case(getId() == 1, errors.New("id can't be 1")).
		Do()

	super.Verify(func(err error) {
		fmt.Println(err)
	}).PreCase(func() bool {
		return n == nil
	}, errors.New("n can't be nil"), func(verify *super.VerifyHandle[error]) bool {
		return verify.Do()
	})

}
Output:

id can't be 1
n can't be nil

func (*VerifyHandle[V]) Case added in v0.0.23

func (slf *VerifyHandle[V]) Case(expression bool, value V) *VerifyHandle[V]

Case 校验用例,当 expression 成立时,将忽略后续 Case,并将在 Do 时执行 handle,返回 false

func (*VerifyHandle[V]) Do added in v0.0.23

func (slf *VerifyHandle[V]) Do() bool

Do 执行校验,当校验失败时,将执行 handle,并返回 false

func (*VerifyHandle[V]) PreCase added in v0.0.23

func (slf *VerifyHandle[V]) PreCase(expression func() bool, value V, caseHandle func(verify *VerifyHandle[V]) bool) bool

PreCase 先决校验用例,当 expression 成立时,将跳过 caseHandle 的执行,直接执行 handle 并返回 false

  • 常用于对前置参数的空指针校验,例如当 a 为 nil 时,不执行 a.B(),而是直接返回 false

Jump to

Keyboard shortcuts

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