utils

package
v1.0.52 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// 200
	Success = NewError(http.StatusOK, http.StatusText(http.StatusOK))
	// 500
	InternalServerError = NewError(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
	NotImplemented      = NewError(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented))
	ServiceUnavailable  = NewError(http.StatusServiceUnavailable, http.StatusText(http.StatusServiceUnavailable)) // 访问了服务器不存在的资源
	// 400
	BadRequest   = NewError(http.StatusBadRequest, http.StatusText(http.StatusBadRequest))     // 客户端请求错误
	Unauthorized = NewError(http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized)) // 需要认证
	NotFound     = NewError(http.StatusNotFound, http.StatusText(http.StatusNotFound))         // 服务器不可访问
	Forbidden    = NewError(http.StatusForbidden, http.StatusText(http.StatusForbidden))       // 禁止访问
)
View Source
var (
	ErrInputParamsInvalid      ClientErrorCode = "input.paramsInvalid"      // 参数错误
	ErrDataDuplicate           ServerErrorCode = "data.duplicate"           // 数据重复
	ErrDataExists              ServerErrorCode = "data.exists"              // 数据已存在
	ErrDataNoFound             ServerErrorCode = "data.notFound"            // 数据不存在
	ErrAuthAccessTokenInvalid  ClientErrorCode = "auth.accessTokenInvalid"  // 访问令牌错误
	ErrAuthLoginFail           ServerErrorCode = "auth.loginFail"           // 登录失败
	ErrAuthRefreshTokenInvalid ServerErrorCode = "auth.refreshTokenInvalid" // 刷新令牌失败
)

Functions

func Call added in v1.0.42

func Call(f FuncOne) (err error)

func Call2 added in v1.0.42

func Call2[T any](f func() (T, error)) (res T, err error)

func Call3 added in v1.0.42

func Call3[T any, V any](f FuncThree[T, V]) (res T, res2 V, err error)

func CallNoop added in v1.0.42

func CallNoop(f FuncNoop) error

func CallType added in v1.0.42

func CallType[T any](f FuncType[T]) (T, error)

func CheckPassword

func CheckPassword(origin, password string) (isValid bool, err error)

func CheckUploadHash

func CheckUploadHash(uploadId, targetId int64, hash string) (bool, error)

func CheckUploadToken

func CheckUploadToken(account string, ts int64, id int64, mod int8, path string, token string) (bool, error)

func CreateCaptcha

func CreateCaptcha(num int) string

func CreatePassword

func CreatePassword(password string) (string, error)

func CreateUploadToken

func CreateUploadToken(account string, ts int64, id int64, mod int8, path string) (string, error)

func EnsureCountNoError

func EnsureCountNoError(count int64, err error)

func EnsureFound added in v1.0.48

func EnsureFound(value any)

func EnsureFoundNoError

func EnsureFoundNoError(found bool, err error)

func EnsureIgnoreFirstNoError

func EnsureIgnoreFirstNoError(count any, err error)

func EnsureNoError

func EnsureNoError(err error)

func FiName added in v1.0.50

func FiName(fi fs.FileInfo) string

func FormatEmail

func FormatEmail(email string) string

func FormatPhone

func FormatPhone(phone string) string

func GenerateHMAC

func GenerateHMAC(text string, key string) (string, error)

func GetEnv

func GetEnv(name EnvKey) string

func GetEnvBool

func GetEnvBool(name EnvKey) bool

func GetEnvBoolDefault

func GetEnvBoolDefault(name EnvKey, defaultValue bool) bool

func GetEnvDefault

func GetEnvDefault(name EnvKey, defaultValue string) string

func GetEnvInt

func GetEnvInt[T int | uint | int64 | uint64](name EnvKey) T

func GetEnvIntDefault

func GetEnvIntDefault[T int | uint | int64 | uint64](name EnvKey, defaultValue T) T

func HashUuid added in v1.0.46

func HashUuid() string

func InitSnowflake

func InitSnowflake(node int64) error

func IntToStrWithPad added in v1.0.50

func IntToStrWithPad(num int, length int, pad string) string

func MakeUploadHash

func MakeUploadHash(uploadId, targetId int64) (string, error)

func RandomChar added in v1.0.46

func RandomChar(n int) string

func RandomCharset added in v1.0.46

func RandomCharset(n int, charset string) string

func RandomNumber added in v1.0.46

func RandomNumber(n int) string

func RandomString added in v1.0.46

func RandomString(n int) string

func ResolveCmd

func ResolveCmd(cmd string) string

func ShortUuid added in v1.0.46

func ShortUuid() string

func SliceExchange added in v1.0.50

func SliceExchange[T any, V any](s []T, exchanger SliceExchangRunner[T, V]) []V

func SliceFilter added in v1.0.50

func SliceFilter[T any](s []T, filter SliceFilterRunner[T]) []T

func SnowCode

func SnowCode() string

func SnowId

func SnowId() int64

func SnowWithPrefix

func SnowWithPrefix(prefix string, id *int64) string

func SortSlice added in v1.0.50

func SortSlice[T any](list []T, cmp SortCompare[T])

func SortSliceStable added in v1.0.50

func SortSliceStable[T any](list []T, cmp SortCompare[T])

func Spawn

func Spawn(cmd string, args []string, opt *SpawnOptions) (spawnOutput, error)

func SpawnSimple

func SpawnSimple(cmd string, args []string) (spawnOutput, error)

func SqlColAs

func SqlColAs(field string, field2 string) string

func SqlDeleted

func SqlDeleted() string

func SqlNotDeleted

func SqlNotDeleted() string

func SqlWhereEq

func SqlWhereEq(field string) string

func SqlWhereGt

func SqlWhereGt(field string) string

func SqlWhereIsNull

func SqlWhereIsNull(field string) string

func SqlWhereLike

func SqlWhereLike(field string) string

func SqlWherePrefix

func SqlWherePrefix(field string) string

func StrPad added in v1.0.50

func StrPad(s string, length int, pad string) string

func StrPadPrefix added in v1.0.50

func StrPadPrefix(s string, length int, pad string) string

func TimeStamp

func TimeStamp() string

func Uuid

func Uuid() string

func ValueOf

func ValueOf[T any](value *T) T

func ValueTo

func ValueTo[T any](value T) *T

func VerifyHMAC

func VerifyHMAC(HMAC string, text string, key string) (bool, error)

func WithRecover

func WithRecover(fn func() error) (err error)

func Wrap added in v1.0.42

func Wrap(f FuncOne) func() error

func Wrap2 added in v1.0.42

func Wrap2[T any](f FuncTwo[T]) func() (T, error)

func Wrap3 added in v1.0.42

func Wrap3[T any, V any](f FuncThree[T, V]) func() (T, V, error)

Types

type AnySlice added in v1.0.50

type AnySlice[T any, V any] []T

func (AnySlice[T, V]) Exchange added in v1.0.50

func (s AnySlice[T, V]) Exchange(exchanger SliceExchangRunner[T, V]) []V

func (AnySlice[T, V]) Filter added in v1.0.50

func (s AnySlice[T, V]) Filter(filter SliceFilterRunner[T]) AnySlice[T, V]

type AnySort added in v1.0.50

type AnySort[T any, V cmp.Ordered] []T

func (AnySort[T, V]) SortSlice added in v1.0.50

func (s AnySort[T, V]) SortSlice(sc SortCompare[T])

func (AnySort[T, V]) SortSlicePeek added in v1.0.50

func (s AnySort[T, V]) SortSlicePeek(pk SortPeek[T, V])

func (AnySort[T, V]) SortSliceStable added in v1.0.50

func (s AnySort[T, V]) SortSliceStable(sc SortCompare[T])

func (AnySort[T, V]) SortSliceStablePeek added in v1.0.50

func (s AnySort[T, V]) SortSliceStablePeek(pk SortPeek[T, V])

type Array

type Array[T any] []T

数组定义

func GetDbStructKeys

func GetDbStructKeys(ins any) (res Array[string])

func GetStructKeys

func GetStructKeys(ins interface{}, tagName string) (res Array[string])

func GetXormStructKeys

func GetXormStructKeys(ins any) (res Array[string])

func (*Array[T]) Concat

func (arr *Array[T]) Concat(next Array[T])

数组合并

func (*Array[T]) Filter

func (arr *Array[T]) Filter(f func(any any) bool) Array[T]

过滤器

func (*Array[T]) ForEach

func (arr *Array[T]) ForEach(f func(e any))

迭代器

func (*Array[T]) Insert

func (arr *Array[T]) Insert(i int, e ...T)

任意位置插入元素

func (*Array[T]) Pop

func (arr *Array[T]) Pop() any

删除末尾元素

func (*Array[T]) Push

func (arr *Array[T]) Push(e ...T)

向末尾添加元素

func (*Array[T]) Remove

func (arr *Array[T]) Remove(i int)

删除任意位置元素

func (*Array[T]) Shift

func (arr *Array[T]) Shift() *T

删除数组开头元素

func (*Array[T]) Unshift

func (arr *Array[T]) Unshift(e ...T)

向头部添加元素

type ClientErrorCode

type ClientErrorCode string

客户端错误

func (ClientErrorCode) Error

func (code ClientErrorCode) Error() *Error

func (ClientErrorCode) Panic

func (code ClientErrorCode) Panic()

func (ClientErrorCode) PanicError

func (code ClientErrorCode) PanicError(err error)

type Data

type Data[T any] struct {
	Data     T         `json:"data,omitempty"`
	Code     string    `json:"code"`
	Message  string    `json:"message"`
	PageInfo *PageInfo `json:"pageInfo,omitempty"`
}

type DeleteStatus

type DeleteStatus int8
const (
	DeleteStatusDefault DeleteStatus = 0
	DeleteStatusDeleted DeleteStatus = 1
	DeleteStatusDroped  DeleteStatus = 2
)

type EnvGetter

type EnvGetter struct {
	Key EnvKey
	// contains filtered or unexported fields
}

func Getter

func Getter(key EnvKey) *EnvGetter

func GetterDefault

func GetterDefault[T int | int8 | int16 | int32 | int64 |
	uint | uint8 | uint16 | uint32 | uint64 |
	string | bool](key EnvKey, defaultValue T) *EnvGetter

func (*EnvGetter) Bool

func (getter *EnvGetter) Bool() bool

func (*EnvGetter) Int

func (getter *EnvGetter) Int() (r int)

func (*EnvGetter) Int64

func (getter *EnvGetter) Int64() (r int64)

func (*EnvGetter) KeyName

func (getter *EnvGetter) KeyName() string

func (*EnvGetter) Kind added in v1.0.14

func (getter *EnvGetter) Kind() reflect.Kind

func (*EnvGetter) String

func (getter *EnvGetter) String() string

func (*EnvGetter) UInt

func (getter *EnvGetter) UInt() (r uint)

func (*EnvGetter) UInt64

func (getter *EnvGetter) UInt64() (r uint64)

func (*EnvGetter) Value added in v1.0.13

func (getter *EnvGetter) Value() any

type EnvKey

type EnvKey string

type Error

type Error struct {
	StatusCode int    `json:"-"`
	Code       string `json:"code"`
	Message    string `json:"message"`
	Stack      error  `json:"-"`
}

错误

func ClientError

func ClientError(code ClientErrorCode) *Error

func NewError

func NewError(statusCode int, message string) *Error

func ServerError

func ServerError(code ServerErrorCode) *Error

func (*Error) Clone

func (e *Error) Clone() *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) GetError

func (e *Error) GetError() error

func (*Error) GinError

func (e *Error) GinError() (int, error)

func (*Error) JsonError

func (e *Error) JsonError() (int, interface{})

func (*Error) Panic

func (e *Error) Panic()

func (*Error) PanicError

func (e *Error) PanicError(err error)

func (*Error) WithCode

func (e *Error) WithCode(code string) *Error

func (*Error) WithMessage

func (e *Error) WithMessage(message string) *Error

func (*Error) WithStack

func (e *Error) WithStack(stack error) *Error

type FuncNoop added in v1.0.42

type FuncNoop func()

type FuncOne added in v1.0.42

type FuncOne func() error

type FuncThree added in v1.0.42

type FuncThree[T any, V any] func() (T, V, error)

type FuncTwo added in v1.0.42

type FuncTwo[T any] func() (T, error)

type FuncType added in v1.0.42

type FuncType[T any] func() T

type PageInfo

type PageInfo struct {
	Current   int    `json:"current,omitempty"`
	PageSize  int    `json:"pageSize,omitempty"`
	Total     int64  `json:"total,omitempty"`
	PageToken string `json:"token,omitempty"`
}

func (PageInfo) Offset

func (s PageInfo) Offset() int

type Runner

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

func NewRunner

func NewRunner(fn func(a any)) *Runner

func (*Runner) Close

func (s *Runner) Close()

func (*Runner) Push

func (s *Runner) Push(data any)

func (*Runner) PushList

func (s *Runner) PushList(data ...any)

func (*Runner) SetRunner

func (s *Runner) SetRunner(fn func(a any))

func (*Runner) Start

func (s *Runner) Start()

func (*Runner) Wait

func (s *Runner) Wait()

type ServerErrorCode

type ServerErrorCode string

服务端错误

func (ServerErrorCode) Error

func (code ServerErrorCode) Error() *Error

func (ServerErrorCode) Panic

func (code ServerErrorCode) Panic()

func (ServerErrorCode) PanicError

func (code ServerErrorCode) PanicError(err error)

type SimpleArray added in v1.0.51

type SimpleArray[T SimpleType] []T

func (*SimpleArray[T]) Concat added in v1.0.51

func (arr *SimpleArray[T]) Concat(next SimpleArray[T])

func (SimpleArray[T]) Contains added in v1.0.51

func (arr SimpleArray[T]) Contains(e T) bool

func (*SimpleArray[T]) Delete added in v1.0.51

func (arr *SimpleArray[T]) Delete(e T) bool

func (SimpleArray[T]) DeleteAll added in v1.0.52

func (arr SimpleArray[T]) DeleteAll(e T) int

func (*SimpleArray[T]) DeleteMany added in v1.0.51

func (arr *SimpleArray[T]) DeleteMany(e ...T) int

func (SimpleArray[T]) DeleteManyAll added in v1.0.52

func (arr SimpleArray[T]) DeleteManyAll(e ...T) int

func (SimpleArray[T]) Filter added in v1.0.51

func (arr SimpleArray[T]) Filter(f func(T) bool) SimpleArray[T]

func (SimpleArray[T]) ForEach added in v1.0.51

func (arr SimpleArray[T]) ForEach(f func(e T))

func (SimpleArray[T]) IndexOf added in v1.0.51

func (arr SimpleArray[T]) IndexOf(e T) int

func (*SimpleArray[T]) Insert added in v1.0.51

func (arr *SimpleArray[T]) Insert(i int, e ...T)

func (SimpleArray[T]) Join added in v1.0.51

func (arr SimpleArray[T]) Join(sep string) string

func (SimpleArray[T]) LastIndexOf added in v1.0.51

func (arr SimpleArray[T]) LastIndexOf(e T) int

func (SimpleArray[T]) Map added in v1.0.51

func (arr SimpleArray[T]) Map(f func(T) T) SimpleArray[T]

func (*SimpleArray[T]) Push added in v1.0.51

func (arr *SimpleArray[T]) Push(e ...T)

func (*SimpleArray[T]) Remove added in v1.0.51

func (arr *SimpleArray[T]) Remove(i int)

func (*SimpleArray[T]) Unshift added in v1.0.51

func (arr *SimpleArray[T]) Unshift(e ...T)

type SimpleType added in v1.0.51

type SimpleType interface {
	~string | ~bool | ~int8 | ~int16 | ~int32 | ~int64 | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~int
}

type SliceExchangRunner added in v1.0.50

type SliceExchangRunner[T any, V any] func(T) V

type SliceFilterRunner added in v1.0.50

type SliceFilterRunner[T any] func(T) bool

type SortCompare added in v1.0.50

type SortCompare[T any] func(T, T) bool

type SortPeek added in v1.0.50

type SortPeek[T any, V cmp.Ordered] func(T) V

type SpawnOptions

type SpawnOptions struct {
	// 使用程序std输入输入
	SystemStdout bool
}

type SqlFieldType added in v1.0.8

type SqlFieldType string

func Field added in v1.0.8

func Field(field string) SqlFieldType

func (SqlFieldType) Eq added in v1.0.8

func (s SqlFieldType) Eq(value any) (string, any)

func (SqlFieldType) Gt added in v1.0.8

func (s SqlFieldType) Gt(value any) (string, any)

func (SqlFieldType) Gte added in v1.0.8

func (s SqlFieldType) Gte(value any) (string, any)

func (SqlFieldType) IsNull added in v1.0.8

func (s SqlFieldType) IsNull() string

func (SqlFieldType) Like added in v1.0.8

func (s SqlFieldType) Like(value any) (string, any)

func (SqlFieldType) Lt added in v1.0.8

func (s SqlFieldType) Lt(value any) (string, any)

func (SqlFieldType) Lte added in v1.0.8

func (s SqlFieldType) Lte(value any) (string, any)

func (SqlFieldType) Neq added in v1.0.8

func (s SqlFieldType) Neq(value any) (string, any)

func (SqlFieldType) NotIsNull added in v1.0.8

func (s SqlFieldType) NotIsNull() string

func (SqlFieldType) OrderAsc added in v1.0.8

func (s SqlFieldType) OrderAsc() string

xorm.OrderBy field asc

func (SqlFieldType) OrderDesc added in v1.0.8

func (s SqlFieldType) OrderDesc() string

xorm.OrderBy field desc

func (SqlFieldType) Prefix added in v1.0.8

func (s SqlFieldType) Prefix(value any) (string, any)

Jump to

Keyboard shortcuts

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