inject

package module
Version: v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2020 License: MIT Imports: 2 Imported by: 2

README

Inject

Is a simplified dependency injection library

Build Status Go Report Card GoDoc GitHub license gocover.io

Example


package main

import (
	"fmt"
	"reflect"

	"github.com/wzshiming/inject"
)

func main() {

	inj := inject.NewInjector(nil)
	inj.Map(reflect.ValueOf(10))
	inj.Map(reflect.ValueOf("Hello world"))

	inj.Call(reflect.ValueOf(func(i int, s string) {
		fmt.Println(s, i)
		// Hello world 10
	}))

	t := struct {
		I int
		S string
	}{}
	inj.InjectStruct(reflect.ValueOf(&t))
	fmt.Println(t)
	// {10 Hello world}
}

License

Pouch is licensed under the MIT License. See LICENSE for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Injector

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

Injector Save the current mapping and parent injectors.

func NewInjector

func NewInjector(parent *Injector) *Injector

NewInjector Create a new injector.

func (*Injector) Call

func (in *Injector) Call(fun reflect.Value) ([]reflect.Value, error)

Call Find the parameter injection from the mapping and call the function.

func (*Injector) Child

func (in *Injector) Child() *Injector

Child returns a sub-injector and inherit the current.

func (*Injector) Get

func (in *Injector) Get(typ reflect.Type) reflect.Value

Get Find the value of the type from the mapping and return zero value if not.

func (*Injector) Inject

func (in *Injector) Inject(val reflect.Value) error

Inject inject this value.

func (*Injector) InjectStruct

func (in *Injector) InjectStruct(val reflect.Value) error

InjectStruct inject this value, structure will find the not `inject:"-"` tags for injection.

func (*Injector) Lookup

func (in *Injector) Lookup(typ reflect.Type) (reflect.Value, bool)

Lookup Find the value of the type from the mapping.

func (*Injector) Map

func (in *Injector) Map(val reflect.Value) error

Map map this value.

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL