do

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2023 License: MIT Imports: 16 Imported by: 4

README

do

Do something interesting.

Base Go1.18 with generic.

Must

Panic if error is not nil, otherwise return some result except the error.

package main

import (
	"fmt"

	"github.com/donnol/do"
)

func main() {
	do.Must(retErr()) // without result

	// specify result type with type parameter
	_ = do.Must1[int](retErrAndOneResult()) // with one result

	_, _ = do.Must2[int, int](retErrAndTwoResult()) // with two result
}

func retErr() error {
	return fmt.Errorf("a new error")
}

func retErrAndOneResult() (int, error) {
	return 1, fmt.Errorf("a new error")
}

func retErrAndTwoResult() (int, int, error) {
	return 0, 1, fmt.Errorf("a new error")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWorkerIsStop = errors.New("Worker is stop")
	ErrNilJobDo     = errors.New("Job do field is nil")
)

错误

View Source
var DefaultWorker = NewWorker(defaultCount)

DefaultWorker 默认Wroker

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 批量处理查询到的数据

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 并发批量处理查询到的数据

func CodeIs200 added in v0.2.0

func CodeIs200(code int) error

func FindAll added in v0.2.0

func FindAll[S Storer, F Finder[R], R any](db S, finder F, inital R) (r []R, err error)

FindAll sql里select的字段数量必须与R的ScanFields方法返回的数组元素数量一致

func FindFirst added in v0.2.0

func FindFirst[S Storer, F Finder[R], R any](db S, finder F, res *R) (err error)

func FindList added in v0.2.0

func FindList[S Storer, F Finder[R], R any](db S, finder F, res *[]R) (err error)

func FindOne added in v0.2.0

func FindOne[S Storer, F Finder[R], R any](db S, finder F, inital R) (r R, err error)

func FuncName added in v0.2.0

func FuncName(skip int, withFileInfo bool) string

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

func JSONExtractor added in v0.2.0

func JSONExtractor[R any](data []byte) (R, error)

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 MatchError

func MatchError(v any) bool

func Must

func Must(args ...any)

Must args的最后一个是非空错误时panic,无结果返回

func Must1

func Must1[T any](args ...any) (r T)

Must1 args的最后一个是非空错误时panic,返回1个结果

func Must2

func Must2[T1, T2 any](args ...any) (r1 T1, r2 T2)

Must2 args的最后一个是非空错误时panic,返回2个结果

func Must3

func Must3[T1, T2, T3 any](args ...any) (r1 T1, r2 T2, r3 T3)

Must3 args的最后一个是非空错误时panic,返回3个结果

func Must4

func Must4[T1, T2, T3, T4 any](args ...any) (r1 T1, r2 T2, r3 T3, r4 T4)

Must4 args的最后一个是非空错误时panic,返回4个结果

func Must5

func Must5[T1, T2, T3, T4, T5 any](args ...any) (r1 T1, r2 T2, r3 T3, r4 T4, r5 T5)

Must5 args的最后一个是非空错误时panic,返回5个结果

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

func NewError

func NewError[T any](inner T) error

func PrintFields added in v0.2.0

func PrintFields(fields []any)

func SendHTTPRequest added in v0.2.0

func SendHTTPRequest[R any](
	client *http.Client,
	method string,
	link string,
	body io.Reader,
	header http.Header,
	codeChecker CodeChecker,
	extractResult ResultExtractor[R],
) (R, error)

func WrapConnFindAll added in v0.2.0

func WrapConnFindAll[F Finder[R], R any](
	ctx context.Context,
	db *sql.DB,
	finder F,
	inital R,
) (r []R, err error)

WrapConnFindAll query by stmt and args, return values with dest support many rows

func WrapDB added in v0.2.0

func WrapDB(
	ctx context.Context,
	driverName string,
	dataSourceName string,
	f func(
		ctx context.Context,
		conn *sql.DB,
	) error,
) error

func WrapSQLConn added in v0.2.0

func WrapSQLConn(
	ctx context.Context,
	db *sql.DB,
	f func(
		ctx context.Context,
		conn *sql.Conn,
	) error,
) error

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 WrapTx added in v0.2.0

func WrapTx(
	ctx context.Context,
	db *sql.DB,
	f func(
		ctx context.Context,
		tx *sql.Tx,
	) error,
) (err error)

func WrapTxFindAll added in v0.2.0

func WrapTxFindAll[F Finder[R], R any](
	ctx context.Context,
	db *sql.DB,
	finder F,
	inital R,
) (r []R, err error)

func XMLExtractor added in v0.2.0

func XMLExtractor[R any](data []byte) (R, error)

Types

type CodeChecker added in v0.2.0

type CodeChecker func(code int) error

type DoWithCtx added in v0.2.0

type DoWithCtx func(ctx context.Context) error

type Error

type Error[T any] struct {
	// contains filtered or unexported fields
}

func ConvertError

func ConvertError(v any) (*Error[*e], bool)

func (Error[T]) Error

func (e Error[T]) Error() string

func (Error[T]) Inner

func (e Error[T]) Inner() T

type ErrorHandler added in v0.2.0

type ErrorHandler func(error)

ErrorHandler 错误处理方法

type Finder added in v0.2.0

type Finder[R any] interface {
	Query() (query string, args []any) // 返回查询语句,参数

	// 新建结果类型对象,不要使用同一个,用来接收结果
	// r需要是指针类型
	// fields需要是字段指针类型,需要与表的列保持一一对应
	NewScanObjAndFields(colTypes []*sql.ColumnType) (r *R, fields []any)
}

type Job added in v0.2.0

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

Job 工作

func NewJob added in v0.2.0

func NewJob(do DoWithCtx, timeout time.Duration, eh ErrorHandler) *Job

type ResultExtractor added in v0.2.0

type ResultExtractor[R any] func(data []byte) (R, error)

type Storer added in v0.2.0

type Storer interface {
	*sql.DB | *sql.Tx | *sql.Conn
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}

type Worker added in v0.2.0

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

Worker 工人

func NewWorker added in v0.2.0

func NewWorker(n int) *Worker

NewWorker new a worker with limit number

func (*Worker) Push added in v0.2.0

func (w *Worker) Push(job Job) error

Push 添加

func (*Worker) Start added in v0.2.0

func (w *Worker) Start()

Start 开始

func (*Worker) Stop added in v0.2.0

func (w *Worker) Stop()

Stop 停止

Directories

Path Synopsis
cmd
letgo Module

Jump to

Keyboard shortcuts

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