Documentation ¶
Index ¶
- Variables
- func As[T any](v any) T
- func Batch[S Storer, F Finder[R], R any](db S, finder F, batchNum int, handler func([]R) error) (err error)
- func BatchConcurrent[S Storer, F Finder[R], R any](db S, finder F, batchNum int, handler func([]R) error, concNum int) (err error)
- func BytesToString(b []byte) string
- func CodeIs200(code int) error
- func DayZero(t time.Time) time.Time
- func FieldsByColumnType(t any, colTypes []*sql.ColumnType, fieldMapper func(string) string) (fields []any)
- func FindAll[S Storer, F Finder[R], R any](db S, finder F, initial R) (r []R, err error)
- func FindFirst[S Storer, F Finder[R], R any](db S, finder F, res *R) (err error)
- func FindFirstByFunc[S Storer, F FindFunc[R], R any](db S, finder F, res *R) (err error)
- func FindList[S Storer, F Finder[R], R any](db S, finder F, res *[]R) (err error)
- func FindListByFunc[S Storer, F FindFunc[R], R any](db S, finder F, res *[]R) (err error)
- func FindOne[S Storer, F Finder[R], R any](db S, finder F, initial R) (r R, err error)
- func ForgotKey(key string)
- func FuncName(skip int, withFileInfo bool) string
- func HashJoin[K comparable, LE, RE, R any](left []LE, right []RE, lk func(item LE) K, rk func(item RE) K, ...) []R
- func IsComparable[T comparable]()
- func IsExpired(deadline, now time.Time) bool
- func IsZero[T comparable](v T) bool
- func JSONExtractor[R any](data []byte) (R, error)
- func KeyBy[K comparable, E any](collection []E, iteratee func(item E) K) map[K]E
- func KeyValueBy[K comparable, E, V any](collection []E, iteratee func(item E) (K, V)) map[K]V
- func Keys[K comparable, E any](collection map[K]E) []K
- func MatchError(v any) bool
- func MergeKeyValue[K comparable, V any](m1, m2 map[K]V) map[K]V
- func MkdirAllIfNotExist(dir string) error
- func MonthFirst(t time.Time) time.Time
- func Must(err error)
- func Must1[T any](a1 T, err error) T
- func Must2[T1, T2 any](a1 T1, a2 T2, err error) (T1, T2)
- func Must3[T1, T2, T3 any](a1 T1, a2 T2, a3 T3, err error) (T1, T2, T3)
- func Must4[T1, T2, T3, T4 any](a1 T1, a2 T2, a3 T3, a4 T4, err error) (T1, T2, T3, T4)
- func Must5[T1, T2, T3, T4, T5 any](a1 T1, a2 T2, a3 T3, a4 T4, a5 T5, err error) (T1, T2, T3, T4, T5)
- func NestedJoin[J, K, R any](left []J, right []K, match func(J, K) bool, mapper func(J, K) R) []R
- func NewError[T any](inner T) error
- func ParseTime(t string, layouts ...string) (r time.Time, err error)
- func PrintFields(fields []any)
- func ProxyTraceBegin(pctx ProxyContext) (stop func())
- func RegisterProxyMethod(pctx ProxyContext, cf ProxyCtxFunc, typeParams ...string)
- func RegisterProxyTracer(tracers ...Tracer)
- func RetryWithDeadline(ctx context.Context, d time.Time, f Doer) error
- func RetryWithTimes(ctx context.Context, tryTimes int, f Doer) error
- func SQLProcess(funcs ...SQLQueryFunc) (query SQLQuery, args SQLArgs)
- func SQLProcessRaw(funcs ...SQLQueryFunc) (query string, args []any)
- func SendHTTPRequest[R any](client *http.Client, method string, link string, body io.Reader, ...) (R, error)
- func SingleFlight[R any](key string, fn SingleFlightCall[R]) (r R, err error)
- func StringToBytes(s string) []byte
- func TCPProxy(localAddr, remoteAddr string, handlers ...func(lconn, rconn net.Conn)) (err error)
- func TCPProxyDefaultHandler(lconn, rconn net.Conn)
- func TCPRecv(localAddr string, handler func(conn net.Conn)) (err error)
- func TCPSend(remoteAddr string, handler func(conn net.Conn) error) (err error)
- func ThisMonthFirst() time.Time
- func ThisYearFirst() time.Time
- func TodayZero() time.Time
- func ValueAs[K comparable, T any](m *Map[K, any], key K) T
- func Values[K comparable, E any](collection map[K]E) []E
- func WrapConnFindAll[F Finder[R], R any](ctx context.Context, db *sql.DB, finder F, initial R) (r []R, err error)
- func WrapDB(ctx context.Context, driverName string, dataSourceName string, ...) error
- func WrapSQLConn(ctx context.Context, db *sql.DB, ...) error
- func WrapSQLQueryRows(ctx context.Context, db *sql.DB, stmt string, args []interface{}, ...) error
- func WrapTx(ctx context.Context, db *sql.DB, f func(ctx context.Context, tx *sql.Tx) error) (err error)
- func WrapTxFindAll[F Finder[R], R any](ctx context.Context, db *sql.DB, finder F, initial R) (r []R, err error)
- func XMLExtractor[R any](data []byte) (R, error)
- func YearFirst(t time.Time) time.Time
- type CodeChecker
- type DoWithCtx
- type Doer
- type Error
- type ErrorHandler
- type Field
- type FindFunc
- type Finder
- type Job
- type KeyOption
- type Map
- type ProxyContext
- type ProxyCtxFunc
- type ProxyCtxFuncStore
- type ResultExtractor
- type SQLArgs
- type SQLQuery
- type SQLQueryFunc
- type SingleFlightCall
- type Slice
- type Storer
- type Struct
- type StructCommentEntity
- type TimeTracer
- type Tracer
- type Worker
Constants ¶
This section is empty.
Variables ¶
var ( ErrWorkerIsStop = errors.New("Worker is stop") ErrNilJobDo = errors.New("Job do field is nil") )
var (
ErrNilDoer = errors.New("f is nil")
)
Functions ¶
func Batch ¶ added in v0.2.0
func Batch[S Storer, F Finder[R], R any](db S, finder F, batchNum int, handler func([]R) error) (err error)
Batch process data find from Storer in batches
func BatchConcurrent ¶ added in v0.2.0
func BatchConcurrent[S Storer, F Finder[R], R any](db S, finder F, batchNum int, handler func([]R) error, concNum int) (err error)
BatchConcurrent batch process data concurrently
func BytesToString ¶ added in v0.5.0
BytesToString converts byte slice to string without a memory allocation.
func FieldsByColumnType ¶ added in v0.10.0
func FieldsByColumnType(t any, colTypes []*sql.ColumnType, fieldMapper func(string) string) (fields []any)
FieldsByColumnType t is a struct pointer, and use it's field match column name to receive scan value. It will use db tag to get column name first, or lower case field name. You can specify fieldMapper to control column name with field name
func FindFirstByFunc ¶ added in v0.16.0
func FindListByFunc ¶ added in v0.16.0
func HashJoin ¶ added in v0.2.0
func HashJoin[K comparable, LE, RE, R any]( left []LE, right []RE, lk func(item LE) K, rk func(item RE) K, mapper func(LE, RE) R, ) []R
HashJoin like hash join
func IsComparable ¶ added in v0.8.0
func IsComparable[T comparable]()
IsComparable check if a type is comparable in compile time
func IsExpired ¶ added in v0.11.0
IsExpired show if deadline is expired compared to now always return false if deadline is zero
func IsZero ¶ added in v0.12.0
func IsZero[T comparable](v T) bool
func JSONExtractor ¶ added in v0.2.0
func KeyBy ¶ added in v0.2.0
func KeyBy[K comparable, E any](collection []E, iteratee func(item E) K) map[K]E
KeyBy slice to map, key specified by iteratee, value is slice element
func KeyValueBy ¶ added in v0.2.0
func KeyValueBy[K comparable, E, V any](collection []E, iteratee func(item E) (K, V)) map[K]V
KeyValueBy slice to map, key value specified by iteratee
func Keys ¶ added in v0.8.0
func Keys[K comparable, E any](collection map[K]E) []K
func MatchError ¶
func MergeKeyValue ¶ added in v0.15.0
func MergeKeyValue[K comparable, V any](m1, m2 map[K]V) map[K]V
MergeKeyValue merge m2 into m1, will override m1 key value if both exists
func MkdirAllIfNotExist ¶ added in v0.8.0
func Must5 ¶
func Must5[T1, T2, T3, T4, T5 any](a1 T1, a2 T2, a3 T3, a4 T4, a5 T5, err error) (T1, T2, T3, T4, T5)
Must5 panic if err is not nill,or return 5 result
func NestedJoin ¶ added in v0.2.0
func NestedJoin[J, K, R any]( left []J, right []K, match func(J, K) bool, mapper func(J, K) R, ) []R
NestedJoin like nested loop join
func ParseTime ¶ added in v0.18.0
ParseTime parse time string t with layout s one by one; if layouts is empty, it will use "2006-01-02 15:04:05" as default
func PrintFields ¶ added in v0.2.0
func PrintFields(fields []any)
func ProxyTraceBegin ¶ added in v0.13.0
func ProxyTraceBegin(pctx ProxyContext) (stop func())
ProxyTraceBegin LIFO
func RegisterProxyMethod ¶ added in v0.14.0
func RegisterProxyMethod(pctx ProxyContext, cf ProxyCtxFunc, typeParams ...string)
RegisterProxyMethod 注册代理方法,根据包名+接口名+方法名唯一对应一个方法;在有了泛型后还要加上类型参数,唯一键变为包名+接口名+方法名+TP1,TP2,...
func RegisterProxyTracer ¶ added in v0.13.0
func RegisterProxyTracer(tracers ...Tracer)
func RetryWithDeadline ¶ added in v0.5.0
RetryWithDeadline retry f before d exceeds if f failed
func RetryWithTimes ¶ added in v0.5.0
RetryWithTimes retry f tryTimes times if f failed
func SQLProcess ¶ added in v0.17.0
func SQLProcess(funcs ...SQLQueryFunc) (query SQLQuery, args SQLArgs)
SQLProcess process sql and args one by one, add more parts based on first sql
func SQLProcessRaw ¶ added in v0.17.1
func SQLProcessRaw(funcs ...SQLQueryFunc) (query string, args []any)
func SendHTTPRequest ¶ added in v0.2.0
func SingleFlight ¶ added in v0.6.0
func SingleFlight[R any](key string, fn SingleFlightCall[R]) (r R, err error)
SingleFlight make sure only one request is doing with one key
func StringToBytes ¶ added in v0.5.0
StringToBytes converts string to byte slice without a memory allocation.
func TCPProxy ¶ added in v0.19.0
TCPProxy listen localAddr and transfer any request to remoteAddr.We can use handlers to specify one custom func to transfer data.
func TCPProxyDefaultHandler ¶ added in v0.20.0
func TCPRecv ¶ added in v0.19.0
TCPRecv recv from local addr with handler, it will block on (*net.TCPListener).Accept
func ThisMonthFirst ¶ added in v0.12.0
func ThisYearFirst ¶ added in v0.12.0
func ValueAs ¶ added in v0.8.0
func ValueAs[K comparable, T any](m *Map[K, any], key K) T
ValueAs get value by key from *Map[K, any], and assert value type to T
func Values ¶ added in v0.8.0
func Values[K comparable, E any](collection map[K]E) []E
func WrapConnFindAll ¶ added in v0.2.0
func WrapConnFindAll[F Finder[R], R any]( ctx context.Context, db *sql.DB, finder F, initial R, ) (r []R, err error)
WrapConnFindAll query by stmt and args, return values with dest support many rows
func WrapSQLConn ¶ added in v0.2.0
func WrapSQLQueryRows ¶ added in v0.2.0
func WrapSQLQueryRows( ctx context.Context, db *sql.DB, stmt string, args []interface{}, dest ...interface{}, ) error
WrapSQLQueryRows query by stmt and args, return values with dest only support one row
func WrapTxFindAll ¶ added in v0.2.0
func XMLExtractor ¶ added in v0.2.0
Types ¶
type CodeChecker ¶ added in v0.2.0
type Error ¶
type Error[T any] struct { // contains filtered or unexported fields }
Error is a error type with any element
func ConvertError ¶
type ErrorHandler ¶ added in v0.2.0
type ErrorHandler func(error)
type Field ¶ added in v0.9.0
type Field struct { reflect.StructField // 内嵌反射结构体字段类型 Comment string // 注释 Struct Struct // 字段的类型是其它结构体 }
type FindFunc ¶ added in v0.16.0
type FindFunc[R any] func() (query string, args []any, genObj func(colTypes []*sql.ColumnType) (r *R, fields []any))
FindFunc return query, args, and object generator; object generator should return new object and it's related fields when called; FindFunc is a Finder too.
func (FindFunc[R]) NewScanObjAndFields ¶ added in v0.16.0
func (f FindFunc[R]) NewScanObjAndFields(colTypes []*sql.ColumnType) (r *R, fields []any)
type Finder ¶ added in v0.2.0
type Finder[R any] interface { // return query sql and args Query() (query string, args []any) // new a result type object, not the same, to receive every row // fields must be pointer type of result object's field, and it is match with query sql's select column one by one NewScanObjAndFields(colTypes []*sql.ColumnType) (r *R, fields []any) }
type Map ¶ added in v0.6.0
type Map[K comparable, T any] struct { // contains filtered or unexported fields }
type ProxyContext ¶ added in v0.13.0
每个包、每个接口、每个方法唯一对应一个方法
func (ProxyContext) IsEmpty ¶ added in v0.13.0
func (pctx ProxyContext) IsEmpty() bool
func (ProxyContext) LogShortf ¶ added in v0.13.0
func (pctx ProxyContext) LogShortf(format string, args ...any)
func (ProxyContext) Logf ¶ added in v0.13.0
func (pctx ProxyContext) Logf(format string, args ...any)
func (ProxyContext) String ¶ added in v0.13.0
func (pctx ProxyContext) String() string
func (ProxyContext) Uniq ¶ added in v0.13.0
func (pctx ProxyContext) Uniq() string
type ProxyCtxFunc ¶ added in v0.13.0
type ProxyCtxFunc func(ctx ProxyContext, method any, args []any) (res []any)
ProxyCtxFunc 对于method: func(string, int) (int, error) f := method.(func(string, int) (int, error)) a1 := args[0].(string) a2 := args[1].(int) r1, r2 := f(a1, a2) res = append(res, r1, r2)
type ProxyCtxFuncStore ¶ added in v0.13.0
type ProxyCtxFuncStore struct {
// contains filtered or unexported fields
}
func GlobalProxyCtxMap ¶ added in v0.14.0
func GlobalProxyCtxMap() *ProxyCtxFuncStore
func NewProxyCtxMap ¶ added in v0.13.0
func NewProxyCtxMap() *ProxyCtxFuncStore
func (*ProxyCtxFuncStore) Lookup ¶ added in v0.13.0
func (m *ProxyCtxFuncStore) Lookup(pctx ProxyContext, typeParams ...string) (ProxyCtxFunc, bool)
func (*ProxyCtxFuncStore) Set ¶ added in v0.13.0
func (m *ProxyCtxFuncStore) Set(pctx ProxyContext, f ProxyCtxFunc, typeParams ...string)
type ResultExtractor ¶ added in v0.2.0
type SQLQuery ¶ added in v0.17.0
type SQLQuery string
type SQLQueryFunc ¶ added in v0.17.0
type SingleFlightCall ¶ added in v0.6.0
type Struct ¶ added in v0.9.0
type Struct struct { Name string // 名字 Comment string // 注释 Description string // 描述 Type reflect.Type // 反射类型 Fields []Field // 结构体字段 }
func MakeStruct ¶ added in v0.9.0
func MakeStruct() Struct
func ResolveStruct ¶ added in v0.9.0
type StructCommentEntity ¶ added in v0.9.0
type TimeTracer ¶ added in v0.13.0
type TimeTracer struct {
// contains filtered or unexported fields
}
func (*TimeTracer) Begin ¶ added in v0.13.0
func (impl *TimeTracer) Begin()
func (*TimeTracer) New ¶ added in v0.13.0
func (impl *TimeTracer) New(pctx ProxyContext) Tracer
func (*TimeTracer) Stop ¶ added in v0.13.0
func (impl *TimeTracer) Stop()
type Tracer ¶ added in v0.13.0
type Tracer interface { New(ProxyContext) Tracer // 新建Tracer,每个方法调用均新建一个 Begin() Stop() }