super

package
v0.0.24 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2023 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToString added in v0.0.20

func BytesToString(b []byte) string

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

func Convert added in v0.0.22

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

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

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

func GetErrorCode added in v0.0.18

func GetErrorCode(err error) (int, error)

GetErrorCode 通过错误引用获取错误码,如果错误不存在则返回 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 If

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

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 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 StringToBytes added in v0.0.20

func StringToBytes(s string) []byte

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

func StringToInt added in v0.0.16

func StringToInt(value string) int

StringToInt 字符串转换为整数

func UnmarshalJSON added in v0.0.21

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

UnmarshalJSON 将 json 转换为对象

Types

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 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