-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fx.Decorate #833
Add fx.Decorate #833
Conversation
Codecov Report
@@ Coverage Diff @@
## master #833 +/- ##
==========================================
+ Coverage 98.32% 98.36% +0.04%
==========================================
Files 28 29 +1
Lines 1012 1041 +29
==========================================
+ Hits 995 1024 +29
Misses 11 11
Partials 6 6
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surprisingly simple after all of the other changes. No major concerns.
Minus the couple missing untested corner cases, I think this is good to go.
Please get a review from someone else on the team as well.
As an aside: 🎉 🎉 🎉
) | ||
|
||
app := fxtest.New(t, | ||
testRedis, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine as a test, but in the real world, I expect we will not nest "redis" under "testredis".
decorate_test.go
Outdated
defer app.RequireStart().RequireStop() | ||
}) | ||
|
||
t.Run("use Decorate in root", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice
var newC []*Coffee | ||
for _, c := range coffee { | ||
newC = append(newC, &Coffee{ | ||
Name: c.Name, | ||
Price: c.Price + 1, | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆
Heck yeah! Nice work :) |
Co-authored-by: r-hang <42982339+r-hang@users.noreply.github.com>
Great work! |
Thanks @Ardagan! Our current estimate is to ship this by end of the month. We have some more work to do to integrate this with the fxevent package, and add fx.Replace (though there's already a PR out for that - #837). We also need to verify this doesn't break any Go services here at Uber. After all those have been done, we should be good to go! |
@Ardagan it's been released now: https://github.com/uber-go/fx/releases/tag/v1.17.0 |
This adds
fx.Decorate
, which lets you specify decorators to an fx app. A decorator can take in one or more dependencies that have already beenProvide
d to the app, and produce one or more values that will be used as replacements in the object graph.For example, suppose there is a simple app like this:
Running this app will print "logger" on the console.
Now let us suppose a decorator was provided:
The decorator here will take in the provided Logger and replace it with another logger whose
Name
isdecorated logger
. TheInvoke
d function is then executed with this replacement value, so running this app will print "decorated logger" on the console.In terms of implementation, a decorator is represented by the target decorator function and the call stack it was provided from, similar to a provider.
module
contains a list of decorators that were specified within its scope.The dig dependency had to be updated to the latest master branch of Dig to ensure the fix for uber-go/dig#316 is in.
Following this PR, there are two additional pieces I will be adding:
This PR along with the two PRs above should make the long-awaited feature of graph modifications in fx finally possible.
Refs #653, #649, #825, uber-go/dig#230, GO-1203, GO-736