inject

package module
v0.0.0-...-14e6c00 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2018 License: MIT Imports: 5 Imported by: 0

README

Go-inject — Dependency Injection Library for Go

A dependency injection system for Go inspired by Guice. See the post for detailed guide.

Installation

Go get
go get github.com/monnoroch/go-inject
Dep
dep ensure -add github.com/monnoroch/go-inject

Examples

Inject an int value

Here's a simple module with a single provider:

import (
	"github.com/monnoroch/go-inject"
)

type singleValue struct{}

type MyModule struct{}

func (_ MyModule) ProvideValue() (int, singleValue) {
	return 10, singleValue{}
}

func main() {
	injector, _ := inject.InjectorOf(MyModule{})
	// Will be 10.
	value := injector.MustGet(new(int), singleValue{}).(int)
}
Dependencies

Let's add a second provider that depends on the first one:

type doubleValue struct{}

func (_ MyModule) ProvideValueDouble(value int, _ singleValue) (int, doubleValue) {
	return value * 2, doubleValue{}
}

func main() {
	injector, _ := inject.InjectorOf(MyModule{})
	// Will be 20.
	value := injector.MustGet(new(int), doubleValue{}).(int)
}
Cross-module dependencies

Let's add a second module, which can depend on values provided by the first module:

type tripleValue struct{}

type MyAnotherModule struct{}

func (_ MyAnotherModule) ProvideValue(
	value int, _ singleValue,
	doubledValue int, _ doubleValue,
) (int, tripleValue) {
	return doubledValue + value, tripleValue{}
}

func main() {
	injector, _ := inject.InjectorOf(
		MyModule{},
		MyAnotherModule{},
	)
	// Will be 30.
	value := injector.MustGet(new(int), tripleValue{}).(int)
}
Lazy dependencies

You can inject a function that returns a value. That way the value will be computed lazily.

type tripleValueLazy struct{}

func (_ MyAnotherModule) ProvideValueLazy(
	value int, _ singleValue,
	doubledValue func() int, _ doubleValue,
) (int, tripleValueLazy) {
	return doubledValue() + value, tripleValueLazy{}
}

func main() {
	injector, _ := inject.InjectorOf(
		MyModule{},
		MyAnotherModule{},
	)
	// Will be 30.
	value := injector.MustGet(new(int), tripleValueLazy{}).(int)
}
Auto injecting dependencies in struct fields
import (
	"github.com/monnoroch/go-inject"
	"github.com/monnoroch/go-inject/auto"
)

type MyStruct struct {
	Value        int
	DoubledValue int
}

type myAnnotation struct{}

func main() {
	injector, _ := inject.InjectorOf(
		MyModule{},
		autoinject.AutoInjectModule(new(MyStruct)).
			WithAnnotation(myAnnotation{}).
			WithFieldAnnotations(struct {
				Value        singleValue
				DoubledValue doubleValue
			}{}),
	)
	// Will be MyStruct{Value: 10, DoubledValue: 20}.
	value := injector.MustGet(new(MyStruct), myAnnotation{}).(MyStruct)
}

You can also inplement autoinject.AutoInjectable to provide default way to construct MyStruct:

func (self MyStruct) ProvideAutoInjectAnnotations() interface{} {
	return struct {
		Value        singleValue
		DoubledValue doubleValue
	}{}
}

func main() {
	injector, _ := inject.InjectorOf(
		MyModule{},
		autoinject.AutoInjectModule(new(MyStruct)).
			WithAnnotation(myAnnotation{}),
	)
	// Will be MyStruct{Value: 10, DoubledValue: 20}.
	value := injector.MustGet(new(MyStruct), myAnnotation{}).(MyStruct)
}

You can also use the default autoinject.Auto annotation to simplify code even further:

func main() {
	injector, _ := inject.InjectorOf(
		MyModule{},
		autoinject.AutoInjectModule(new(MyStruct)),
	)
	// Will be MyStruct{Value: 10, DoubledValue: 20}.
	value := injector.MustGet(new(MyStruct), autoinject.Auto{}).(MyStruct)
}
Other features

Providing singletons, private providers, annotation rewriting, dynamic modules and other features are explained in more detail in the guide.

Documentation

Overview

/ A dependency injection library for Go. / / This library provides the `Injector` type that is used for providing values and / after being configured with a collection of `Module`-s. / The library also provides iinterfaces for defining modules both from structs with provider methods and / dynamically generated providers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckModule

func CheckModule(module Module) error

CheckModule checks if the object is in fact a Module. Handles collections of modules correctly.

Types

type Annotation

type Annotation interface{}

/ Annotation is the interface that has to be implemented by all annotations. / It is empty, so implementation is trivial.

type DynamicModule

type DynamicModule interface {
	Module
	/// Generate a list of providers.
	Providers() ([]Provider, error)
}

/ Dynamic providers module. A type that, instead of having provider methods, / as with a static providers module, has a method for generating providers dynamically.

type Injector

type Injector struct {
	// contains filtered or unexported fields
}

Injector is a component for providing values exported by modules.

func InjectorOf

func InjectorOf(modules ...Module) (*Injector, error)

Create an injector from the list of modules.

func (*Injector) Get

func (self *Injector) Get(pointerToType interface{}, annotation Annotation) (interface{}, error)

Get the annotated value from the injector.

func (*Injector) MustGet

func (self *Injector) MustGet(pointerToType interface{}, annotation Annotation) interface{}

Get the annotated value from the injector, panic if there was an error.

type Module

type Module interface{}

/ Module is the interface that has to be implemented by all modules. / It is empty, so implementation is trivial. / In addition to this interface all Modules have to have methods that have two or three outputs: / - A value type. / - An annotation type. / - Optionally, an error. / These methods can have inputs that should come in pairs: values and their annotations.

func CombineModules

func CombineModules(modules ...Module) Module

Combine multiple modules into one.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

/ A dynamic provider is a type containing all the data about a provider that / the injector needs to be able to provide it.

func NewProvider

func NewProvider(function interface{}) Provider

/ Create a new provider from either a function or a `reflect.Value` with a function.

func Providers

func Providers(module Module) ([]Provider, error)

/ Get the list of providers from the module.

func (Provider) Cached

func (self Provider) Cached(cached bool) Provider

/ Create a cached or non-cached version of this provider.

func (Provider) Function

func (self Provider) Function() reflect.Value

/ Return the `reflect.Value` with the function of this provider.

func (Provider) IsCached

func (self Provider) IsCached() bool

/ Test if this provider is cached or not.

func (Provider) IsValid

func (self Provider) IsValid() bool

/ Test if the provider is valid: has the right number and types of inputs and outputs.

Directories

Path Synopsis
/ autoinject extends go-inject library with a way to automatically generate a provider / for a struct type.
/ autoinject extends go-inject library with a way to automatically generate a provider / for a struct type.
examples
/ rewrite provides tools to dynamically transform modules to chenge their providers using reflection.
/ rewrite provides tools to dynamically transform modules to chenge their providers using reflection.

Jump to

Keyboard shortcuts

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