-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
195 lines (170 loc) · 3.81 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
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
package materialize
import (
"fmt"
"reflect"
)
// FactoryFunc creates an instance.
type FactoryFunc func(*Context) (reflect.Value, error)
// Factory holds information for factory of a type.
type Factory struct {
Type reflect.Type
Func FactoryFunc
Tags Tags
}
func (f *Factory) newInstance(x *Context) (reflect.Value, error) {
return f.Func(x.child(f))
}
var (
errType = reflect.TypeOf((*error)(nil)).Elem()
ctxType = reflect.TypeOf((*Context)(nil))
)
func newFactory(fn interface{}, tags []string) (*Factory, error) {
rfn := reflect.ValueOf(fn)
ft := rfn.Type()
if ft.Kind() != reflect.Func {
return nil, ErrorFactoryType
}
var (
inP inProc
outP outProcs
typ reflect.Type
)
switch ft.NumOut() {
case 1:
outP.checkLen(1)
typ = ft.Out(0)
case 2:
// second of outs should be `error`.
if !ft.Out(1).AssignableTo(errType) {
return nil, fmt.Errorf("last of return values should be error")
}
outP.checkLen(2)
outP.checkErr(1)
typ = ft.Out(0)
default:
return nil, ErrorFactoryRetun
}
switch ft.NumIn() {
case 0:
inP = withoutContext
case 1:
// type of 1st arg should be *Context
if ft.In(0) != ctxType {
return nil, ErrorFactoryFirstArg
}
inP = withContext
outP.checkCtx()
default:
return nil, ErrorFactoryArgsRule
}
outP.checkZero()
return &Factory{
Type: typ,
Func: wrapFunc(typ, rfn, inP, outP),
Tags: newTags(tags),
}, nil
}
type inProc func(*Context) []reflect.Value
func withoutContext(*Context) []reflect.Value {
return nil
}
func withContext(x *Context) []reflect.Value {
return []reflect.Value{
reflect.ValueOf(x),
}
}
type outProc func(*Context, []reflect.Value) error
type outProcs []outProc
func (ps *outProcs) add(p ...outProc) {
*ps = append(*ps, p...)
}
func (ps *outProcs) checkLen(expect int) {
ps.add(func(x *Context, out []reflect.Value) error {
n := len(out)
if n == expect {
return nil
}
panic(fmt.Sprintf("factory for %s should return %d values but %d", x.typ(), expect, n))
})
}
func (ps *outProcs) checkZero() {
ps.add(func(x *Context, out []reflect.Value) error {
o0 := out[0]
if o0.Kind() == reflect.Ptr && o0.IsNil() {
return fmt.Errorf("factory for %s returned nil at 1st value", x.typ())
}
return nil
})
}
func (ps *outProcs) checkErr(nerr int) {
ps.add(func(x *Context, out []reflect.Value) error {
rerr := out[nerr]
if rerr.IsNil() {
return nil
}
err := rerr.Interface().(error)
return fmt.Errorf("factory for %s failed: %w", x.f.Type, err)
})
}
func (ps *outProcs) checkCtx() {
ps.add(func(x *Context, out []reflect.Value) error {
if x.err != nil {
return x.err
}
if x.val == nil {
return nil
}
v := out[0].Interface()
if v == x.val {
return nil
}
panic(fmt.Sprintf("resolved value doesn't matched: resolved=%v returned=%v", x.val, v))
})
}
// wrapFunc wraps a factory func can be used.
func wrapFunc(typ reflect.Type, fn reflect.Value, inP inProc, outP outProcs) FactoryFunc {
return func(x *Context) (reflect.Value, error) {
zv := reflect.Zero(typ)
out := fn.Call(inP(x))
for _, p := range outP {
err := p(x, out)
if err != nil {
return zv, err
}
}
// check context
if x.err != nil {
return zv, x.err
}
// XXX: verify these invalid codes.
//if x.val != nil {
//}
return out[0], nil
}
}
type factorySet map[string]*Factory
func (fs factorySet) add(f *Factory) error {
k := f.Tags.joinKeys()
if _, ok := fs[k]; ok {
return fmt.Errorf("duplicated factory for type:%s tags:%+v", f.Type, f.Tags)
}
fs[k] = f
return nil
}
func (fs factorySet) find(mf *matchedFactory, tags Tags) *matchedFactory {
for _, f := range fs {
sc := f.Tags.score(tags)
if sc >= 0 && (mf == nil || mf.sc < sc) {
mf = &matchedFactory{
fac: f,
sc: sc,
}
// FIXME: log matchedFactory
}
}
return mf
}
type matchedFactory struct {
fac *Factory
sc int
}