utils

package module
v0.0.0-...-e4a6f1e Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: MIT Imports: 22 Imported by: 0

README

utils

simple go gin utils

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 ArrayContains

func ArrayContains[T ArrayType](arr *Array[T], e T) bool

func ArrayDelete

func ArrayDelete[T ArrayType](arr *Array[T], e T) bool

func ArrayDeleteMany

func ArrayDeleteMany[T ArrayType](arr *Array[T], e ...T)

func ArrayIndexOf

func ArrayIndexOf[T ArrayType](arr *Array[T], e T) int

func ArrayJoin

func ArrayJoin(arr *Array[string], sep string) string

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 EnsureFoundNoError

func EnsureFoundNoError(found bool, err error)

func EnsureIgnoreFirstNoError

func EnsureIgnoreFirstNoError(count any, err error)

func EnsureNoError

func EnsureNoError(err error)

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 GetEnvMap

func GetEnvMap() map[EnvKey]any

func InitSnowflake

func InitSnowflake(node int64) error

func MakeUploadHash

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

func ResolveCmd

func ResolveCmd(cmd string) string

func SnowCode

func SnowCode() string

func SnowId

func SnowId() int64

func SnowWithPrefix

func SnowWithPrefix(prefix string, id *int64) string

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

Types

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 ArrayType

type ArrayType interface {
	string | int
}

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
	DefaultValue any
}

func Getter

func Getter(key EnvKey) *EnvGetter

func GetterDefault

func GetterDefault[T int | uint | int64 | uint64 | bool | string](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) String

func (getter *EnvGetter) String() string

func (*EnvGetter) UInt

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

func (*EnvGetter) UInt64

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

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 PageInfo

type PageInfo struct {
	PageNumber int    `json:"pageNumber,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 SpawnOptions

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

Jump to

Keyboard shortcuts

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