-
Notifications
You must be signed in to change notification settings - Fork 13
/
inject.go
224 lines (208 loc) · 4.7 KB
/
inject.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
package di
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// Inject indicates that struct public fields will be injected automatically.
//
// type Application struct {
// di.Inject
//
// Server *http.Server // will be injected
// }
//
// You can specify tags for injected types:
//
// type Application struct {
// di.Inject
//
// Public *http.Server `type:"public"` // *http.Server with type:public tag combination will be injected
// Private *http.Server `type:"private"` // *http.Server with type:private tag combination will be injected
// }
type Inject struct {
injectable
}
// injectable interface needs to struct fields injection functional.
type injectable interface {
isInjectable()
}
type field struct {
rt reflect.Type
tags Tags
optional bool
}
// canInject checks that type t contain di.Inject and supports injecting.
func canInject(t reflect.Type) bool {
if !t.Implements(injectableInterface) {
return false
}
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return false
}
return true
}
// parsePopulateFields parses fields of struct that can be populated.
func parsePopulateFields(rt reflect.Type) map[int]field {
if !canInject(rt) {
return nil
}
var rv reflect.Value
if !rv.IsValid() {
switch rt.Kind() {
case reflect.Ptr:
rv = reflect.New(rt.Elem())
default:
rv = reflect.New(rt).Elem()
}
}
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
rv = rv.Elem()
}
fields := make(map[int]field, rt.NumField())
// fi - field index
for fi := 0; fi < rt.NumField(); fi++ {
fv := rv.Field(fi)
// check that field can be set
if !fv.CanSet() {
continue
}
// cur - current field
cur := rt.Field(fi)
f, valid := inspectStructField(rt, cur)
if !valid {
continue
}
fields[fi] = field{
rt: cur.Type,
tags: f.tags,
optional: f.optional,
}
}
return fields
}
// inspectStructField parses struct field
func inspectStructField(rt reflect.Type, f reflect.StructField) (field, bool) {
result := field{
rt: f.Type,
tags: Tags{},
optional: false,
}
if f.Tag == "" {
return result, true
}
diTag, found := f.Tag.Lookup("di")
if found {
if diTag == "" {
return result, true
}
for _, v := range strings.Split(diTag, ",") {
v = strings.TrimSpace(v)
switch v {
case "skip":
return field{}, false
case "optional":
result.optional = true
default:
kv := strings.SplitN(v, "=", 2)
if len(kv) == 2 {
result.tags[kv[0]] = kv[1]
} else {
panic(fmt.Sprintf("invalid di tag: key=value got: %s", v))
}
}
}
return result, true
} else {
// handle the old deprecated struct tagging style.
result, noSkip := inspectStructFieldDeprecated(f)
tracer.Trace("Deprecation warning: please replace the field tags on '%s.%s' with: %v", rt.Name(), f.Name, newTagStyleText(result.tags, result.optional, !noSkip))
return result, noSkip
}
}
func newTagStyleText(tags map[string]string, optional bool, skip bool) string {
parts := []string{}
if skip {
parts = append(parts, "skip")
} else {
if optional {
parts = append(parts, "optional")
}
for k, v := range tags {
parts = append(parts, k+"="+v)
}
}
return `di:"` + strings.Join(parts, ",") + `"`
}
func inspectStructFieldDeprecated(f reflect.StructField) (field, bool) {
tags := Tags{}
t := string(f.Tag)
optional := false
// this code copied from reflect.StructField.Lookup() method.
for t != "" {
// Skip leading space.
i := 0
for i < len(t) && t[i] == ' ' {
i++
}
t = t[i:]
if t == "" {
break
}
// Scan to colon. A space, a quote or a control character is a syntax error.
// Strictly speaking, control chars include the range [0x7f, 0x9f], not just
// [0x00, 0x1f], but in practice, we ignore the multi-byte control characters
// as it is simpler to inspect the tag's bytes than the tag's runes.
i = 0
for i < len(t) && t[i] > ' ' && t[i] != ':' && t[i] != '"' && t[i] != 0x7f {
i++
}
if i == 0 || i+1 >= len(t) || t[i] != ':' || t[i+1] != '"' {
break
}
name := string(t[:i])
t = t[i+1:]
// Scan quoted string to find value.
i = 1
for i < len(t) && t[i] != '"' {
if t[i] == '\\' {
i++
}
i++
}
if i >= len(t) {
break
}
qvalue := string(t[:i+1])
t = t[i+1:]
value, err := strconv.Unquote(qvalue)
if err != nil {
break
}
if name == "skip" && value == "true" {
return field{
rt: f.Type,
tags: tags,
optional: optional,
}, false
}
if name == "optional" {
if value == "true" {
optional = true
}
continue
}
tags[name] = value
}
return field{
rt: f.Type,
tags: tags,
optional: optional,
}, true
}
var injectableInterface = reflect.TypeOf(new(injectable)).Elem()