Skip to content

Instantly share code, notes, and snippets.

@giovannizotta
Last active March 21, 2024 22:03
Show Gist options
  • Save giovannizotta/d74a5ccfa28208ad2f582590d10b605c to your computer and use it in GitHub Desktop.
Save giovannizotta/d74a5ccfa28208ad2f582590d10b605c to your computer and use it in GitHub Desktop.
package main
import (
"go.uber.org/fx"
)
type Fetcher interface{}
type GameFetcher interface {
Fetcher
}
type UserFetcher interface {
Fetcher
}
type GameFetcherImpl struct{}
type UserFetcherImpl struct{}
func newUserFetcher() *UserFetcherImpl {
return &UserFetcherImpl{}
}
func newGameFetcher() *GameFetcherImpl {
return &GameFetcherImpl{}
}
type StructUsingAllFetchers struct {
fetchers []Fetcher
}
func NewStructUsingAllFetchers(fetchers []Fetcher) *StructUsingAllFetchers {
return &StructUsingAllFetchers{
fetchers: fetchers,
}
}
func AsFetcher(f any, fetcherType any) any {
return fx.Annotate(
f,
fx.As(new(Fetcher)),
fx.As(fetcherType),
fx.ResultTags(`group:"fetchers"`),
)
}
func main() {
fx.New(
fx.Provide(
AsFetcher(newGameFetcher, new(GameFetcher)),
AsFetcher(newUserFetcher, new(UserFetcher)),
fx.Annotate(
NewStructUsingAllFetchers,
fx.ParamTags(`group:"fetchers"`),
),
),
fx.Invoke(func(s *StructUsingAllFetchers) {}),
fx.Invoke(func(gameFetcher GameFetcher) {}),
).Run()
}
@giovannizotta
Copy link
Author

giovannizotta commented Mar 21, 2024

[Fx] PROVIDE	main.Fetcher[group = "fetchers"] <= fx.Annotate(main.newGameFetcher(), fx.ResultTags(["group:\"fetchers\""]), fx.As([[main.Fetcher] [main.GameFetcher]])
[Fx] PROVIDE	main.GameFetcher[group = "fetchers"] <= fx.Annotate(main.newGameFetcher(), fx.ResultTags(["group:\"fetchers\""]), fx.As([[main.Fetcher] [main.GameFetcher]])
[Fx] PROVIDE	main.Fetcher[group = "fetchers"] <= fx.Annotate(main.newUserFetcher(), fx.ResultTags(["group:\"fetchers\""]), fx.As([[main.Fetcher] [main.UserFetcher]])
[Fx] PROVIDE	main.UserFetcher[group = "fetchers"] <= fx.Annotate(main.newUserFetcher(), fx.ResultTags(["group:\"fetchers\""]), fx.As([[main.Fetcher] [main.UserFetcher]])
[Fx] PROVIDE	*main.StructUsingAllFetchers <= fx.Annotate(main.NewStructUsingAllFetchers(), fx.ParamTags(["group:\"fetchers\""])
[Fx] PROVIDE	fx.Lifecycle <= go.uber.org/fx.New.func1()
[Fx] PROVIDE	fx.Shutdowner <= go.uber.org/fx.(*App).shutdowner-fm()
[Fx] PROVIDE	fx.DotGraph <= go.uber.org/fx.(*App).dotGraph-fm()
[Fx] INVOKE		main.main.func1()
[Fx] RUN	provide: fx.Annotate(main.newGameFetcher(), fx.ResultTags(["group:\"fetchers\""]), fx.As([[main.Fetcher] [main.GameFetcher]])
[Fx] RUN	provide: fx.Annotate(main.newUserFetcher(), fx.ResultTags(["group:\"fetchers\""]), fx.As([[main.Fetcher] [main.UserFetcher]])
[Fx] RUN	provide: fx.Annotate(main.NewStructUsingAllFetchers(), fx.ParamTags(["group:\"fetchers\""])
[Fx] INVOKE		main.main.func2()
[Fx] ERROR		fx.Invoke(main.main.func2()) called from:
main.main
	main.go:59
runtime.main
	/opt/homebrew/Cellar/go/1.21.5/libexec/src/runtime/proc.go:267
Failed: missing dependencies for function "main".main.func2
	main.go:59:
missing type:
	- main.GameFetcher (did you mean to use one of *main.StructUsingAllFetchers, fx.DotGraph, fx.Lifecycle, or fx.Shutdowner?)
[Fx] ERROR		Failed to start: missing dependencies for function "main".main.func2
	main.go:59:
missing type:
	- main.GameFetcher (did you mean to use one of *main.StructUsingAllFetchers, fx.DotGraph, fx.Lifecycle, or fx.Shutdowner?)
exit status 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment