-
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.Replace #837
Add Fx.Replace #837
Conversation
Codecov Report
@@ Coverage Diff @@
## master #837 +/- ##
==========================================
- Coverage 98.56% 98.42% -0.15%
==========================================
Files 29 30 +1
Lines 1043 1076 +33
==========================================
+ Hits 1028 1059 +31
- Misses 10 11 +1
- Partials 5 6 +1
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.
Minor issue in the test case.
Separately from this PR, we might need to tweak the value group handling a bit more.
Question: if I do this, what's the value in group:"t"
at the top-level/what should it be?
foo := fx.Module("foo",
fx.Supply(
fx.Annotate([]string{"a", "b", "c"}, `group:"t,flatten"`),
),
)
fx.New(
fx.Module("wrapfoo",
foo,
fx.Replace(
fx.Annotate([]string{"d", "e", "f"}, `group:"t"`),
),
),
)
@abg thanks for catching the issue with the test. Fixed that.
I believe that should be |
This adds `fx.Replace`, which replaces a value already provided in the graph with another value. This is similar to how `fx.Supply` provides an instantiated value to the graph. For example the following code that uses fx.Decorate to modify the `*bytes.Buffer` type... ```go fx.New( fx.Provide(func() *bytes.Buffer { ... }), fx.Decorate(func() *bytes.Buffer { ... }), fx.Invoke(func(b *bytes.Buffer) { // I get a modified buffer! }), ) ``` is the same as this version that uses fx.Replace: ```go b := func() *bytes.Buffer { ... } fx.New( fx.Provide(func() *bytes.Buffer { ... }), fx.Replace(b), fx.Invoke(func(b *bytes.Buffer) { // I get a modified buffer! }), ) ``` Implementation-wise, it is very similar to how fx.Supply creates a function that produces the given value using reflection and `dig.Provide`s it - fx.Replace creates a function that produces the given value with no argument using reflection, and `dig.Decorate` with that function.
This adds
fx.Replace
, which replaces a value already provided in the graph with another value. This is similar to howfx.Supply
provides an instantiated value to the graph.For example the following code that uses fx.Decorate to modify the
*bytes.Buffer
type...is the same as this version that uses fx.Replace:
Implementation-wise, it is very similar to how fx.Supply creates a function that produces the given value using reflection and
dig.Provide
s it - fx.Replace creates a function that produces the given value with no argument using reflection, anddig.Decorate
with that function.