-
Notifications
You must be signed in to change notification settings - Fork 291
/
extract.go
156 lines (137 loc) · 4.03 KB
/
extract.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package fx
import (
"fmt"
"reflect"
"unicode"
"unicode/utf8"
)
var _typeOfIn = reflect.TypeOf(In{})
// Extract fills the given struct with values from the dependency injection
// container on application initialization. The target MUST be a pointer to a
// struct. Only exported fields will be filled.
//
// Deprecated: Use Populate instead.
func Extract(target interface{}) Option {
v := reflect.ValueOf(target)
if t := v.Type(); t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
return Error(fmt.Errorf("Extract expected a pointer to a struct, got a %v", t))
}
v = v.Elem()
t := v.Type()
// We generate a function which accepts a single fx.In struct as an
// argument. This struct contains all exported fields of the target
// struct.
// Fields of the generated fx.In struct.
fields := make([]reflect.StructField, 0, t.NumField()+1)
// Anonymous dig.In field.
fields = append(fields, reflect.StructField{
Name: _typeOfIn.Name(),
Anonymous: true,
Type: _typeOfIn,
})
// List of values in the target struct aligned with the fields of the
// generated struct.
//
// So for example, if the target is,
//
// var target struct {
// Foo io.Reader
// bar []byte
// Baz io.Writer
// }
//
// The generated struct has the shape,
//
// struct {
// fx.In
//
// F0 io.Reader
// F2 io.Writer
// }
//
// And `targets` is,
//
// [
// target.Field(0), // Foo io.Reader
// target.Field(2), // Baz io.Writer
// ]
//
// As we iterate through the fields of the generated struct, we can copy
// the value into the corresponding value in the targets list.
targets := make([]reflect.Value, 0, t.NumField())
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
// Skip unexported fields.
if f.Anonymous {
// If embedded, StructField.PkgPath is not a reliable indicator of
// whether the field is exported. See
// https://github.com/golang/go/issues/21122
t := f.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if !isExported(t.Name()) {
continue
}
} else if f.PkgPath != "" {
continue
}
// We don't copy over names or embedded semantics.
fields = append(fields, reflect.StructField{
Name: fmt.Sprintf("F%d", i),
Type: f.Type,
Tag: f.Tag,
})
targets = append(targets, v.Field(i))
}
// Equivalent to,
//
// func(r struct {
// fx.In
//
// F1 Foo
// F2 Bar
// }) {
// target.Foo = r.F1
// target.Bar = r.F2
// }
fn := reflect.MakeFunc(
reflect.FuncOf(
[]reflect.Type{reflect.StructOf(fields)},
nil, /* results */
false, /* variadic */
),
func(args []reflect.Value) []reflect.Value {
result := args[0]
for i := 1; i < result.NumField(); i++ {
targets[i-1].Set(result.Field(i))
}
return nil
},
)
return Invoke(fn.Interface())
}
// isExported reports whether the identifier is exported.
func isExported(id string) bool {
r, _ := utf8.DecodeRuneInString(id)
return unicode.IsUpper(r)
}