do

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 19 Imported by: 4

README

do

GoDoc Go Report Card

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(retErrAndOneResult()) // with one result

	_, _ = do.Must2(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")
}

Slice to map by key

r := KeyValueBy([]string{"a", "aa", "aaa"}, func(str string) (string, int) {
	return str, len(str)
})
want := map[string]int{"a": 1, "aa": 2, "aaa": 3}
// r is what we want.

Join

r := NestedJoin([]Book{
	{Id: 1, Title: "hello", Author: 1},
	{Id: 2, Title: "world", Author: 1},
	{Id: 3, Title: "good", Author: 2},
	{Id: 4, Title: "job", Author: 2},
}, []User{
	{Id: 1, Name: "jd"},
	{Id: 2, Name: "jc"},
}, UserBookMatcher, func(j Book, k User) BookWithUser {
	return BookWithUser{
		Book:     j,
		UserName: k.Name,
	}
})
want := []BookWithUser{
	{Book{1, "hello", 1}, "jd"},
	{Book{2, "world", 1}, "jd"},
	{Book{3, "good", 2}, "jc"},
	{Book{4, "job", 2}, "jc"},
}
// r is what we want.
r := HashJoin([]Book{
	{Id: 1, Title: "hello", Author: 1},
	{Id: 2, Title: "world", Author: 1},
	{Id: 3, Title: "good", Author: 2},
	{Id: 4, Title: "job", Author: 2},
}, []User{
	{Id: 1, Name: "jd"},
	{Id: 2, Name: "jc"},
}, func(item Book) uint64 {
	return item.Author
}, func(item User) uint64 {
	return item.Id
}, func(j Book, k User) BookWithUser {
	return BookWithUser{
		Book:     j,
		UserName: k.Name,
	}
})
want := []BookWithUser{
	{Book{1, "hello", 1}, "jd"},
	{Book{2, "world", 1}, "jd"},
	{Book{3, "good", 2}, "jc"},
	{Book{4, "job", 2}, "jc"},
}
// r is what we want.

Send HTTP request

Send a http request with a simple function.

Worker

A worker pool process job with a limited number Goroutine.

DB connect and find

// 0. open a db
var tdb *sql.DB

// 1. define a finder
type finderOfUser struct {
	id uint64
}

func (f *finderOfUser) Query() (query string, args []any) {
	query = `select * from user where id = ?`
	args = append(args, f.id)
	return
}

func (f *finderOfUser) NewScanObjAndFields(colTypes []*sql.ColumnType) (r *UserForDB, fields []any) {
	r = &UserForDB{}
	fields = append(fields,
		&r.Id,
		&r.Name,
	)
	return
}

// 2. find
finder := &finderOfUser{
	id: 1,
}
r, err := do.FindAll(tdb, finder, (UserForDB{}))
if err != nil {
	panic(err)
}

// 3. find per batch
finder := &finderOfUser{
	id: 1,
}
// batchNum is 10, if there are 20 records, it will be processed in two parts
err := do.Batch(tdb, finder, 10, func(r []UserForDB) error {
	// Process this batch of data

	return nil
}

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 (
	ErrNilDoer = errors.New("f is nil")
)

Functions

func As added in v0.8.0

func As[T any](v any) T

As assert the value v to type T

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

func BytesToString(b []byte) string

BytesToString converts byte slice to string without a memory allocation.

func CodeIs200 added in v0.2.0

func CodeIs200(code int) error

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

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

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, initial R) (r R, err error)

func ForgotKey added in v0.6.0

func ForgotKey(key string)

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

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 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 Keys added in v0.8.0

func Keys[K comparable, E any](collection map[K]E) []K

func MatchError

func MatchError(v any) bool

func MkdirAllIfNotExist added in v0.8.0

func MkdirAllIfNotExist(dir string) error

func Must

func Must(err error)

Must panic if err is not nill

func Must1

func Must1[T any](a1 T, err error) T

Must1 panic if err is not nill,or return 1 result

func Must2

func Must2[T1, T2 any](a1 T1, a2 T2, err error) (T1, T2)

Must2 panic if err is not nill,or return 2 result

func Must3

func Must3[T1, T2, T3 any](a1 T1, a2 T2, a3 T3, err error) (T1, T2, T3)

Must3 panic if err is not nill,or return 3 result

func Must4

func Must4[T1, T2, T3, T4 any](a1 T1, a2 T2, a3 T3, a4 T4, err error) (T1, T2, T3, T4)

Must4 panic if err is not nill,or return 4 result

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 NewError

func NewError[T any](inner T) error

func PrintFields added in v0.2.0

func PrintFields(fields []any)

func RetryWithDeadline added in v0.5.0

func RetryWithDeadline(ctx context.Context, d time.Time, f Doer) error

RetryWithDeadline retry f before d exceeds if f failed

func RetryWithTimes added in v0.5.0

func RetryWithTimes(ctx context.Context, tryTimes int, f Doer) error

RetryWithTimes retry f tryTimes times if f failed

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

func StringToBytes(s string) []byte

StringToBytes converts string to byte slice without a memory allocation.

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 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,
	initial 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 Doer added in v0.5.0

type Doer = func(context.Context) (canRetry bool, err error)

type Error

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

Error is a error type with any element

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)

type Field added in v0.9.0

type Field struct {
	reflect.StructField        // 内嵌反射结构体字段类型
	Comment             string // 注释
	Struct              Struct // 字段的类型是其它结构体
}

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

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

func NewJob added in v0.2.0

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

type Map added in v0.6.0

type Map[K comparable, T any] struct {
	// contains filtered or unexported fields
}

func NewMap added in v0.6.0

func NewMap[K comparable, T any](size ...int) *Map[K, T]

func (*Map[K, T]) Get added in v0.6.0

func (m *Map[K, T]) Get(key K) (value T)

func (*Map[K, T]) Insert added in v0.6.0

func (m *Map[K, T]) Insert(key K, value T)

func (*Map[K, T]) Lookup added in v0.6.0

func (m *Map[K, T]) Lookup(key K) (value T, ok bool)

func (*Map[K, T]) Range added in v0.6.0

func (m *Map[K, T]) Range(f func(key K, value T))

func (*Map[K, T]) Remove added in v0.6.0

func (m *Map[K, T]) Remove(key K)

type ResultExtractor added in v0.2.0

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

type SingleFlightCall added in v0.6.0

type SingleFlightCall[R any] func() (R, error)

type Slice added in v0.6.0

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

func NewSlice added in v0.6.0

func NewSlice[T any](lenAndCap ...int) *Slice[T]

func (*Slice[T]) Append added in v0.6.0

func (s *Slice[T]) Append(values ...T)

func (*Slice[T]) Index added in v0.6.0

func (s *Slice[T]) Index(i int) T

func (*Slice[T]) Range added in v0.6.0

func (s *Slice[T]) Range(f func(item T, index int))

func (*Slice[T]) Reset added in v0.6.0

func (s *Slice[T]) Reset(lenAndCap ...int)

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

func ResolveStruct(value any) (Struct, error)

func (Struct) GetFields added in v0.9.0

func (s Struct) GetFields() []Field

GetFields return all field in struct, include anonymous fields

type StructCommentEntity added in v0.9.0

type StructCommentEntity struct {
	StructName    string
	StructComment map[string]string
	FieldComment  map[string]string
}

type Worker added in v0.2.0

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

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

func (*Worker) Start added in v0.2.0

func (w *Worker) Start()

func (*Worker) Stop added in v0.2.0

func (w *Worker) 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