Documentation ¶
Overview ¶
A simple, lightweight, and lazy-loaded DI (really just a singleton management) library.
An Instance of data stored in the Injector. This is what will be retrieved from and added to the Injector.
Index ¶
- type Args
- func (f Args) Any(idx int) (val interface{})
- func (f Args) Bool(idx int) (val bool)
- func (f Args) Float32(idx int) (val float32)
- func (f Args) Float64(idx int) (val float64)
- func (f Args) Int(idx int) (val int)
- func (f Args) Int64(idx int) (val int64)
- func (f Args) String(idx int) (val string)
- func (f Args) UInt(idx int) (val uint)
- type Binder
- type Binding
- type BindingBuilder
- type ConstantBindingBuilder
- type Factory
- type FactoryArgsBindingBuilder
- type FactoryBindingBuilder
- type InjectableEntityBindingBuilder
- type Injector
- type Instance
- func Any(instance interface{}) Instance
- func Bool(instance bool) Instance
- func Float32(instance float32) Instance
- func Float64(instance float64) Instance
- func Int(instance int) Instance
- func Int32(instance int32) Instance
- func Int64(instance int64) Instance
- func String(instance string) Instance
- func StructPtr(instance interface{}) Instance
- type Package
- type Provider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Args ¶
type Args []interface{}
Args that are passed into a Factory upon creation (call to injector.Get*(...)). These Args will take precedence over any and all other entities managed by axon. For instance, if you have a field that is tagged with inject, and is also instantiated via Args passed in a Factory, the value provided by the Arg will always remain.
Add Args through a Binding like so.
axon.NewPackage(axon.Bind("MyService").To().Factory(MyServiceFactory).WithArgs(axon.Args{"arg1"})
And access Args within a Factory like so.
func MyServiceFactory(args axon.Args) axon.Instance { return &myServiceImpl{StringField: args.String(0)} // The arg returned is the string "arg1". }
func (Args) Any ¶
Gets an interface{} from the Args. If the passed in index does not exist, a zero interface{} is returned
func (Args) Bool ¶
Gets a bool from the Args. If the passed in index is not a bool or does not exist, false is returned
func (Args) Float32 ¶
Gets a float32 from the Args. If the passed in index is not a float32 or does not exist, a zero float32 is returned
func (Args) Float64 ¶
Gets a float64 from the Args. If the passed in index is not a float64 or does not exist, a zero float64 is returned
func (Args) Int ¶
Gets a int from the Args. If the passed in index is not a int or does not exist, a zero int is returned
func (Args) Int64 ¶
Gets a int64 from the Args. If the passed in index is not a int64 or does not exist, a zero int64 is returned
type Binder ¶
type Binder interface { // The slice of Packages that this Binder stores. Packages() []Package }
A collection of Packages. The Binder is the top-level definition of what the Injector will store and how it will store it. The Bindings provided by this Binder's Packages will not be evaluated until an injector.Get*(string) method is called.
type Binding ¶
type Binding interface { // Retrieves the Instance that this Binding manages. This field is mutually exclusive with the Provider stored // in the Binding. GetInstance() Instance // Retrieves the Provider that this Binding manages. This field is mutually exclusive with the Instance stored // in the Binding. The Key tied to the Provider will ultimately be tied to the Instance that the Provider produces, // not the Provider itself. GetProvider() Provider // The key that is bound to the Provider or Instance. GetKey() string }
A "binder" between a Key and an Injectable Entity (Instance or Provider) within an Injector. A Binding will never hold both an Instance and a Provider.
type BindingBuilder ¶
type BindingBuilder interface { // Passthrough for defining an Injectable Entity (Provider or Instance). To() InjectableEntityBindingBuilder }
An intermediary builder for a Binding. Offers no functional impact on the Binding. Is only comprised of prepositions to allow the flow of the builder to be more intuitive.
func Bind ¶
func Bind(key string) BindingBuilder
Defines a Binding in a Package from a specified key to an Instance or a Provider.
axon.NewBinder(axon.NewPackage( axon.Bind("UserService").To().Instance(axon.StructPtr(new(UserService))), axon.Bind("MyString").To().String("hello world"), axon.Bind("DB").To().Factory(DBFactory).WithoutArgs(), ))
type ConstantBindingBuilder ¶
type ConstantBindingBuilder interface { // Binds the provided Key to a string value. String(s string) Binding // Binds the provided Key to an int value. Int(i int) Binding // Binds the provided Key to an int32 value. Int32(i int32) Binding // Binds the provided Key to an int64 value. Int64(i int64) Binding // Binds the provided Key to a float32 value. Float32(f float32) Binding // Binds the provided Key to a float64 value. Float64(f float64) Binding // Binds the provided Key to a bool value. Bool(b bool) Binding // Binds the provided Key to a ptr to a struct value. StructPtr(s interface{}) Binding // Binds the provided Key to the value that is passed in. Any(s interface{}) Binding }
Builds Bindings which utilize constants.
type Factory ¶
Function that constructs an Instance. The args field are provided via the Provider listed within a Binding. The Injector provided is the state of the Injector at the time of calling the factory. This should be used carefully as a user can end up in an infinite loop
type FactoryArgsBindingBuilder ¶
type FactoryArgsBindingBuilder interface { // Pass in arguments into your Factory. // Bind("service").To().Factory(ServiceFactory).WithArgs(axon.Args{"my arg"}) // All arguments passed in here will take precedence over injected values. These will not be // overwritten. WithArgs(args Args) Binding // Specifies that the Factory within the Binding does not have Args. WithoutArgs() Binding }
type FactoryBindingBuilder ¶
type FactoryBindingBuilder interface { // Binds a Key to a provided Factory. Factory(factory Factory) FactoryArgsBindingBuilder }
Builds Bindings which utilize a Factory.
type InjectableEntityBindingBuilder ¶
type InjectableEntityBindingBuilder interface { FactoryBindingBuilder ConstantBindingBuilder // Bind the provided Key to an Instance. Instance(instance Instance) Binding }
Builder for a binding to an Injectable Entity (an Instance or a Provider).
type Injector ¶
type Injector interface { // Gets a specific Instance within the Injector. If the Instance does not yet exist (created via a Provider) it is // constructed here. If the key is not found, nil is returned. Get(key string) Instance // Get a struct Instance from the injector. This will always return a pointer to whatever struct // was passed in via the Binding. If not found, nil is returned. GetStructPtr(key string) interface{} // Get a bool Instance from the Injector. If not found, false is returned. GetBool(key string) bool // Get a int Instance from the Injector. If not found, 0 is returned. GetInt(key string) int // Get a string Instance from the Injector. If not found, "" is returned. GetString(key string) string // Get a float32 Instance from the Injector. If not found, 0 is returned. GetFloat32(key string) float32 // Get a float64 Instance from the Injector. If not found, 0 is returned. GetFloat64(key string) float64 // Get a int64 Instance from the Injector. If not found, 0 is returned. GetInt64(key string) int64 // Get a int32 Instance from the Injector. If not found, 0 is returned. GetInt32(key string) int32 // Add an Instance to the Injector. If the Instance with the specified key already exists, it is replaced. // // Every time this is called, all dependencies of the Instance will be rebuilt on subsequent Get*(string) // calls. // // WARNING: Do not use this method at runtime within source code. This method is for TESTING purposes in order to // provide mocks without having to define a completely separate Binder. If you use this within source code, it will // work within a synchronous environment but has undefined behavior in an asynchronous environment. Add(key string, instance Instance) // Add a Provider to the Injector. If the Instance with the specified key already exists, it is replaced. // // Every time this is called, all dependencies of the Instance will be rebuilt on subsequent Get*(string) // calls. // // WARNING: Do not use this method at runtime within source code. This method is for TESTING purposes in order to // provide mocks without having to define a completely separate Binder. If you use this within source code, it will // work within a synchronous environment but has undefined behavior in an asynchronous environment. AddProvider(key string, provider Provider) }
An Injector is, in essence, a map of a string Key to an Injectable Entity (Instance or a Provider). When a user calls injector.Get(key string) they are providing the key into the map which will either simply return the instance, or construct it via the Provider's Factory.
This is the main object you will interact with to get whatever you place into the injector via a Binder.
func NewInjector ¶
Creates and instantiates an Injector via a Binder provided by the user.
binder := axon.NewBinder(axon.NewPackage( axon.Bind("Car").To().Factory(CarFactory).WithoutArgs()), axon.Bind("Engine").To().Instance(axon.StructPtr(new(EngineImpl)), axon.Bind("LockCode").To().String(os.Getenv("LOCK_CODE")), )) injector := axon.NewInjector(binder)
The user can now retrieve their Instances via a call to the Injector's Get*(string) methods.
fmt.Println(injector.GetString("LockCode")) // Prints the value of the env var "LOCK_CODE"
Or the Binding can be replaced before running a test via the injector.Add(...) method.
injector.Add("Engine", axon.StructPtr(new(EngineMock)))
type Instance ¶
type Instance interface { // Get the reflect Kind of the Instance. To note, this reflect.Kind is consistent within // the axon codebase but may not be consistent with the raw Go type. For instance, a Struct // type is actually a reflect.Ptr. GetKind() reflect.Kind // The pointer to the struct that the Instance is managing. GetStructPtr() interface{} // The int that the Instance is managing. GetInt() int // The int32 that the Instance is managing. GetInt32() int32 // The int64 that the Instance is managing. GetInt64() int64 // The bool that the Instance is managing. GetBool() bool // The string that the Instance is managing. GetString() string // The float32 that the Instance is managing. GetFloat32() float32 // The float64 that the Instance is managing. GetFloat64() float64 // The raw value that the Instance is managing. GetValue() interface{} // contains filtered or unexported methods }
func Any ¶
func Any(instance interface{}) Instance
An Instance of any type. This should be used as sparingly as possible as more reflection is needed to manage it (which is slower). Use this if none of the other Instance types can fit your needs.
func StructPtr ¶
func StructPtr(instance interface{}) Instance
An Instance of a struct ptr type. This type MUST be a ptr to a struct value. If it is not, a panic will occur. Good:
func MyStructFactory(_ axon.Injector, _ axon.Args) axon.Instance { return axon.StructPtr(new(MyStruct)) }
Bad:
func MyStructFactory(_ axon.Injector, _ axon.Args) axon.Instance { return axon.StructPtr(MyStruct{}) }
Will be stored within the Injector simply as &MyStruct{}
type Package ¶
type Package interface { // Returns all of the Bindings that this Package stores. Bindings() []Binding }
A group of Bindings that are conceptually tied together. A Package only serves as a way to separate Binding definitions into different conceptual chunks. A Package does not provide any functional difference within the Injector
func NewPackage ¶
Creates a new Package to pass into your Binder. You can also define a Package as a struct by implementing the Package interface like so
type ConfigPackage struct { } type ConfigPackage struct { } func (*ConfigPackage) Entries() []Binding { return []Binding{ Bind("Dev").To().Factory(TestServiceFactory).WithoutArgs(), } } ... axon.NewBinder(new(ConfigPackage), ...)
type Provider ¶
type Provider interface { // The Factory that the Provider will use to construct your Instance when called upon by the injector.Get(key string) // method. GetFactory() Factory // Arguments that will be passed into your Factory when it's called. GetArgs() Args }
Defines how to "provide" an Instance to the Injector via a Factory. A Provider's Factory will only ever be called once by the Injector. After the Provider's Factory creates your Instance, the injector will stop referring to the Provider and will only refer to the Instance that the Provider's Factory produced.
func NewProvider ¶
Creates a new Provider from the passed in Args and Factory. This function should mainly be used for adding Mock Providers into your tests. Rather than calling this function directly within source code, use Bindings instead like so
axon.Bind("MyService").To().Factory(ServiceFactory).WithoutArgs()