-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_branch_action.go
More file actions
58 lines (51 loc) · 1.98 KB
/
Copy pathsimple_branch_action.go
File metadata and controls
58 lines (51 loc) · 1.98 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package chain
import "context"
// BranchFunc represents the signature for the function that defines the branching logic
// for a BranchAction in the package. It takes the running context and output as input
// and returns the direction for the next step along with any error.
type BranchFunc[T any] func(ctx context.Context, output T) (direction string, err error)
// NewSimpleBranchAction creates a new BranchAction with customizable directions.
// It accepts a name, custom directions, and a BranchFunc that selects the next
// direction from the action's output.
//
// A custom runFunc can be provided to define execution logic. If runFunc is nil,
// the action passes the input through as output.
//
// This allows simple BranchActions to be created without manually defining a
// struct that implements BranchAction.
func NewSimpleBranchAction[T any](name string, runFunc RunFunc[T], directions []string, branchFunc BranchFunc[T]) BranchAction[T] {
if len(directions) == 0 {
panic("directions cannot be empty")
} else if branchFunc == nil {
panic("branchFunc cannot be nil")
}
if runFunc == nil {
runFunc = func(_ context.Context, input T) (T, error) { return input, nil }
}
copiedDirections := make([]string, len(directions))
copy(copiedDirections, directions)
return &simpleBranchAction[T]{
name: name,
runFunc: runFunc,
directions: copiedDirections,
branchFunc: branchFunc,
}
}
type simpleBranchAction[T any] struct {
name string
directions []string
runFunc RunFunc[T]
branchFunc BranchFunc[T]
}
func (s simpleBranchAction[T]) Name() string { return s.name }
func (s simpleBranchAction[T]) Directions() []string {
directions := make([]string, len(s.directions))
copy(directions, s.directions)
return directions
}
func (s simpleBranchAction[T]) Run(ctx context.Context, input T) (output T, err error) {
return s.runFunc(ctx, input)
}
func (s simpleBranchAction[T]) NextDirection(ctx context.Context, output T) (string, error) {
return s.branchFunc(ctx, output)
}