-
Notifications
You must be signed in to change notification settings - Fork 291
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
Implement struct fx.Annotated to inject named values without relying on fx.Out #633
Conversation
Hello and thanks for the PR! This is a good start. To answer your questions (in-order):
|
), | ||
fx.Invoke(func(in in) { | ||
assert.NotNil(t, in.A, "expected in.A to be injected") | ||
assert.Equal(t, "foo", in.A.name, "expected to get a type 'a' of name 'foo'") |
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.
Instead of putting assertions in the Invoke
, consider moving them out with
the use of an fx.Populate
.
var result struct {
fx.In
A *a `name:"foo"`
}
app := fxtest.New(t,
...,
fx.Populate(&result),
)
defer app.RequireStart().RequireStop()
assert.[..]
Otherwise, if we have a bug that neglects to call that fx.Invoke
, the
assertion will be ignored and the test will still pass.
@@ -404,6 +404,13 @@ func (app *App) provide(constructor interface{}) { | |||
return | |||
} | |||
|
|||
if a, ok := constructor.(Annotated); ok { | |||
if err := app.container.Provide(a.Fn, dig.Name(a.Name)); err != nil { |
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.
For the case where the constructor is just a function, we should probably
verify that we're not producing fx.Annotated
s into the container. It would
be easy for someone to assume that they can do func() fx.Annotated
to get
the same behavior so we can catch that now and give them a better error
message.
You should be able to adapt some of the code in "internal/fxreflect".ReturnTypes
to get a list of reflect.Type
s being provided by the constructor and check
that one of them isn't reflect.TypeOf(Annotated{})
. (The code in that
package has some duplication with dig; we plan on exposing APIs in dig to
reduce that in the future.)
Thanks for your feedback @abhinav. Next week I will have time to work on it again. |
@dimiro1 Looks like this PR has stalled. Are you able to pick this back up at some point, or this kind of functionality is no longer desired for your use case? |
Hello @glibsm, sorry for that. I was planning to work on the PR but I could not find time to work on it. Feel free to close the PR. |
@dimiro1 Thanks for the contribution nonetheless. We have a good idea of the |
Superseeded by #656 |
See: #610
This is a version for review, there are a few things I would like to receive feedback.