-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchain.go
More file actions
163 lines (142 loc) · 4.22 KB
/
chain.go
File metadata and controls
163 lines (142 loc) · 4.22 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package cosy
// ProcessChain represents a typed, ordered pipeline composed of optional stages.
//
// Life cycle (fixed order, any stage may be nil and will be skipped):
// Create/Update/Custom: [Prepare] -> [Validate] -> [BeforeDecode] -> [Decode] -> [BeforeExecute] -> [GormAction] -> [Executed] -> [Response]
// Get/List: [Prepare] -> [BeforeExecute] -> [GormAction] -> [Response]
// Delete/Recover: [Prepare] -> [BeforeExecute] -> [GormAction] -> [Response]
//
// Notes:
// - Each Set* method registers the corresponding stage function.
// - Stages are executed with the same core context (Ctx[T]).
// - If ctx.abort becomes true during execution, remaining stages are not executed.
type ProcessChain[T any] struct {
core *Ctx[T]
prepare func(ctx *Ctx[T])
validate func(ctx *Ctx[T])
beforeDecode func(ctx *Ctx[T])
decode func(ctx *Ctx[T])
beforeExecute func(ctx *Ctx[T])
gormAction func(ctx *Ctx[T])
executed func(ctx *Ctx[T])
response func(ctx *Ctx[T])
}
// NewProcessChain creates a new ProcessChain bound to the provided core context.
// The returned value is the first step of a staged builder that enforces
// the registration order at compile time. You can ignore intermediate
// returns and later call Run() on the underlying *ProcessChain if needed.
func NewProcessChain[T any](core *Ctx[T]) *ProcessChain[T] {
return &ProcessChain[T]{
core: core,
}
}
// SetPrepare registers the Prepare stage and returns the next builder interface.
func (c *ProcessChain[T]) SetPrepare(fn func(ctx *Ctx[T])) *ProcessChain[T] {
c.prepare = fn
return c
}
// SetValidate registers the Validate stage and returns the next builder interface.
func (c *ProcessChain[T]) SetValidate(fn func(ctx *Ctx[T])) *ProcessChain[T] {
c.validate = fn
return c
}
// SetBeforeDecode registers the BeforeDecode stage and returns the next builder interface.
func (c *ProcessChain[T]) SetBeforeDecode(fn func(ctx *Ctx[T])) *ProcessChain[T] {
c.beforeDecode = fn
return c
}
// SetDecode registers the Decode stage and returns the next builder interface.
func (c *ProcessChain[T]) SetDecode(fn func(ctx *Ctx[T])) *ProcessChain[T] {
c.decode = fn
return c
}
// SetBeforeExecute registers the BeforeExecute stage and returns the next builder interface.
func (c *ProcessChain[T]) SetBeforeExecute(fn func(ctx *Ctx[T])) *ProcessChain[T] {
c.beforeExecute = fn
return c
}
// SetGormAction registers the GormAction stage and returns the next builder interface.
func (c *ProcessChain[T]) SetGormAction(fn func(ctx *Ctx[T])) *ProcessChain[T] {
c.gormAction = fn
return c
}
// SetExecuted registers the Executed stage and returns the next builder interface.
func (c *ProcessChain[T]) SetExecuted(fn func(ctx *Ctx[T])) *ProcessChain[T] {
c.executed = fn
return c
}
// SetResponse registers the Response stage. This completes the staged builder.
func (c *ProcessChain[T]) SetResponse(fn func(ctx *Ctx[T])) *ProcessChain[T] {
c.response = fn
return c
}
// CreateOrModify executes the process chain for create or modify actions.
func (c *ProcessChain[T]) CreateOrModify() {
chain := []func(ctx *Ctx[T]){
c.prepare,
c.validate,
c.beforeDecode,
c.decode,
c.beforeExecute,
c.gormAction,
c.executed,
}
for _, fn := range chain {
if fn == nil {
continue
}
fn(c.core)
if c.core.abort {
break
}
}
if c.core.useTransaction {
c.core.Tx.Commit()
}
if c.core.abort == false && c.response != nil {
c.response(c.core)
}
}
// GetOrGetList executes the process chain for get or get list actions.
func (c *ProcessChain[T]) GetOrGetList() {
chain := []func(ctx *Ctx[T]){
c.prepare,
c.beforeExecute,
c.gormAction,
}
for _, fn := range chain {
if fn == nil {
continue
}
fn(c.core)
if c.core.abort {
break
}
}
if c.core.abort == false && c.response != nil {
c.response(c.core)
}
}
// DeleteOrRecover executes the process chain for delete or recover actions.
func (c *ProcessChain[T]) DestroyOrRecover() {
chain := []func(ctx *Ctx[T]){
c.prepare,
c.beforeExecute,
c.gormAction,
}
for _, fn := range chain {
if fn == nil {
continue
}
fn(c.core)
if c.core.abort {
break
}
}
if c.core.useTransaction {
c.core.Tx.Commit()
}
if c.core.abort == false && c.response != nil {
c.response(c.core)
}
}