injectgo

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2019 License: MIT Imports: 3 Imported by: 1 module, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

injectgo

Build Status Godoc

A golang inject module.

Usage

type Client struct {
    Address string
}

type Logger interface {
    Println(interface{})
}

type Model struct {
    Cli *Client `inject:""`
    Log Logger  `inject:""`
}

type dummyLogger struct{}

func (l dummyLogger) Println(s interface{}) {
    fmt.Println(s)
}

// ...

c := injectgo.NewContainer()

m := &Model{}

c.Provide(m, &Client{Address: "127.0.0.1:8080"})
c.ProvideFunc(injectgo.InjectFunc{
    Fn: func() (Logger, error) {
        return &dummyLogger{}, nil
    },
    Label: "",
})
c.Populate(nil)

m.Log.Println(m.Cli)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

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

Container receive all provided objects and function then inject all of them.

func NewContainer

func NewContainer() (c *Container)

NewContainer

func (*Container) Populate

func (c *Container) Populate(labelSelector FuncLabelSelector)

Populate call all provided functions then inject all provided and returned by function objects. It panics if any error occurs. Param labelSelector choice function with it's label. If nil passed, all function will selected.

func (*Container) Provide

func (c *Container) Provide(objs ...interface{})

Provide panics if objs are not pointer to struct or interface.

func (*Container) ProvideByName

func (c *Container) ProvideByName(name string, obj interface{})

ProvideByName panics if name is duplicate. Param name should match other object inject tag like `inject:"Name"`.

func (*Container) ProvideFunc

func (c *Container) ProvideFunc(funcs ...InjectFunc)

ProvideFunc support function types:

  • func() T
  • func() (T, error)

If use unsupport function as arguments, it will panic. Param label is associated with fn and can be selected. Only selected function will call. If label is empty, by default it is selected.

func (*Container) ProvideFuncByName

func (c *Container) ProvideFuncByName(name string, ifn InjectFunc)

ProvideFuncByName use `name` as object name, panic if name is duplicate.

type FuncLabelSelector

type FuncLabelSelector interface {
	IsLabelAllowed(string) bool
}

FuncLabelSelector is used to decide a label is selected or not.

type InjectFunc

type InjectFunc struct {
	Fn    interface{} // func() T / func() (T, error)
	Label string      // default selected
}

InjectFunc contains a function to new object and label of the function.

Jump to

Keyboard shortcuts

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