-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
69 lines (56 loc) · 1.22 KB
/
factory.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
package scene
// LensInit is a function initialize a lens
// if error happens, it should panic
type LensInit func()
type InitArray []LensInit
func (inits InitArray) Inits() {
for _, init := range inits {
init()
}
}
type AppInit[T Application] func() T
type IModuleDependencyProvider[T any] interface {
Provide() T
}
type IModuleFactory interface {
Init() LensInit
Apps() []any
}
type IDefaultableModuleFactory[T any] interface {
IModuleFactory
Default() T
}
type ModuleFactory struct {
}
func (b ModuleFactory) Init() LensInit {
return nil
}
func (b ModuleFactory) Apps() []any {
return nil
}
type ModuleFactoryArray []IModuleFactory
func BuildInitArray(builders ModuleFactoryArray) InitArray {
var inits InitArray
for _, builder := range builders {
init := builder.Init()
if init != nil {
inits = append(inits, init)
}
}
return inits
}
func BuildApps[T Application](builders ModuleFactoryArray) []T {
var apps []T
for _, builder := range builders {
for _, app := range builder.Apps() {
// should be AppInit[T], but golang compiler complains about it
// So use func() T instead
if init, ok := app.(func() T); ok {
if init != nil {
apps = append(apps, init())
}
}
}
}
return apps
}