godif

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2019 License: MIT Imports: 5 Imported by: 10

README

godif

Go dependency injection for functions (and not only...)

Usage Example

Usage

Provide key-value

  • Declare: var MyMap map[string]int
  • Require skipped, no error
  • Implement
    • Manually: MyMap = map[string]int{}
      • Use Provide() -> error
    • Provide implementation: godif.Provide(&MyMap, map[string]int{})
      • Multiple implementations -> error
      • Non-map or map of incompatible key or value type -> error
    • No implementation -> error
  • Provide data: godif.ProvideKeyValue(&MyMap, "key1", 1)
    • Multiple values per key -> error
    • Key or value of different types provided -> error

Provide key-slice

  • Declare: var MyMap map[string][]int
  • Require skipped, no error
  • Implement
    • Manually: MyMap = map[string][]int{}
      • Use Provide() -> error
    • Provide implementation: godif.Provide(&MyMap, map[string][]int{})
      • Multiple implementations -> error
      • slice of incompatible element type -> error
    • No implementation -> error
  • Add initial data if needed: MyMap["key1"] = append(MyMap["key1"], 42)
    • Further godif.ProvideKeyValue() calls will append data to the existing slice
  • Provide data:
    • godif.ProvideKeyValue(&MyMap, "key1", 1)
    • godif.ProvideKeyValue(&MyMap, "key1", 2)
    • godif.ProvideKeyValue(&MyMap, "key1", []int{3, 4})

Provide slice element

  • Declare: var MySlice []string
  • Implement
    • Manually: MySlice = []string{}
      • Use Provide() -> error
    • Provide implementation: godif.Provide(&MySlice, []string{})
      • Multiple implementations -> error
      • Incompatible types -> error
    • No implementation -> error
  • Add initial data if needed: MySlice = append(MySlice, 42)
    • Further godif.ProvideSliceElement() calls will append data to the existing slice
  • Provide data:
    • godif.ProvideSliceElement(&MySlice, "str1")
    • godif.ProvideSliceElement(&MySlice, []string{"str3", "str4"})

Reset all injections

  • godif.Reset()
  • Provided vars will be nilled
  • Manually inited vars will be kept
  • Data injected into manually inited vars will be kept

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Provide

func Provide(ref interface{}, funcImplementation interface{})

Provide registers implementation of ref type

func ProvideKeyValue added in v0.2.0

func ProvideKeyValue(pointerToMap interface{}, key interface{}, value interface{})

ProvideKeyValue s.e.

func ProvideSliceElement added in v0.2.0

func ProvideSliceElement(pointerToSlice interface{}, element interface{})

ProvideSliceElement s.e.

func Require

func Require(toInject interface{})

Require registers dep

func Reset

func Reset()

Reset clears all assignations

Types

type EAlreadyResolved added in v0.2.0

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

EAlreadyResolved occurs on ResolveAll() call if it called already

func (*EAlreadyResolved) Error added in v0.2.0

func (e *EAlreadyResolved) Error() string

type EImplementationNotProvided

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

EImplementationNotProvided error occurs if there is no implementation provided for a type

func (*EImplementationNotProvided) Error

type EImplementationProvidedForNonNil added in v0.2.0

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

EImplementationProvidedForNonNil error occurs if target value is not nil but implementation provided

func (*EImplementationProvidedForNonNil) Error added in v0.2.0

type EIncompatibleTypesFunc added in v0.2.0

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

EIncompatibleTypesFunc error occurs if type of a requirement (func) is incompatible to provided implementation

func (*EIncompatibleTypesFunc) Error added in v0.2.0

func (e *EIncompatibleTypesFunc) Error() string

type EIncompatibleTypesStorage added in v0.4.0

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

EIncompatibleTypesStorage error occurs if type of an array or slice element or key or value of map is incompatible to provided implementation

func (*EIncompatibleTypesStorage) Error added in v0.4.0

func (e *EIncompatibleTypesStorage) Error() string

type EMultipleFuncImplementations added in v0.4.0

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

EMultipleFuncImplementations occurs if there are more than one implementations provided for one func

func (*EMultipleFuncImplementations) Error added in v0.4.0

type EMultipleStorageImplementations added in v0.4.0

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

EMultipleStorageImplementations occurs if there are more than one implementations provided for slice or map

func (*EMultipleStorageImplementations) Error added in v0.4.0

type EMultipleValues

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

EMultipleValues error occurs if more than one value is provided per one key by ProvideMapValue() call

func (*EMultipleValues) Error

func (e *EMultipleValues) Error() string

type ENonAssignableRequirement

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

ENonAssignableRequirement error occurs if non-assignable (e.g. not variable) requirement is declared

func (*ENonAssignableRequirement) Error

func (e *ENonAssignableRequirement) Error() string

type EProvidedNotUsed

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

EProvidedNotUsed error occurs if something is provided but not required

func (*EProvidedNotUsed) Error

func (e *EProvidedNotUsed) Error() string

type Errors

type Errors []error

Errors is multiple errors container

func ResolveAll

func ResolveAll() Errors

ResolveAll all deps

func (Errors) Error

func (e Errors) Error() string

Directories

Path Synopsis
example command

Jump to

Keyboard shortcuts

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