action

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: MPL-2.0 Imports: 4 Imported by: 0

README

WIP

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoNothing

func DoNothing[T any](_ context.Context, _ T) error

Types

type Action

type Action[T any] func(context.Context, T) error

Action is a specialized task.Task which accepts one param.

func Do

func Do[T any](f func(context.Context, T) error) Action[T]

Do wraps f into Action, mostly for for typing purpose.

func DoMicro

func DoMicro[T any](f func(T)) Action[T]

DoMicro creates Action from non-cancellable, never-fail function.

func DoTiny

func DoTiny[T any](f func(T) error) Action[T]

DoTiny creates Action from non-cancellable function.

func (Action[T]) Apply

func (a Action[T]) Apply(v T) task.Task

Apply creates a task.Task that executes the action with a value.

func (Action[T]) Micro

func (a Action[T]) Micro() func(T)

func (Action[T]) Then

func (act Action[T]) Then(next Action[T]) Action[T]

Then creates an Action which runs next after act is finished successfully.

func (Action[T]) Tiny

func (a Action[T]) Tiny() func(T) error

func (Action[T]) Use

func (a Action[T]) Use(d Data[T]) task.Task

Use creates a task.Task that executes the action with Data. The value of data is generated by d on-the-fly.

func (Action[T]) With

func (a Action[T]) With(mod task.CtxMod) Action[T]

type Action2 added in v0.0.3

type Action2[A, B any] func(context.Context, A, B) error

Action2 is an action that accepts two parameters.

func A2

func A2[A, B any](f func(context.Context, A, B) error) Action2[A, B]

func (Action2[A, B]) Apply added in v0.0.3

func (act Action2[A, B]) Apply(va A) Action[B]

Apply creates an Action by currifying an Action2 with a raw value.

func (Action2[A, B]) Then added in v0.0.3

func (act Action2[A, B]) Then(next Action2[A, B]) Action2[A, B]

Then creates an Action2 by running next after act is finished successfully.

func (Action2[A, B]) Use added in v0.0.3

func (act Action2[A, B]) Use(a Data[A]) Action[B]

Use creates an Action by currifying it with Data.

type Action3 added in v0.0.3

type Action3[A, B, C any] func(context.Context, A, B, C) error

Action3 is like Action2, but accepts one more param.

func A3

func A3[A, B, C any](f func(context.Context, A, B, C) error) Action3[A, B, C]

func (Action3[A, B, C]) Apply added in v0.0.3

func (act Action3[A, B, C]) Apply(va A) Action2[B, C]

func (Action3[A, B, C]) Then added in v0.0.3

func (act Action3[A, B, C]) Then(next Action3[A, B, C]) Action3[A, B, C]

func (Action3[A, B, C]) Use added in v0.0.3

func (act Action3[A, B, C]) Use(a Data[A]) Action2[B, C]

type Action4 added in v0.0.3

type Action4[A, B, C, D any] func(context.Context, A, B, C, D) error

Action4 is like Action3, but accepts one more param.

func A4

func A4[A, B, C, D any](f func(context.Context, A, B, C, D) error) Action4[A, B, C, D]

func (Action4[A, B, C, D]) Apply added in v0.0.3

func (act Action4[A, B, C, D]) Apply(va A) Action3[B, C, D]

func (Action4[A, B, C, D]) Then added in v0.0.3

func (act Action4[A, B, C, D]) Then(next Action4[A, B, C, D]) Action4[A, B, C, D]

func (Action4[A, B, C, D]) Use added in v0.0.3

func (act Action4[A, B, C, D]) Use(a Data[A]) Action3[B, C, D]

type Converter

type Converter[I, O any] func(context.Context, I) (O, error)

func C

func C[I, O any](f func(context.Context, I) (O, error)) Converter[I, O]

func CMicro

func CMicro[I, O any](f func(I) O) Converter[I, O]

func CTiny

func CTiny[I, O any](f func(I) (O, error)) Converter[I, O]

func (Converter[I, O]) Apply

func (c Converter[I, O]) Apply(i I) Data[O]

func (Converter[I, O]) Use

func (c Converter[I, O]) Use(i Data[I]) Data[O]

func (Converter[I, O]) With

func (c Converter[I, O]) With(mod task.CtxMod) Converter[I, O]

type Converter2 added in v0.0.3

type Converter2[A, B, O any] func(context.Context, A, B) (O, error)

func C2

func C2[A, B, O any](f func(context.Context, A, B) (O, error)) Converter2[A, B, O]

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

type Converter3[A, B, C, O any] func(context.Context, A, B, C) (O, error)

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

type Converter4[A, B, C, D, O any] func(context.Context, A, B, C, D) (O, error)

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

type Data[T any] func(context.Context) (T, error)

Data is a function which can generate some data.

func Use

func Use[T any](f func(context.Context) (T, error)) Data[T]

func UseError

func UseError[T any](err error) Data[T]

func UseMicro

func UseMicro[T any](f func() T) Data[T]

func UseTiny

func UseTiny[T any](f func() (T, error)) Data[T]

func UseValue

func UseValue[T any](v T) Data[T]

func (Data[T]) Cached

func (d Data[T]) Cached() Data[T]

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

func (d Data[T]) Default(v T) Data[T]

func (Data[T]) DefaultIf

func (d Data[T]) DefaultIf(errf func(error) bool, v T) Data[T]

DefaultIf wraps d to return v instead if any error matched by errf occurred.

func (Data[T]) DefaultIfNot

func (d Data[T]) DefaultIfNot(errf func(error) bool, v T) Data[T]

DefaultIfNot uses v if error occurred and is NOT matched by errf.

func (Data[T]) Defer

func (d Data[T]) Defer(f func(context.Context, T, error) (T, error)) Data[T]

Defer wraps d to run f after it.

func (Data[T]) Pre

func (d Data[T]) Pre(f task.Task) Data[T]

Pre wraps d to run f before it. d is skipped if f retruns error.

func (Data[T]) Retry

func (d Data[T]) Retry() Data[T]

func (Data[T]) RetryN

func (d Data[T]) RetryN(n int) Data[T]

func (Data[T]) Saved

func (d Data[T]) Saved() Data[T]

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>

func (Data[T]) Then

func (d Data[T]) Then(c Converter[T, T]) Data[T]

func (Data[T]) Tiny

func (d Data[T]) Tiny() func() (T, error)

func (Data[T]) To

func (d Data[T]) To(a Action[T]) task.Task

func (Data[T]) With

func (d Data[T]) With(mod task.CtxMod) Data[T]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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