Skip to content

Commit 1d9f0f1

Browse files
authored
Add Scope.Decorate (#313)
This adds Scope.Decorate which lets the user decorate an existing provider for a given type by overriding that with another provider. For example, c := dig.New() c.Provide(func() *Logger { return Logger { Name: "Default", } }) c.Decorate(func(l *Logger) *Logger { return Logger { Name: "Decorated", } }) the code snippet above shows a provider that injects a *Logger type into the top-level scope. Then it adds a decorator which takes in the *Logger type and replaces its name with something else. Once that has been done, the following Invoke will get the decorated name: dig.Invoke(func(l *Logger) { l.Log(l.Name) // will log "Decorated" }) Decorations are limited to its scope only. i.e. a child's decorator does not affect the parent scope or any of the ancestor scopes. A parent's decorator does affect the child and the descendant scopes. One limitation: a Scope can only decorate a type once. It is possible, however, to create a child scope and then decorate once more in the child scope. In such case, the parent's decorator will be executed prior to the child's decorator. In terms of implementation, an additional set of types were added. Namely: decorator: This is an interface that abstract a decorator that was provided to a Scope. decoratorNode: This implements the decorator interface. DecorateOption: Functional options for Scope.Decorate. Unimplemented. In addition, Scope also has new fields to keep track of all the decorators and decorated values/value groups that were injected in the scope directly. The way they work is quite similar to providers and values/value groups list. Basically, decorators are analogous to providers (constructors), and it keeps a separated list of decorated values and value groups in addition to provider-injected values and value groups.
1 parent f478a90 commit 1d9f0f1

File tree

12 files changed

+1003
-39
lines changed

12 files changed

+1003
-39
lines changed

constructor.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"reflect"
2626

27+
"go.uber.org/dig/internal/digerror"
2728
"go.uber.org/dig/internal/digreflect"
2829
"go.uber.org/dig/internal/dot"
2930
)
@@ -135,7 +136,7 @@ func (n *constructorNode) Call(c containerStore) error {
135136
}
136137
}
137138

138-
args, err := n.paramList.BuildList(c)
139+
args, err := n.paramList.BuildList(c, false /* decorating */)
139140
if err != nil {
140141
return errArgumentsFailed{
141142
Func: n.location,
@@ -145,7 +146,7 @@ func (n *constructorNode) Call(c containerStore) error {
145146

146147
receiver := newStagingContainerWriter()
147148
results := c.invoker()(reflect.ValueOf(n.ctor), args)
148-
if err := n.resultList.ExtractList(receiver, results); err != nil {
149+
if err := n.resultList.ExtractList(receiver, false /* decorating */, results); err != nil {
149150
return errConstructorFailed{Func: n.location, Reason: err}
150151
}
151152

@@ -179,11 +180,19 @@ func (sr *stagingContainerWriter) setValue(name string, t reflect.Type, v reflec
179180
sr.values[key{t: t, name: name}] = v
180181
}
181182

183+
func (sr *stagingContainerWriter) setDecoratedValue(_ string, _ reflect.Type, _ reflect.Value) {
184+
digerror.BugPanicf("stagingContainerWriter.setDecoratedValue must never be called")
185+
}
186+
182187
func (sr *stagingContainerWriter) submitGroupedValue(group string, t reflect.Type, v reflect.Value) {
183188
k := key{t: t, group: group}
184189
sr.groups[k] = append(sr.groups[k], v)
185190
}
186191

192+
func (sr *stagingContainerWriter) submitDecoratedGroupedValue(_ string, _ reflect.Type, _ reflect.Value) {
193+
digerror.BugPanicf("stagingContainerWriter.submitDecoratedGroupedValue must never be called")
194+
}
195+
187196
// Commit commits the received results to the provided containerWriter.
188197
func (sr *stagingContainerWriter) Commit(cw containerWriter) {
189198
for k, v := range sr.values {

container.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,18 @@ type containerWriter interface {
7676
// overwritten.
7777
setValue(name string, t reflect.Type, v reflect.Value)
7878

79+
// setDecoratedValue sets a decorated value with the given name and type
80+
// in the container. If a decorated value with the same name and type already
81+
// exists, it will be overwritten.
82+
setDecoratedValue(name string, t reflect.Type, v reflect.Value)
83+
7984
// submitGroupedValue submits a value to the value group with the provided
8085
// name.
8186
submitGroupedValue(name string, t reflect.Type, v reflect.Value)
87+
88+
// submitDecoratedGroupedValue submits a decorated value to the value group
89+
// with the provided name.
90+
submitDecoratedGroupedValue(name string, t reflect.Type, v reflect.Value)
8291
}
8392

8493
// containerStore provides access to the Container's underlying data store.
@@ -94,11 +103,17 @@ type containerStore interface {
94103
// Retrieves the value with the provided name and type, if any.
95104
getValue(name string, t reflect.Type) (v reflect.Value, ok bool)
96105

106+
// Retrieves a decorated value with the provided name and type, if any.
107+
getDecoratedValue(name string, t reflect.Type) (v reflect.Value, ok bool)
108+
97109
// Retrieves all values for the provided group and type.
98110
//
99111
// The order in which the values are returned is undefined.
100112
getValueGroup(name string, t reflect.Type) []reflect.Value
101113

114+
// Retrieves all decorated values for the provided group and type, if any.
115+
getDecoratedValueGroup(name string, t reflect.Type) (reflect.Value, bool)
116+
102117
// Returns the providers that can produce a value with the given name and
103118
// type.
104119
getValueProviders(name string, t reflect.Type) []provider
@@ -111,6 +126,14 @@ type containerStore interface {
111126
// type across all the Scopes that are in effect of this containerStore.
112127
getAllValueProviders(name string, t reflect.Type) []provider
113128

129+
// Returns the decorators that can produce values for the given name and
130+
// type.
131+
getValueDecorators(name string, t reflect.Type) []decorator
132+
133+
// Reutrns the decorators that can produce values for the given group and
134+
// type.
135+
getGroupDecorators(name string, t reflect.Type) []decorator
136+
114137
// Reports a list of stores (starting at this store) up to the root
115138
// store.
116139
storesToRoot() []containerStore

decorate.go

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
// Copyright (c) 2022 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+
type decorator interface {
32+
Call(c containerStore) error
33+
ID() dot.CtorID
34+
}
35+
36+
type decoratorNode struct {
37+
dcor interface{}
38+
dtype reflect.Type
39+
40+
id dot.CtorID
41+
42+
// Location where this function was defined.
43+
location *digreflect.Func
44+
45+
// Whether the decorator owned by this node was already called.
46+
called bool
47+
48+
// Parameters of the decorator.
49+
params paramList
50+
51+
// Results of the decorator.
52+
results resultList
53+
54+
// order of this node in each Scopes' graphHolders.
55+
orders map[*Scope]int
56+
57+
// scope this node was originally provided to.
58+
s *Scope
59+
}
60+
61+
func newDecoratorNode(dcor interface{}, s *Scope) (*decoratorNode, error) {
62+
dval := reflect.ValueOf(dcor)
63+
dtype := dval.Type()
64+
dptr := dval.Pointer()
65+
66+
pl, err := newParamList(dtype, s)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
rl, err := newResultList(dtype, resultOptions{})
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
n := &decoratorNode{
77+
dcor: dcor,
78+
dtype: dtype,
79+
id: dot.CtorID(dptr),
80+
location: digreflect.InspectFunc(dcor),
81+
orders: make(map[*Scope]int),
82+
params: pl,
83+
results: rl,
84+
s: s,
85+
}
86+
return n, nil
87+
}
88+
89+
func (n *decoratorNode) Call(s containerStore) error {
90+
if n.called {
91+
return nil
92+
}
93+
94+
if err := shallowCheckDependencies(s, n.params); err != nil {
95+
return errMissingDependencies{
96+
Func: n.location,
97+
Reason: err,
98+
}
99+
}
100+
101+
args, err := n.params.BuildList(n.s, true /* decorating */)
102+
if err != nil {
103+
return errArgumentsFailed{
104+
Func: n.location,
105+
Reason: err,
106+
}
107+
}
108+
109+
results := reflect.ValueOf(n.dcor).Call(args)
110+
if err := n.results.ExtractList(n.s, true /* decorated */, results); err != nil {
111+
return err
112+
}
113+
n.called = true
114+
return nil
115+
}
116+
117+
func (n *decoratorNode) ID() dot.CtorID { return n.id }
118+
119+
// DecorateOption modifies the default behavior of Provide.
120+
// Currently, there is no implementation of it yet.
121+
type DecorateOption interface {
122+
noOptionsYet()
123+
}
124+
125+
// Decorate provides a decorator for a type that has already been provided in the Container.
126+
// Decorations at this level affect all scopes of the container.
127+
// See Scope.Decorate for information on how to use this method.
128+
func (c *Container) Decorate(decorator interface{}, opts ...DecorateOption) error {
129+
return c.scope.Decorate(decorator, opts...)
130+
}
131+
132+
// Decorate provides a decorator for a type that has already been provided in the Scope.
133+
//
134+
// Similar to Provide, Decorate takes in a function with zero or more dependencies and one
135+
// or more results. Decorate can be used to modify a type that was already introduced to the
136+
// Scope, or completely replace it with a new object.
137+
//
138+
// For example,
139+
// s.Decorate(func(log *zap.Logger) *zap.Logger {
140+
// return log.Named("myapp")
141+
// })
142+
//
143+
// This takes in a value, augments it with a name, and returns a replacement for it. Functions
144+
// in the Scope's dependency graph that use *zap.Logger will now use the *zap.Logger
145+
// returned by this decorator.
146+
//
147+
// A decorator can also take in multiple parameters and replace one of them:
148+
// s.Decorate(func(log *zap.Logger, cfg *Config) *zap.Logger {
149+
// return log.Named(cfg.Name)
150+
// })
151+
//
152+
// Or replace a subset of them:
153+
// s.Decorate(func(
154+
// log *zap.Logger,
155+
// cfg *Config,
156+
// scope metrics.Scope
157+
// ) (*zap.Logger, metrics.Scope) {
158+
// log = log.Named(cfg.Name)
159+
// scope = scope.With(metrics.Tag("service", cfg.Name))
160+
// return log, scope
161+
// })
162+
//
163+
// Decorating a Scope affects all the child scopes of this Scope.
164+
//
165+
// Similar to a provider, the decorator function gets called *at most once*.
166+
func (s *Scope) Decorate(decorator interface{}, opts ...DecorateOption) error {
167+
_ = opts // there are no options at this time
168+
169+
dn, err := newDecoratorNode(decorator, s)
170+
if err != nil {
171+
return err
172+
}
173+
174+
keys := findResultKeys(dn.results)
175+
for _, k := range keys {
176+
if len(s.decorators[k]) > 0 {
177+
return fmt.Errorf("cannot decorate using function %v: %s already decorated",
178+
dn.dtype,
179+
k,
180+
)
181+
}
182+
s.decorators[k] = append(s.decorators[k], dn)
183+
}
184+
return nil
185+
}
186+
187+
func findResultKeys(r resultList) []key {
188+
// use BFS to search for all keys included in a resultList.
189+
var (
190+
q []result
191+
keys []key
192+
)
193+
q = append(q, r)
194+
195+
for len(q) > 0 {
196+
res := q[0]
197+
q = q[1:]
198+
199+
switch innerResult := res.(type) {
200+
case resultSingle:
201+
keys = append(keys, key{t: innerResult.Type, name: innerResult.Name})
202+
case resultGrouped:
203+
keys = append(keys, key{t: innerResult.Type.Elem(), group: innerResult.Group})
204+
case resultObject:
205+
for _, f := range innerResult.Fields {
206+
q = append(q, f.Result)
207+
}
208+
case resultList:
209+
q = append(q, innerResult.Results...)
210+
}
211+
}
212+
return keys
213+
}

0 commit comments

Comments
 (0)