Skip to content
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

Annotated Invokes #691

Closed
fionera opened this issue Dec 23, 2019 · 2 comments
Closed

Annotated Invokes #691

fionera opened this issue Dec 23, 2019 · 2 comments

Comments

@fionera
Copy link

fionera commented Dec 23, 2019

Hi,

currently we need to call fx.Invoke with a custom Type with fx.In and the parameters. How about implementing support for fx.Annotated in fx.Invoke calls?

@abhinav
Copy link
Collaborator

abhinav commented Jan 27, 2020

Hey, we considered that but the problem is that constructors usually have
multiple parameters. A name or group specified with fx.Annotated would
apply to all parameters, which is not what you usually want.

For example, if you have,

func New(ro *sql.DB, rw *sql.DB) Repository

There's no easy way to annotate the names for individual parameters.
We're open to suggestions that address this concern.

@abhinav
Copy link
Collaborator

abhinav commented Nov 10, 2021

This feature is now available in v1.15.0 in the following form:

fx.Invoke(
  fx.Annotate(
    func(foo Thing) {
        // ...
    },
    fx.ParamTags(`name:"a"`),
  ),
)

Which is roughly equivalent to,

type invokeParams struct {
    fx.In
    
    Foo Thing `name:"a"`
}

fx.Invoke(
    func(p invokeParams) {
        foo := p.Foo

        // ...
    },
)

@abhinav abhinav closed this as completed Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants