Documentation
¶
Index ¶
- func ExtractConfigField(tag string, in interface{}, options ...ExtractConfigFieldOption) fx.Option
- func Hook(hook FxHook) fx.Hook
- func HookStart(hook FxHookOnStart) fx.Hook
- func HookStop(hook FxHookOnStop) fx.Hook
- func ProvideConfig(name string, unmarshal Unmarshal) fx.Option
- func ProvideJSONConfigFile(path string, options ...ProvideConfigFileOption) fx.Option
- func ProvideYAMLConfigFile(path string, options ...ProvideConfigFileOption) fx.Option
- func WithAnnotated(annos ...Annotation) func(interface{}) interface{}
- type Annotation
- type ExtractConfigFieldOption
- type FxHook
- type FxHookOnStart
- type FxHookOnStop
- type ProvideConfigFileOption
- type Unmarshal
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractConfigField ¶
func ExtractConfigField(tag string, in interface{}, options ...ExtractConfigFieldOption) fx.Option
For example,
type MySQLConfig struct {
Address string `json:"address"`
User string `json:"user"`
Password string `json:"password"`
}
fx.New(
fxx.ProvideJSONConfigFile("/etc/config/myapp.json"),
fxx.ExtractConfigField(`json:"mysql"`, &MySQLConfig{}),
fx.Provide(func (config *MySQLConfig) (*sql.DB, error) {
return sql.Open("mysql", fmt.Sprintf("mysql://%s:%s@tcp(%s)", config.User, config.Password, config.Address))
}),
)
ExtractConfigField inject given struct with value from a field of a config file
func HookStart ¶
func HookStart(hook FxHookOnStart) fx.Hook
func HookStop ¶
func HookStop(hook FxHookOnStop) fx.Hook
func ProvideConfig ¶
ProvideConfig inject an unmarshaler with name, which can be specifically used by ExtractConfigFieldFromFile
func ProvideJSONConfigFile ¶
func ProvideJSONConfigFile(path string, options ...ProvideConfigFileOption) fx.Option
ProvideJSONConfigFile an helper to ProvideConfigFile for json files, you only need to specify path to file here
func ProvideYAMLConfigFile ¶
func ProvideYAMLConfigFile(path string, options ...ProvideConfigFileOption) fx.Option
ProvideYAMLConfigFile an helper to ProvideConfigFile for yaml files, you only need to specify path to file here
func WithAnnotated ¶
func WithAnnotated(annos ...Annotation) func(interface{}) interface{}
WithAnnotated allows to inject annotated options without declare your own struct
For example,
func NewReadOnlyConnection(...) (*Connection, error)
fx.Provide(fx.Annotated{
Name: "ro",
Target: NewReadOnlyConnection,
})
fx.Supply(&Server{})
fx.Invoke(fx.WithAnnotated(fx.NameAnnotation("ro)(func(roConn *Connection, s *Server) error {
})
Is equivalent to,
type Params struct {
fx.In
Connection *Connection `name:"ro"`
Server *Server
}
fx.Invoke(func(params Params) error {
roConn := params.Connection
s := params.Server
return nil
})
WithAnnotated takes an array of names, and returns function to be called with user function. names are in order.
Types ¶
type Annotation ¶
type Annotation interface {
IsOptional() Annotation
// contains filtered or unexported methods
}
Annotation this will be passed to WithAnnotated to identify which to be injected
func GroupAnnotation ¶
func GroupAnnotation(group string) Annotation
GroupAnnotation use group Annotated inject
func NameAnnotation ¶
func NameAnnotation(name string) Annotation
NameAnnotation use name Annotated inject
type ExtractConfigFieldOption ¶
type ExtractConfigFieldOption func(o *extractConfigFieldOptions)
func ExtractConfigFieldFromFile ¶
func ExtractConfigFieldFromFile(file string) ExtractConfigFieldOption
type FxHook ¶
type FxHook interface {
FxHookOnStart
FxHookOnStop
}
type FxHookOnStart ¶
type FxHookOnStop ¶
type ProvideConfigFileOption ¶
type ProvideConfigFileOption func(o *provideConfigFileOptions)
func ProvideConfigFileWithName ¶
func ProvideConfigFileWithName(name string) ProvideConfigFileOption