-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoptions.go
233 lines (191 loc) · 5.44 KB
/
options.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
package fx_opt //nolint
import (
"context"
"fmt"
"reflect"
"go.uber.org/fx"
)
type Invoke int
var invokeVal int
type StopFunc func(context.Context) error
func NextInvoke() Invoke {
preInvoke := invokeVal
invokeVal++
return Invoke(preInvoke)
}
func defaults() []Option {
return []Option{}
}
// New builds and starts new Filecoin node
func New(ctx context.Context, opts ...Option) (StopFunc, error) {
settings := Settings{
modules: map[interface{}]fx.Option{},
invokes: make([]fx.Option, invokeVal),
}
// apply module options in the right order
if err := Options(Options(defaults()...), Options(opts...))(&settings); err != nil {
return nil, fmt.Errorf("applying node options failed: %w", err)
}
// gather constructors for fx.Options
ctors := make([]fx.Option, 0, len(settings.modules))
for _, opt := range settings.modules {
ctors = append(ctors, opt)
}
// fill holes in invokes for use in fx.Options
for i, opt := range settings.invokes {
if opt == nil {
settings.invokes[i] = fx.Options()
}
}
app := fx.New(
fx.Logger(&Logger{}),
fx.Options(ctors...),
fx.Options(settings.invokes...),
)
// TODO: we probably should have a 'firewall' for Closing signal
// on this context, and implement closing logic through lifecycles
// correctly
if err := app.Start(ctx); err != nil {
return nil, fmt.Errorf("starting node: %w", err)
}
return app.Stop, nil
}
type Settings struct {
// modules is a map of constructors for DI
//
// In most cases the index will be a reflect. Type of element returned by
// the constructor, but for some 'constructors' it's hard to specify what's
// the return type should be (or the constructor returns fx group)
modules map[interface{}]fx.Option
// invokes are separate from modules as they can't be referenced by return
// type, and must be applied in correct order
invokes []fx.Option
}
// Option is a functional option which can be used with the New function to
// change how the node is constructed
//
// Options are applied in sequence
type Option func(*Settings) error
// Options groups multiple options into one
func Options(opts ...Option) Option {
return func(s *Settings) error {
for _, opt := range opts {
if err := opt(s); err != nil {
return err
}
}
return nil
}
}
// Error is a special option which returns an error when applied
func Error(err error) Option {
return func(_ *Settings) error {
return err
}
}
func ApplyIf(check func(s *Settings) bool, opts ...Option) Option {
return func(s *Settings) error {
if check(s) {
return Options(opts...)(s)
}
return nil
}
}
func If(b bool, opts ...Option) Option {
return ApplyIf(func(_ *Settings) bool {
return b
}, opts...)
}
// Override option changes constructor for a given type
func Override(typ, constructor interface{}) Option {
return func(s *Settings) error {
if i, ok := typ.(Invoke); ok {
s.invokes[i] = fx.Invoke(constructor)
return nil
}
ctor := as(constructor, typ)
rt := reflect.TypeOf(typ).Elem()
s.modules[rt] = fx.Provide(ctor)
return nil
}
}
func Annotate(typ, constructor interface{}) Option {
return func(s *Settings) error {
s.modules[typ] = fx.Provide(constructor)
return nil
}
}
func Unset(typ interface{}) Option {
return func(s *Settings) error {
if i, ok := typ.(Invoke); ok {
s.invokes[i] = nil
return nil
}
rt := reflect.TypeOf(typ).Elem()
delete(s.modules, rt)
return nil
}
}
// From (*T) -> func(t T) T {return t}
func From(typ interface{}) interface{} {
rt := []reflect.Type{reflect.TypeOf(typ).Elem()}
ft := reflect.FuncOf(rt, rt, false)
return reflect.MakeFunc(ft, func(args []reflect.Value) (results []reflect.Value) {
return args
}).Interface()
}
func FromVal[T any](v T) func() T {
return func() T {
return v
}
}
// from go-ipfs
// as casts input constructor to a given interface (if a value is given, it
// wraps it into a constructor).
//
// Note: this method may look like a hack, and in fact it is one.
// This is here only because https://github.com/uber-go/fx/issues/673 wasn't
// released yet
//
// Note 2: when making changes here, make sure this method stays at
// 100% coverage. This makes it less likely it will be terribly broken
func as(in interface{}, as interface{}) interface{} {
outType := reflect.TypeOf(as)
if outType.Kind() != reflect.Ptr {
panic("outType is not a pointer")
}
inType := reflect.TypeOf(in)
if inType.Kind() != reflect.Func || inType.AssignableTo(outType.Elem()) {
ctype := reflect.FuncOf(nil, []reflect.Type{outType.Elem()}, false)
return reflect.MakeFunc(ctype, func(_ []reflect.Value) (results []reflect.Value) {
out := reflect.New(outType.Elem())
out.Elem().Set(reflect.ValueOf(in))
return []reflect.Value{out.Elem()}
}).Interface()
}
ins := make([]reflect.Type, inType.NumIn())
outs := make([]reflect.Type, inType.NumOut())
for i := range ins {
ins[i] = inType.In(i)
}
outs[0] = outType.Elem()
for i := range outs[1:] {
outs[i+1] = inType.Out(i + 1)
}
ctype := reflect.FuncOf(ins, outs, false)
return reflect.MakeFunc(ctype, func(args []reflect.Value) (results []reflect.Value) {
outs := reflect.ValueOf(in).Call(args)
out := reflect.New(outType.Elem())
if outs[0].Type().AssignableTo(outType.Elem()) {
// Out: Iface = In: *Struct; Out: Iface = In: OtherIface
out.Elem().Set(outs[0])
} else {
// Out: Iface = &(In: Struct)
t := reflect.New(outs[0].Type())
t.Elem().Set(outs[0])
out.Elem().Set(t)
}
outs[0] = out.Elem()
return outs
}).Interface()
}