Documentation
¶
Index ¶
- func Get[T Dependency](name string) (T, error)
- func Inject(d Dependency)
- func Must[T any](v T, _ error) T
- func Parse(path ...string) error
- func Run[R Runnable]() error
- func Version() string
- type C
- type Config
- func (c Config) Any(key string) (any, error)
- func (c Config) Bool(key string) (bool, error)
- func (c Config) Duration(key string) (time.Duration, error)
- func (c Config) EnvString(key string) (string, error)
- func (c Config) Float(key string) (float64, error)
- func (c Config) Int(key string) (int, error)
- func (c Config) String(key string) (string, error)
- type D
- type Dependency
- type Idle
- type M
- type Optional
- type R
- type Runnable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶ added in v0.0.7
func Get[T Dependency](name string) (T, error)
Get returns the dependency by its name. It should be already initialized.
func Inject ¶
func Inject(d Dependency)
Inject adds a dependency to the global scope. Always inject dependencies you are going to use. Always pass the pointer to the dependency.
func Must ¶
Must is a helper function when working with `di.Config` that returns zero value of the type if the error is not nil.
func Parse ¶
Parse parses the global `di.yml` configuration file. It also sets the `Version` of current dependencies state.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config represents the configuration of the dependency. It exposes a bunch of useful methods to get the values.
func (Config) Any ¶
Any retrieves the value for the given key. It returns an error if the key is missing.
func (Config) EnvString ¶
EnvString retrieves the value for the given key as a string, treating values starting with "$" as environment variables. It returns an error if the environment variable is empty.
type Dependency ¶
type Dependency interface { // Name returns the name of the dependency. // The name is used to identify the dependency in the `di.yml`. Name() string // New creates a new instance of the dependency using the provided // config `di.Config`. Don't worry about filling the new instance with // other injectable dependencies, it will be done automatically. // // NOTE: Always return the pointer to the dependency. New(C) (D, error) }
Dependency represents an injectable dependency.
type Idle ¶ added in v0.0.7
type Idle struct{}
Idle is a dummy runnable that does nothing. Use it for initializing your dependencies without running anything.
type Optional ¶ added in v0.0.3
type Optional[T Dependency] struct { // contains filtered or unexported fields }
Optional is a wrapping type that can be used to inject optional dependencies.
func (Optional[T]) Get ¶ added in v0.0.3
Get returns the value of the optional dependency and a boolean flag indicating whether it was injected.
func (Optional[T]) Use ¶ added in v0.0.4
func (o Optional[T]) Use(f func(*T))
Use calls the provided function with the value of the optional dependency if it was injected. See With for the usage example.
func (Optional[T]) With ¶ added in v0.0.3
With calls the provided function with the value of the optional dependency if it was injected. This is an error-powered wrapper.
Usage:
var s struct { cache di.Optional[cache.Cache] `di:"x/cache"` } s.cache.With(func(c *cache.Cache) error { return c.Set("key", "value") })