uber-go/fx

Factory like provider?

joesonw opened this issue · 3 comments

Is your feature request related to a problem? Please describe.
current I have this kind of scenario, I have mutiple endpoints registers into fx and wil be mounted to a service

type Endpoint1 struct {
    fx.In
    Logger *zap.Logger // prefer to be automatically provided as logger.Named('Endpoint1')
}
type Endpoint2 struct {
    fx.In
    Logger *zap.Logger   // prefer to be automatically provided as logger.Named('Endpoint2')
}

But all the endpoints receives the same logger. I would like it to invoke some kind of factory method that is aware of what and where an dependency is injected, thus each logger can be named one.

Describe the solution you'd like
A clear and concise description of what you want to happen.

fx.ProvideFactory((*zap.Logger)(nil), func (logger *zap.Logger) {
  return func (contextInfo *fx.FactoryDenepdencyContext) fx.Option {
     in, ok := contextInfo.Target.(interface { GetPath() string })
     if ok {
       return logger.Named(in.GetPath())
     }
     return logger
  }
})

Describe alternatives you've considered
Currently, it's being managed by hard coding.

Is this a breaking change?
No, This will be an additional feature

Additional context
.

Same concerns for us :)