Last active
March 21, 2024 22:03
-
-
Save giovannizotta/d74a5ccfa28208ad2f582590d10b605c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Author
giovannizotta
commented
Mar 21, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment