-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
95 lines (78 loc) · 2.21 KB
/
module.go
File metadata and controls
95 lines (78 loc) · 2.21 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
package queue
import (
"fmt"
"github.com/tinh-tinh/tinhtinh/v2/common"
"github.com/tinh-tinh/tinhtinh/v2/core"
)
const QUEUE core.Provide = "QUEUE"
func ForRoot(opt *Options) core.Modules {
return func(module core.Module) core.Module {
queueModule := module.New(core.NewModuleOptions{})
queueModule.NewProvider(core.ProviderOptions{
Name: QUEUE,
Value: opt,
})
queueModule.Export(QUEUE)
return queueModule
}
}
func ForRootFactory(factory func(ref core.RefProvider) *Options) core.Modules {
return func(module core.Module) core.Module {
opt := factory(module)
queueModule := module.New(core.NewModuleOptions{})
queueModule.NewProvider(core.ProviderOptions{
Name: QUEUE,
Value: opt,
})
queueModule.Export(QUEUE)
return queueModule
}
}
// getQueueName generates a unique name for a queue provider.
//
// The name is in the form "<name>Queue".
func getQueueName(name string) core.Provide {
return core.Provide(fmt.Sprintf("%s_QUEUE", name))
}
// Register registers a new queue module with the given name and options. The
// registered module creates a new queue with the given name and options, and
// exports the queue under the name "<name>Queue".
func Register(name string, opts ...*Options) core.Modules {
var option *Options
if len(opts) > 0 {
option = opts[0]
}
return func(module core.Module) core.Module {
defaultOptions, ok := module.Ref(QUEUE).(*Options)
if option == nil {
if ok {
option = defaultOptions
}
} else {
if ok {
mergeOpt := common.MergeStruct(*option, *defaultOptions)
option = &mergeOpt
}
}
if option == nil {
panic("not config option for queue")
}
queueModule := module.New(core.NewModuleOptions{})
queueModule.NewProvider(core.ProviderOptions{
Name: getQueueName(name),
Value: New(name, option),
})
queueModule.Export(getQueueName(name))
return queueModule
}
}
// InjectQueue injects a queue from the given module, using the given name. If the
// module does not contain a queue with the given name, or if the queue is not of
// type *Queue, InjectQueue returns nil.
func Inject(module core.RefProvider, name string) *Queue {
queue, ok := module.Ref(getQueueName(name)).(*Queue)
if !ok {
return nil
}
return queue
}