-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_action.go
32 lines (27 loc) · 1.12 KB
/
simple_action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package chain
import "context"
// RunFunc defines the signature of a function used to implement an Action's execution logic.
// It is a function that takes an input of type T (a generic type) and returns an output of type T
// along with any error encountered during execution.
type RunFunc[T any] func(ctx context.Context, input T) (output T, err error)
// NewSimpleAction creates a new Action with a custom Run function,
// which can be a pure function or closure.
// The provided runFunc must match the RunFunc signature, where T is a generic type representing
// the input and output types for the Action's execution.
//
// This allows for the creation of simple Actions without manually defining a separate struct
// that implements the Action interface.
func NewSimpleAction[T any](name string, runFunc RunFunc[T]) Action[T] {
return &simpleAction[T]{
name: name,
runFunc: runFunc,
}
}
type simpleAction[T any] struct {
name string
runFunc RunFunc[T]
}
func (s simpleAction[T]) Name() string { return s.name }
func (s simpleAction[T]) Run(ctx context.Context, input T) (output T, err error) {
return s.runFunc(ctx, input)
}