Documentation
¶
Overview ¶
Package action is designed to write code like this:
func fetchAPI(context.Context) (*http.Response, error)
func parseResult(context.Context, *http.Response) (MyDataType, error) {}
func saveToDB(context.Context, sql.DB, MyDataType) error {}
func generateReport(context.Context, MyDataType) error {}
someData := action.C(parseResult).
From(fetchAPI).
RetryN(3).
Cached()
err := action.A2(saveToDB).
Bind(dbConn).
Then(generateReport).
Use(someData).
Run(ctx)
Index ¶
- func DoNothing[T any](_ context.Context, _ T) error
- type Action
- type Action2
- type Action3
- type Action4
- type Converter
- type Converter2
- type Converter3
- type Converter4
- type Data
- func (d Data[T]) Cached() Data[T]
- func (d Data[T]) Default(v T) Data[T]
- func (d Data[T]) DefaultIf(errf func(error) bool, v T) Data[T]
- func (d Data[T]) DefaultIfNot(errf func(error) bool, v T) Data[T]
- func (d Data[T]) Defer(f func(context.Context, T, error) (T, error)) Data[T]
- func (d Data[T]) Pre(f task.Task) Data[T]
- func (d Data[T]) Retry() Data[T]
- func (d Data[T]) RetryN(n int) Data[T]
- func (d Data[T]) Saved() Data[T]
- func (d Data[T]) Then(c Converter[T, T]) Data[T]
- func (d Data[T]) Tiny() func() (T, error)
- func (d Data[T]) To(a Action[T]) task.Task
- func (d Data[T]) With(mod task.CtxMod) Data[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Action ¶
Action is a specialized task.Task which accepts one param.
type Action2 ¶ added in v0.0.3
Action2 is an action that accepts two parameters.
func (Action2[A, B]) Apply ¶ added in v0.0.3
Apply creates an Action by currifying an Action2 with a raw value.
type Action3 ¶ added in v0.0.3
Action3 is like Action2, but accepts one more param.
type Action4 ¶ added in v0.0.3
Action4 is like Action3, but accepts one more param.
type Converter2 ¶ added in v0.0.3
func (Converter2[A, B, O]) Apply ¶ added in v0.0.3
func (c Converter2[A, B, O]) Apply(va A) Converter[B, O]
func (Converter2[A, B, O]) Use ¶ added in v0.0.3
func (c Converter2[A, B, O]) Use(a Data[A]) Converter[B, O]
type Converter3 ¶ added in v0.0.3
func C3 ¶
func C3[A, B, C, O any](f func(context.Context, A, B, C) (O, error)) Converter3[A, B, C, O]
func (Converter3[A, B, C, O]) Apply ¶ added in v0.0.3
func (c Converter3[A, B, C, O]) Apply(va A) Converter2[B, C, O]
func (Converter3[A, B, C, O]) Use ¶ added in v0.0.3
func (c Converter3[A, B, C, O]) Use(a Data[A]) Converter2[B, C, O]
type Converter4 ¶ added in v0.0.3
func C4 ¶
func C4[A, B, C, D, O any](f func(context.Context, A, B, C, D) (O, error)) Converter4[A, B, C, D, O]
func (Converter4[A, B, C, D, O]) Apply ¶ added in v0.0.3
func (c Converter4[A, B, C, D, O]) Apply(va A) Converter3[B, C, D, O]
func (Converter4[A, B, C, D, O]) Use ¶ added in v0.0.3
func (c Converter4[A, B, C, D, O]) Use(a Data[A]) Converter3[B, C, D, O]
type Data ¶
Data is a function which can generate some data.
func (Data[T]) Cached ¶
Cached wraps d to cache its result, no matter success or failed.
Example ¶
a := UseTiny(func() (int32, error) {
fmt.Println("executed")
ret := rand.Int31n(100)
if ret < 50 {
return 0, errors.New("50/50")
}
return ret, nil
}).Cached()
v1, e1 := a(context.TODO()) // print "executed"
v2, e2 := a(context.TODO()) // nothing printed
if v1 != v2 {
fmt.Println("v1 != v2")
}
if e1 != e2 {
fmt.Println("e1 != e2")
}
Output: executed
func (Data[T]) DefaultIf ¶
DefaultIf wraps d to return v instead if any error matched by errf occurred.
func (Data[T]) DefaultIfNot ¶
DefaultIfNot uses v if error occurred and is NOT matched by errf.
func (Data[T]) Saved ¶
Saved wraps d to cache its result only when success.
Example ¶
err := errors.New("error")
cnt := 0
a := UseTiny(func() (int, error) {
fmt.Println("executed")
cnt++
if cnt < 2 {
return cnt, err
}
return cnt, nil
}).Saved()
fmt.Println(a(context.TODO()))
fmt.Println(a(context.TODO()))
fmt.Println(a(context.TODO()))
Output: executed 1 error executed 2 <nil> 2 <nil>
Source Files
¶
Click to show internal directories.
Click to hide internal directories.