-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
158 lines (131 loc) · 3.15 KB
/
app.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
package trier
import (
"context"
"errors"
"fmt"
"os"
"reflect"
"github.com/shupkg/trier/log"
"go.uber.org/dig"
)
type (
Out = dig.Out
In = dig.In
)
var (
app = dig.New()
)
var Exit = func() {
os.Exit(1)
}
var HandleMustError = func(err error) {
if err != nil {
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
log.Errorf("%v", err)
Exit()
}
}
}
func Provide(constructors ...interface{}) error {
for _, constructor := range constructors {
if err := app.Provide(constructor); err != nil {
return dig.RootCause(err)
}
}
return nil
}
func ProvideMust(constructors ...interface{}) {
HandleMustError(Provide(constructors...))
}
func Context(ctx context.Context) {
_ = Provide(func() context.Context { return ctx })
}
func Supply(values ...interface{}) error {
for _, v := range values {
provide, err := newSupplyConstructor(v)
if err != nil {
return err
}
if err := app.Provide(provide); err != nil {
return dig.RootCause(err)
}
}
return nil
}
func SupplyMust(values ...interface{}) {
HandleMustError(Supply(values...))
}
func Invoke(functions ...interface{}) error {
for _, function := range functions {
if err := app.Invoke(function); err != nil {
return dig.RootCause(err)
}
}
return nil
}
func InvokeMust(functions ...interface{}) {
HandleMustError(Invoke(functions...))
}
func Populate(targets ...interface{}) error {
function, err := newPopulateFunc(targets...)
if err != nil {
return err
}
if err := app.Invoke(function); err != nil {
return dig.RootCause(err)
}
return nil
}
func PopulateMust(targets ...interface{}) {
HandleMustError(Populate(targets...))
}
func newSupplyConstructor(value interface{}) (interface{}, error) {
switch value.(type) {
case nil:
return nil, errors.New("untyped nil passed to fx.Supply")
case error:
return nil, errors.New("error value passed to fx.Supply")
}
returnTypes := []reflect.Type{reflect.TypeOf(value)}
returnValues := []reflect.Value{reflect.ValueOf(value)}
ft := reflect.FuncOf([]reflect.Type{}, returnTypes, false)
fv := reflect.MakeFunc(ft, func([]reflect.Value) []reflect.Value {
return returnValues
})
return fv.Interface(), nil
}
func newPopulateFunc(targets ...interface{}) (interface{}, error) {
// Validate all targets are non-nil pointers.
targetTypes := make([]reflect.Type, len(targets))
for i, t := range targets {
if t == nil {
return nil, fmt.Errorf("failed to Populate: target %v is nil", i+1)
}
rt := reflect.TypeOf(t)
if rt.Kind() != reflect.Ptr {
return nil, fmt.Errorf("failed to Populate: target %v is not a pointer type, got %T", i+1, t)
}
targetTypes[i] = reflect.TypeOf(t).Elem()
}
// Build a function that looks like:
//
// func(t1 T1, t2 T2, ...) {
// *targets[0] = t1
// *targets[1] = t2
// [...]
// }
//
fnType := reflect.FuncOf(targetTypes, nil, false /* variadic */)
fn := reflect.MakeFunc(fnType, func(args []reflect.Value) []reflect.Value {
for i, arg := range args {
reflect.ValueOf(targets[i]).Elem().Set(arg)
}
return nil
})
return fn.Interface(), nil
}
//func invokeErr(err error) interface{} {
// return func() error {
// return err
// }
//}