|
| 1 | +// Copyright (c) 2021 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package dig |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "reflect" |
| 26 | + |
| 27 | + "go.uber.org/dig/internal/digreflect" |
| 28 | + "go.uber.org/dig/internal/dot" |
| 29 | +) |
| 30 | + |
| 31 | +// constructorNode is a node in the dependency graph that represents |
| 32 | +// a constructor provided by the user. |
| 33 | +// |
| 34 | +// constructorNodes can produce zero or more values that they store into the container. |
| 35 | +// For the Provide path, we verify that constructorNodes produce at least one value, |
| 36 | +// otherwise the function will never be called. |
| 37 | +type constructorNode struct { |
| 38 | + ctor interface{} |
| 39 | + ctype reflect.Type |
| 40 | + |
| 41 | + // Location where this function was defined. |
| 42 | + location *digreflect.Func |
| 43 | + |
| 44 | + // id uniquely identifies the constructor that produces a node. |
| 45 | + id dot.CtorID |
| 46 | + |
| 47 | + // Whether the constructor owned by this node was already called. |
| 48 | + called bool |
| 49 | + |
| 50 | + // Type information about constructor parameters. |
| 51 | + paramList paramList |
| 52 | + |
| 53 | + // Type information about constructor results. |
| 54 | + resultList resultList |
| 55 | + |
| 56 | + order int // order of this node in graphHolder |
| 57 | +} |
| 58 | + |
| 59 | +type constructorOptions struct { |
| 60 | + // If specified, all values produced by this constructor have the provided name |
| 61 | + // belong to the specified value group or implement any of the interfaces. |
| 62 | + ResultName string |
| 63 | + ResultGroup string |
| 64 | + ResultAs []interface{} |
| 65 | + Location *digreflect.Func |
| 66 | +} |
| 67 | + |
| 68 | +func newConstructorNode(ctor interface{}, c containerStore, opts constructorOptions) (*constructorNode, error) { |
| 69 | + cval := reflect.ValueOf(ctor) |
| 70 | + ctype := cval.Type() |
| 71 | + cptr := cval.Pointer() |
| 72 | + |
| 73 | + params, err := newParamList(ctype, c) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + results, err := newResultList( |
| 79 | + ctype, |
| 80 | + resultOptions{ |
| 81 | + Name: opts.ResultName, |
| 82 | + Group: opts.ResultGroup, |
| 83 | + As: opts.ResultAs, |
| 84 | + }, |
| 85 | + ) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + location := opts.Location |
| 91 | + if location == nil { |
| 92 | + location = digreflect.InspectFunc(ctor) |
| 93 | + } |
| 94 | + |
| 95 | + n := &constructorNode{ |
| 96 | + ctor: ctor, |
| 97 | + ctype: ctype, |
| 98 | + location: location, |
| 99 | + id: dot.CtorID(cptr), |
| 100 | + paramList: params, |
| 101 | + resultList: results, |
| 102 | + } |
| 103 | + n.order = c.newGraphNode(n) |
| 104 | + return n, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (n *constructorNode) Location() *digreflect.Func { return n.location } |
| 108 | +func (n *constructorNode) ParamList() paramList { return n.paramList } |
| 109 | +func (n *constructorNode) ResultList() resultList { return n.resultList } |
| 110 | +func (n *constructorNode) ID() dot.CtorID { return n.id } |
| 111 | +func (n *constructorNode) CType() reflect.Type { return n.ctype } |
| 112 | +func (n *constructorNode) Order() int { return n.order } |
| 113 | + |
| 114 | +func (n *constructorNode) String() string { |
| 115 | + return fmt.Sprintf("deps: %v, ctor: %v", n.paramList, n.ctype) |
| 116 | +} |
| 117 | + |
| 118 | +// Call calls this constructor if it hasn't already been called and |
| 119 | +// injects any values produced by it into the provided container. |
| 120 | +func (n *constructorNode) Call(c containerStore) error { |
| 121 | + if n.called { |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + if err := shallowCheckDependencies(c, n.paramList); err != nil { |
| 126 | + return errMissingDependencies{ |
| 127 | + Func: n.location, |
| 128 | + Reason: err, |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + args, err := n.paramList.BuildList(c) |
| 133 | + if err != nil { |
| 134 | + return errArgumentsFailed{ |
| 135 | + Func: n.location, |
| 136 | + Reason: err, |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + receiver := newStagingContainerWriter() |
| 141 | + results := c.invoker()(reflect.ValueOf(n.ctor), args) |
| 142 | + if err := n.resultList.ExtractList(receiver, results); err != nil { |
| 143 | + return errConstructorFailed{Func: n.location, Reason: err} |
| 144 | + } |
| 145 | + receiver.Commit(c) |
| 146 | + n.called = true |
| 147 | + |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +// stagingContainerWriter is a containerWriter that records the changes that |
| 152 | +// would be made to a containerWriter and defers them until Commit is called. |
| 153 | +type stagingContainerWriter struct { |
| 154 | + values map[key]reflect.Value |
| 155 | + groups map[key][]reflect.Value |
| 156 | +} |
| 157 | + |
| 158 | +var _ containerWriter = (*stagingContainerWriter)(nil) |
| 159 | + |
| 160 | +func newStagingContainerWriter() *stagingContainerWriter { |
| 161 | + return &stagingContainerWriter{ |
| 162 | + values: make(map[key]reflect.Value), |
| 163 | + groups: make(map[key][]reflect.Value), |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func (sr *stagingContainerWriter) setValue(name string, t reflect.Type, v reflect.Value) { |
| 168 | + sr.values[key{t: t, name: name}] = v |
| 169 | +} |
| 170 | + |
| 171 | +func (sr *stagingContainerWriter) submitGroupedValue(group string, t reflect.Type, v reflect.Value) { |
| 172 | + k := key{t: t, group: group} |
| 173 | + sr.groups[k] = append(sr.groups[k], v) |
| 174 | +} |
| 175 | + |
| 176 | +// Commit commits the received results to the provided containerWriter. |
| 177 | +func (sr *stagingContainerWriter) Commit(cw containerWriter) { |
| 178 | + for k, v := range sr.values { |
| 179 | + cw.setValue(k.name, k.t, v) |
| 180 | + } |
| 181 | + |
| 182 | + for k, vs := range sr.groups { |
| 183 | + for _, v := range vs { |
| 184 | + cw.submitGroupedValue(k.group, k.t, v) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments