forked from ctiom/kitchen
-
Notifications
You must be signed in to change notification settings - Fork 10
/
types.go
236 lines (231 loc) · 6.69 KB
/
types.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package kitchen
import (
"context"
"database/sql"
"github.com/go-preform/kitchen/delivery"
"net/http"
"net/url"
"reflect"
)
type (
DishCooker[D ICookware, I any, O any] func(IContext[D], I) (output O, err error)
PipelineDishCooker[D IPipelineCookware[M], M IPipelineModel, I any, O any] func(IPipelineContext[D, M], M, I) (output O, toSaveCanNil M, err error)
AfterListenHandlers[D ICookware, I any, O any] func(ctx IContext[D], input I, output O, err error)
iMarshaller[I any] interface {
unmarshal(raw []byte) (output I, recycle func(any), err error)
marshal(output I) (outputData []byte, err error)
}
// IWebBundle is generated by web router wrapper, it contains all the information of a web request.
IWebBundle interface {
Ctx() context.Context
Method() string
Body() ([]byte, error)
Url() *url.URL
UrlParams() map[string]string
Headers() http.Header
Raw() any
Response() http.ResponseWriter
}
ICookware interface {
}
// ICookwareInheritable is a cookware that can be inherited by another cookware.
// Useful when cross menu cooking is needed and pass the cookware around.
ICookwareInheritable interface {
ICookware
Inherit(ICookware) ICookware
}
ICookwareFactory[D ICookware] interface {
New() D
Put(any)
}
// ITraceableCookware is a cookware that trace the life-cycle of cooking
// can refer to built in tracers.
ITraceableCookware[D ICookware] interface {
StartTrace(ctx IContext[D], id string, input any) (context.Context, iTraceSpan[D])
}
ITraceSpan interface {
Detail() any
End(output any, err error)
AddEvent(name string, attrSets ...map[string]any)
SetAttributes(key string, value any)
Raw() any
}
iTraceSpan[D ICookware] interface {
ITraceSpan
logSideEffect(ctx IContext[D], instanceName string, toLog []any) (context.Context, iTraceSpan[D])
}
IInstance interface {
Name() string
Menu() IMenu
Nodes() []IInstance
}
iCookbook[D ICookware] interface {
IInstance
ifLock() func()
inherit(...iCookbook[D])
emitAfterCook(IContext[D], any, any, error)
isTraceableDep() bool
isInheritableDep() bool
menu() iMenu[D]
}
// IManager is the used for managing menus for scaling.
IManager interface {
AddMenu(menuInitializer func() IMenu) IManager
SetMainKitchen(url string, port uint16) IManager
order(dish IDish) (func(ctx context.Context, input []byte) (output []byte, err error), error)
Init(ctx context.Context) (IManager, error)
SelectServeMenus(menuNames ...string) IManager
DisableMenu(name string) IManager
}
IMenu interface {
IInstance
Name() string
Manager() IManager
Cookware() ICookware
setManager(m IManager, id uint32)
ID() uint32
orderDish(context.Context, *delivery.Order)
cookwareRecycle(any)
}
iMenu[D ICookware] interface {
IMenu
iCookbook[D]
init(iMenu[D], any)
initWithoutFields(iMenu[D], any)
setName(name string)
setCookware(any)
cookware() D
Dependency() D
pushDish(iDish[D]) int
Dishes() []iDish[D]
}
ISet interface {
IInstance
Name() string
Menu() IMenu
Tree() []ISet
}
iSet[D ICookware] interface {
ISet
iCookbook[D]
init(menu iMenu[D], group iSet[D], parent iSet[D], name string)
tree() []iSet[D]
menu() iMenu[D]
}
IDish interface {
Name() string
FullName() string
Input() any
IO() (any, any)
Id() uint32
Menu() IMenu
Cookware() ICookware
Sets() []ISet
Tags() reflect.StructTag
cookByte(ctx context.Context, inputData []byte) (outputData []byte, err error)
// CookAny is for cooking with any input, mainly for external node calling with network.
CookAny(ctx context.Context, input any) (output any, err error)
}
iDish[D ICookware] interface {
IDish
iCookbook[D]
init(parent iCookbook[D], action iDish[D], name string, tags reflect.StructTag)
refreshCooker()
prefork(ch chan asyncTask[D, any, any], hasPreHeatCookware bool) (onCook func(cookware D, ctx IContext[D], input any, callback func(any, error)), restore func())
}
IContextWithSession interface {
context.Context
Session(...IDishServe) []IDishServe
SetCtx(context.Context)
RawCookware() ICookware
WebBundle() IWebBundle
GetCtx() context.Context
servedWeb()
served()
}
IDishServe interface {
finish(output any, err error)
Record() (action IDish, finish bool, input any, output any, err error)
}
IContext[D ICookware] interface {
IContextWithSession
traceableCookware() ITraceableCookware[D]
startTrace(id string, input any) iTraceSpan[D]
Menu() iMenu[D]
Sets() []iSet[D]
Dish() iDish[D]
Dependency() D
Cookware() D
logSideEffect(instanceName string, toLog []any) (IContext[D], iTraceSpan[D])
TraceSpan() iTraceSpan[D]
setCookware(D)
}
IPipelineContext[D IPipelineCookware[M], M IPipelineModel] interface {
IContext[D]
Tx() IDbTx
Pipeline() iPipeline[D, M]
Stage() iPipelineStage[D, M]
}
PipelineStatus string
IPipelineModel interface {
GetStatus() PipelineStatus
SetStatus(status PipelineStatus)
PrimaryKey() any
}
IPipelineModelCanMap interface {
ToMap() map[string]string
}
IDbRunner interface {
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
IDbTx interface {
IDbRunner
Rollback() error
Commit() error
}
IPipelineCookware[M IPipelineModel] interface {
ICookware
BeginTx(context.Context, ...*sql.TxOptions) (IDbTx, error)
FinishTx(IDbTx, error) error
GetModelById(ctx context.Context, pks ...any) (M, error)
SaveModel(db IDbRunner, model M, oStatus PipelineStatus) error
}
IPipeline interface {
IMenu
GetActionsForModel(any) (status string, actions []IPipelineAction)
GetActionsForStatus(status string) []IPipelineAction
NewModel() IPipelineModel
}
iPipeline[D IPipelineCookware[M], M IPipelineModel] interface {
IPipeline
iMenu[D]
initPipeline(pipeline iPipeline[D, M], bundle D)
}
IPipelineStage interface {
ISet
Status() PipelineStatus
Actions() []IPipelineAction
}
iPipelineStage[D IPipelineCookware[M], M IPipelineModel] interface {
IPipelineStage
iSet[D]
initStage(parent iCookbook[D], stage iPipelineStage[D, M], stageName PipelineStatus)
pipeline() iPipeline[D, M]
}
IPipelineAction interface {
IDish
Status() PipelineStatus
ModelToMap(model IPipelineModel) map[string]string
ExecByIdAny(ctx context.Context, input any, ids ...any) (output any, err error)
WillCreateModel() bool
Pipeline() IPipeline
}
iPipelineAction[D IPipelineCookware[M], M IPipelineModel] interface {
IPipelineAction
iDish[D]
initAction(parent iCookbook[D], act iPipelineAction[D, M], name string, tags reflect.StructTag)
}
)