Provide an array of interfaces as a dependency #880
-
Hey, I'm trying to setup an array of interfaces ( Thanks.
What I've tried with so far.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey there! It looks like what you're looking for is value groups. Value groups are a feature of Fx that allow:
You can use value groups in one of two forms:
Value groups with fx.In and fx.OutTo produce values to a value group, make the constructor for your producer return a struct that embeds fx.Out, and annotate the field for the value with type Result struct {
fx.Out
Result HealthChecker `group:"checkers"`
}
func NewDBHealthChecker(...) Result {
return ...
} To consume a value group, have your constructor accept a struct that embeds fx.In, and annotate the field with type Params struct {
fx.In
Checkers []HealthChecker `group:"checkers"`
}
func NewHealthCheckManager(p Params) HealthCheckManager {
// ...
} Value groups with annotationsA more concise and conveient form of using value groups is with To produce values to a value group, leave your original constructor unchanged and annotate it when you provide it to Fx—either via func NewDBHealthChecker(...) HealthChecker { ... }
// Elsewhere,
fx.Provide(
fx.Annotate(NewDBHealthChecker, fx.ResultTags(`group:"checkers"`)),
)
// Or,
var Module = fx.Module("DB",
fx.Provide(
fx.Annotate(NewDBHealthChecker, fx.ResultTags(`group:"checkers"`)),
// other provides for the DB module
),
)
To consume a value group, leave your original constructor similarly unchanged, provided that it accepts a slice of func NewHealthCheckerManager(healthCheckers []HealthChecker) *HealthCheckerManager {
...
}
// Elsewhere,
fx.Provide(
fx.Annotate(NewHealthCheckerManager, fx.ParamTags(`group:"checkers"`)),
) If your function accepts other parameters, you can pass // Given,
// func NewHealthCheckerManager(cfg *Config, checkers []HealthChecker) *HealthCheckManager
fx.Annotate(NewHealthCheckerManager, fx.ParamTags("", `group:"checkers"`)), Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hey there! It looks like what you're looking for is value groups.
Value groups are a feature of Fx that allow:
You can use value groups in one of two forms:
Value groups with fx.In and fx.Out
To produce values to a value group, make the constructor for your producer return a struct that embeds fx.Out, and annotate the field for the value with
group:"name"
where"name"
is the name of your value group.