forked from ctiom/kitchen
-
Notifications
You must be signed in to change notification settings - Fork 10
/
menu.go
205 lines (179 loc) · 4.91 KB
/
menu.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
// Package kitchen A golang framework for building progressive backend services.
package kitchen
import (
"context"
"errors"
"github.com/go-preform/kitchen/delivery"
"reflect"
"sync"
)
// MenuBase is a struct for supporting IMenu, should embed to all menus.
type MenuBase[WPtr iMenu[D], D ICookware] struct {
cookbook[D, any, any]
name string
menuCookware D
cookwareFactory ICookwareFactory[D]
dishes []iDish[D]
dishCnt uint32
manager IManager
idUnderManager uint32
recycleCookware func(any)
}
// InitMenu initializes a menu.
func InitMenu[W iMenu[D], D ICookware](menuPtr W, bundle any) W {
menuPtr.init(menuPtr, bundle)
return menuPtr
}
func (b *MenuBase[W, D]) init(w iMenu[D], bundle any) {
b.cookbook.init()
b.initWithoutFields(w, bundle)
b.nodes = iterateStruct(w, w, nil, bundle)
}
func iterateStruct[D ICookware](s any, parentMenu iMenu[D], set iSet[D], bundle any) []iCookbook[D] {
var (
fieldType reflect.StructField
sValue = reflect.ValueOf(s).Elem()
sType = sValue.Type()
nodes []iCookbook[D]
path string
ok bool
)
for i, l := 0, sType.NumField(); i < l; i++ {
fieldType = sType.Field(i)
if fieldType.IsExported() && !fieldType.Anonymous {
path = fieldType.Name
node := sValue.Field(i).Addr().Interface()
if _, ok = node.(IDish); ok {
if set != nil {
initDish(set.(iCookbook[D]), node.(iDish[D]), path, fieldType.Tag)
} else {
initDish(parentMenu.(iCookbook[D]), node.(iDish[D]), path, fieldType.Tag)
}
nodes = append(nodes, node.(iCookbook[D]))
} else if _, ok = node.(ISet); ok {
initSet(parentMenu, node.(iSet[D]), set, path)
nodes = append(nodes, node.(iCookbook[D]))
} else if _, ok = node.(IMenu); ok {
InitMenu(node.(iMenu[D]), bundle)
nodes = append(nodes, node.(iCookbook[D]))
} else if fieldType.Type.Kind() == reflect.Struct {
nodes = append(nodes, iterateStruct[D](sValue.Field(i).Addr().Interface(), parentMenu, set, bundle)...)
}
}
}
return nodes
}
func (r MenuBase[W, D]) Cookware() ICookware {
if r.cookwareFactory != nil {
return r.cookwareFactory.New()
}
return r.menuCookware
}
// Manager returns the manager of the menu, maybe nil if not associated with a manager.
func (b *MenuBase[W, D]) Manager() IManager {
return b.manager
}
func (b *MenuBase[W, D]) ID() uint32 {
return b.idUnderManager
}
func (b *MenuBase[W, D]) setManager(m IManager, id uint32) {
b.idUnderManager = id
b.manager = m.(*Manager)
for _, d := range b.dishes {
d.refreshCooker()
}
}
func (b *MenuBase[W, D]) initWithoutFields(w iMenu[D], bundle any) {
var (
menuValue = reflect.ValueOf(w).Elem()
menuType = menuValue.Type()
)
w.setCookware(bundle)
_, b.isTraceable = any(b.menuCookware).(ITraceableCookware[D])
b.instance = w
b.name = menuType.Name()
_, b.isInheritableCookware = any(bundle).(ICookwareInheritable)
b.concurrentLimit = new(int32)
b.running = new(int32)
b.spinLocker = &sync.Mutex{}
b.runningLock = &sync.Mutex{}
w.setName(menuType.Name())
}
func (b *MenuBase[W, D]) pushDish(action iDish[D]) int {
b.dishes = append(b.dishes, action)
b.dishCnt++
return len(b.dishes) - 1
}
func (b *MenuBase[W, D]) Dishes() []iDish[D] {
return b.dishes
}
func (b *MenuBase[W, D]) setName(name string) {
b.name = name
}
func (b *MenuBase[W, D]) Menu() IMenu {
return b.instance.(IMenu)
}
func (b *MenuBase[W, D]) menu() iMenu[D] {
return b.instance.(iMenu[D])
}
func (b MenuBase[W, D]) Name() string {
return b.name
}
type poolLike interface {
New() any
Put(any)
}
// wrap the output of poolLike to be a ICookwareFactory
type poolLikeWrapper[D ICookware] struct {
poolLike
}
func (p poolLikeWrapper[D]) New() D {
return p.poolLike.New().(D)
}
func (p *poolLikeWrapper[D]) Put(d any) {
p.poolLike.Put(d)
}
func (b *MenuBase[W, D]) setCookware(cookware any) {
b.recycleCookware = func(any) {}
switch cookware.(type) {
case ICookwareFactory[D]:
f := cookware.(ICookwareFactory[D])
b.cookwareFactory = f
cookware = f.New()
b.recycleCookware = f.Put
case poolLike:
n := cookware.(poolLike)
b.cookwareFactory = &poolLikeWrapper[D]{poolLike: n}
b.menuCookware = n.New().(D)
b.recycleCookware = n.Put
case D:
default:
panic("cookware type not supported")
}
b.menuCookware = cookware.(D)
}
func (b *MenuBase[W, D]) cookwareRecycle(cookware any) {
b.recycleCookware(cookware)
}
func (b MenuBase[W, D]) Dependency() D {
if b.cookwareFactory != nil {
return b.cookwareFactory.New()
}
return b.menuCookware
}
func (b MenuBase[W, D]) cookware() D {
if b.cookwareFactory != nil {
return b.cookwareFactory.New()
}
return b.menuCookware
}
var errDishNotFound = errors.New("dish not found")
func (b MenuBase[W, D]) orderDish(ctx context.Context, order *delivery.Order) {
if order.DishId >= b.dishCnt {
_ = order.Response(nil, errDishNotFound)
return
}
output, err := b.dishes[order.DishId].cookByte(ctx, order.Input)
_ = order.Response(output, err)
return
}