fxx

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

README

GoDev Master

FXX - an extention to uber-go/fx

See docs for reference

Documentation

Index

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 Hook

func Hook(hook FxHook) fx.Hook

func HookStart

func HookStart(hook FxHookOnStart) fx.Hook

func HookStop

func HookStop(hook FxHookOnStop) fx.Hook

func ProvideConfig

func ProvideConfig(name string, unmarshal Unmarshal) fx.Option

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 FxHookOnStart interface {
	OnStart(context.Context) error
}

type FxHookOnStop

type FxHookOnStop interface {
	OnStop(context.Context) error
}

type ProvideConfigFileOption

type ProvideConfigFileOption func(o *provideConfigFileOptions)

func ProvideConfigFileWithName

func ProvideConfigFileWithName(name string) ProvideConfigFileOption

type Unmarshal

type Unmarshal func(interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL