di: github.com/sarulabs/di Index | Files

package di

import "github.com/sarulabs/di"

Index

Package Files

builder.go context.go contextCore.go contextGetter.go contextInterface.go contextLineage.go contextNastyGetter.go contextSlayer.go definition.go logger.go scope.go utils.go

Constants

const App = "app"

App is the name of the application scope.

const Request = "request"

Request is the name of the request scope.

const SubRequest = "subrequest"

SubRequest is the name of the subrequest scope.

type BasicLogger Uses

type BasicLogger struct{}

BasicLogger is a Logger that uses log.Println to write the error on the standard output.

func (BasicLogger) Error Uses

func (l BasicLogger) Error(args ...interface{})

type Builder Uses

type Builder struct {
    Logger Logger
    // contains filtered or unexported fields
}

Builder is the only way to create a working Context. The scopes and object definitions are set in the Builder that can create a Context based on these information.

func NewBuilder Uses

func NewBuilder(scopes ...string) (*Builder, error)

NewBuilder is the only way to create a working Builder. It initializes the Builder with a list of scopes. The scope are ordered from the wider to the narrower. If no scope is provided, the default scopes are used : [App, Request, SubRequest]

func (*Builder) AddDefinition Uses

func (b *Builder) AddDefinition(def Definition) error

AddDefinition adds an object Definition in the Builder. It returns an error if the Definition can't be added.

func (*Builder) Build Uses

func (b *Builder) Build() Context

Build creates a Context in the wider scope with all the current scopes and definitions.

func (*Builder) Definitions Uses

func (b *Builder) Definitions() map[string]Definition

Definitions returs a map with the objects definitions added with the AddDefinition method. The key of the map is the name of the Definition.

func (*Builder) IsDefined Uses

func (b *Builder) IsDefined(name string) bool

IsDefined returns true if there is already a definition with the given name.

func (*Builder) Scopes Uses

func (b *Builder) Scopes() []string

Scopes returns the list of available scopes.

func (*Builder) Set Uses

func (b *Builder) Set(name string, obj interface{}) error

Set adds a definition for an already build object. The scope used as the Definition scope is the Builder wider scope.

type Context Uses

type Context interface {
    // Definition returns the map of the available Definitions ordered by Definition name.
    // These Definitions represent all the objects that this Context can build.
    Definitions() map[string]Definition

    // Scope returns the Context scope.
    Scope() string

    // Scopes returns the list of available scopes.
    Scopes() []string

    // ParentScopes returns the list of scopes wider than the Context scope.
    ParentScopes() []string

    // SubScopes returns the list of scopes narrower than the Context scope.
    SubScopes() []string

    // Parent returns the parent Context.
    Parent() Context

    // SubContext creates a new Context in the next subscope
    // that will have this Container as parent.
    SubContext() (Context, error)

    // SafeGet retrieves an object from the Context.
    // The object has to belong to this scope or a wider one.
    // If the object does not already exist, it is created and saved in the Context.
    // If the object can't be created, it returns an error.
    SafeGet(name string) (interface{}, error)

    // Get is similar to SafeGet but it does not return the error.
    Get(name string) interface{}

    // Fill is similar to SafeGet but it does not return the object.
    // Instead it fills the provided object with the value returned by SafeGet.
    // The provided object must be a pointer to the value returned by SafeGet.
    Fill(name string, dst interface{}) error

    // NastySafeGet retrieves an object from the Context, like SafeGet.
    // The difference is that the object can be retrieved
    // even if it belongs to a narrower scope.
    // To do so NastySafeGet creates a subcontext.
    // When the created object is no longer needed,
    // it is important to use the Clean method to Delete this subcontext.
    NastySafeGet(name string) (interface{}, error)

    // NastyGet is similar to NastySafeGet but it does not return the error.
    NastyGet(name string) interface{}

    // NastyFill is similar to NastySafeGet but copies the object in dst instead of returning it.
    NastyFill(name string, dst interface{}) error

    // Clean deletes the subcontext created by NastySafeGet, NastyGet or NastyFill.
    Clean()

    // DeleteWithSubContexts takes all the objects saved in this Context
    // and calls the Close function of their Definition on them.
    // It will also call DeleteWithSubContexts on each child and remove its reference in the parent Context.
    // After deletion, the Context can no longer be used.
    DeleteWithSubContexts()

    // Delete works like DeleteWithSubContexts but do not delete the subcontexts.
    // If the Context has subcontexts, it will not be deleted right away.
    // The deletion only occurs when all the subcontexts have been deleted.
    Delete()

    // IsClosed returns true if the Context has been deleted.
    IsClosed() bool
}

Context represents a dependency injection container. A Context has a scope and may have a parent with a wider scope and children with a narrower scope. Objects can be retrieved from the Context. If the desired object does not already exist in the Context, it is built thanks to the object Definition. The following attempts to get this object will return the same object.

type Definition Uses

type Definition struct {
    Name  string
    Scope string
    Build func(ctx Context) (interface{}, error)
    Close func(obj interface{})
    Tags  []Tag
}

Definition contains information to build and close an object inside a Context.

type DefinitionMap Uses

type DefinitionMap map[string]Definition

DefinitionMap is a collection of Definition ordered by name.

func (DefinitionMap) Copy Uses

func (m DefinitionMap) Copy() map[string]Definition

Copy retuns a copy of the DefinitionMap.

type Logger Uses

type Logger interface {
    Error(args ...interface{})
}

Logger is the interface used to log errors that occurred while an object is built or closed.

type MuteLogger Uses

type MuteLogger struct{}

MuteLogger is a Logger that doesn't log anything.

func (MuteLogger) Error Uses

func (l MuteLogger) Error(args ...interface{})

type ScopeList Uses

type ScopeList []string

ScopeList is a slice of scope.

func (ScopeList) Copy Uses

func (l ScopeList) Copy() ScopeList

Copy returns a copy of the ScopeList.

func (ScopeList) ParentScopes Uses

func (l ScopeList) ParentScopes(scope string) ScopeList

ParentScopes retuns the scopes before the one given as parameter.

func (ScopeList) SubScopes Uses

func (l ScopeList) SubScopes(scope string) ScopeList

SubScopes retuns the scopes after the one given as parameter.

type Tag Uses

type Tag struct {
    Name string
    Args map[string]string
}

Tag can contain more specific information about a Definition. It is useful to find a Definition thanks to its tags instead of its name.

Package di imports 6 packages (graph). Updated 2017-02-19. Refresh now. Tools for package owners.