forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_args.go
184 lines (156 loc) · 4.13 KB
/
struct_args.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
package container
import (
"reflect"
"github.com/pkg/errors"
)
// In can be embedded in another struct to inform the container that the
// fields of the struct should be treated as dependency inputs.
// This allows a struct to be used to specify dependencies rather than
// positional parameters.
//
// Fields of the struct may support the following tags:
// optional if set to true, the dependency is optional and will
// be set to its default value if not found, rather than causing
// an error
type In struct{}
func (In) isIn() {}
type isIn interface{ isIn() }
var isInType = reflect.TypeOf((*isIn)(nil)).Elem()
// Out can be embedded in another struct to inform the container that the
// fields of the struct should be treated as dependency outputs.
// This allows a struct to be used to specify outputs rather than
// positional return values.
type Out struct{}
func (Out) isOut() {}
type isOut interface{ isOut() }
var isOutType = reflect.TypeOf((*isOut)(nil)).Elem()
func expandStructArgsConstructor(constructor ProviderDescriptor) (ProviderDescriptor, error) {
var foundStructArgs bool
var newIn []ProviderInput
for _, in := range constructor.Inputs {
if in.Type.AssignableTo(isInType) {
foundStructArgs = true
inTypes, err := structArgsInTypes(in.Type)
if err != nil {
return ProviderDescriptor{}, err
}
newIn = append(newIn, inTypes...)
} else {
newIn = append(newIn, in)
}
}
var newOut []ProviderOutput
for _, out := range constructor.Outputs {
if out.Type.AssignableTo(isOutType) {
foundStructArgs = true
newOut = append(newOut, structArgsOutTypes(out.Type)...)
} else {
newOut = append(newOut, out)
}
}
if foundStructArgs {
return ProviderDescriptor{
Inputs: newIn,
Outputs: newOut,
Fn: expandStructArgsFn(constructor),
Location: constructor.Location,
}, nil
}
return constructor, nil
}
func expandStructArgsFn(constructor ProviderDescriptor) func(inputs []reflect.Value) ([]reflect.Value, error) {
fn := constructor.Fn
inParams := constructor.Inputs
outParams := constructor.Outputs
return func(inputs []reflect.Value) ([]reflect.Value, error) {
j := 0
inputs1 := make([]reflect.Value, len(inParams))
for i, in := range inParams {
if in.Type.AssignableTo(isInType) {
v, n := buildIn(in.Type, inputs[j:])
inputs1[i] = v
j += n
} else {
inputs1[i] = inputs[j]
j++
}
}
outputs, err := fn(inputs1)
if err != nil {
return nil, err
}
var outputs1 []reflect.Value
for i, out := range outParams {
if out.Type.AssignableTo(isOutType) {
outputs1 = append(outputs1, extractFromOut(out.Type, outputs[i])...)
} else {
outputs1 = append(outputs1, outputs[i])
}
}
return outputs1, nil
}
}
func structArgsInTypes(typ reflect.Type) ([]ProviderInput, error) {
n := typ.NumField()
var res []ProviderInput
for i := 0; i < n; i++ {
f := typ.Field(i)
if f.Type.AssignableTo(isInType) {
continue
}
var optional bool
optTag, found := f.Tag.Lookup("optional")
if found {
if optTag == "true" {
optional = true
} else {
return nil, errors.Errorf("bad optional tag %q (should be \"true\") in %v", optTag, typ)
}
}
res = append(res, ProviderInput{
Type: f.Type,
Optional: optional,
})
}
return res, nil
}
func structArgsOutTypes(typ reflect.Type) []ProviderOutput {
n := typ.NumField()
var res []ProviderOutput
for i := 0; i < n; i++ {
f := typ.Field(i)
if f.Type.AssignableTo(isOutType) {
continue
}
res = append(res, ProviderOutput{
Type: f.Type,
})
}
return res
}
func buildIn(typ reflect.Type, values []reflect.Value) (reflect.Value, int) {
numFields := typ.NumField()
j := 0
res := reflect.New(typ)
for i := 0; i < numFields; i++ {
f := typ.Field(i)
if f.Type.AssignableTo(isInType) {
continue
}
res.Elem().Field(i).Set(values[j])
j++
}
return res.Elem(), j
}
func extractFromOut(typ reflect.Type, value reflect.Value) []reflect.Value {
numFields := typ.NumField()
var res []reflect.Value
for i := 0; i < numFields; i++ {
f := typ.Field(i)
if f.Type.AssignableTo(isOutType) {
continue
}
res = append(res, value.Field(i))
}
return res
}