uber-go/fx

fx.Invoke with value group

supersonicclay opened this issue · 2 comments

Is it possible to use fx.Invoke with a value group?

https://pkg.go.dev/go.uber.org/fx#hdr-Value_Groups

Seems there's a way to consume them in New calls

type ServerParams struct {
  fx.In

  Handlers []Handler `group:"server"`
}

func NewServer(p ServerParams) *Server {
  server := newServer()
  for _, h := range p.Handlers {
    server.Register(h)
  }
  return server
}

But the same doesn't seem to work with fx.Invoke

fx.Options(
  fx.Provide(New),
  fx.Invoke(ConsumeGroup),
)

func ConsumeGroup(handlers []Handler) {
  // ...
}

I'm getting an error:

missing dependencies for function \"...: missing type: []Handler"

Specifically, I'm trying to get glue decorators injected into a function.

Is it possible to use fx.Invoke with a value group?

It is possible. The function you're invoking (ConsumeGroup) does not specify that it's taking in the "server" group, so that's why it can't find the dependencies.

You can either fx.Annotate the function being invoked like:

fx.Invoke(
  fx.Annotate(ConsumeGroup, fx.ParamTags(`group:"server"`))
),

Or use fx.In struct like you did with ServerParams above.

Specifically, I'm trying to get glue decorators injected into a function.

Let's actually sync offline via internal Slack regarding your use case. Please ping me @ sungyoon.