godif

package module
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2020 License: MIT Imports: 6 Imported by: 10

README

codecov

godif

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

Usage Example

Usage

Provide func implementation

  • Declare: var toInject func()
  • Register to be injected: godif.Require(&toInject)
  • Provide implementation: godif.Provide(&toInject, f)
  • Resolve: godif.ResolveAll()
    • Incompatible types -> error
    • More than one implementations provided -> error
    • No implementation -> error
    • Something provided from a package but nothing is required for the package -> error (package is not used)
    • Non-assignable var provided on Require() or Provide() call -> error, further validation is skipped

Provide key-value

  • Declare: var MyMap map[string]int
  • Require skipped, no error
  • Implement
    • Manually: MyMap = map[string]int{}
    • Provide implementation: godif.Provide(&MyMap, map[string]int{})
  • Provide data: godif.ProvideKeyValue(&MyMap, "key1", 1)
  • Resolve: godif.ResolveAll()
    • Data provided but not implemented -> error
    • Use godif.Provide() if implemented manually -> error
    • If implementation provided
      • Multiple implementations -> error
      • Non-map or map of incompatible key or value type -> error
    • Multiple values per key -> error
    • Key or value of different types provided -> error
    • Non-assignable var provided or ProvideKeyValue() call -> error, further validation is skipped

Provide key-slice

  • Declare: var MyMap map[string][]int
  • Require skipped, no error
  • Implement
    • Manually: MyMap = map[string][]int{}
    • Provide implementation: godif.Provide(&MyMap, map[string][]int{})
  • 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})
  • Resolve: godif.ResolveAll()
    • Data provided but not implemented -> error
    • Use godif.Provide() if implemented manually -> error
    • If implementation provided
      • Multiple implementations -> error
      • Slice of incompatible element type -> error
    • Non-assignable var provided on ProvideKeyValue() call -> error, further validation is skipped

Provide slice element

  • Declare: var MySlice []string
  • Require skipped, no error
  • Implement
    • Manually: MySlice = []string{}
    • Provide implementation: godif.Provide(&MySlice, []string{})
  • 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"})
  • Resolve: godif.ResolveAll()
    • Data provided but not implemented -> error
    • Use godif.Provide() if implemented manually -> error
    • Multiple implementations -> error
    • Incompatible types -> error
    • Non-assignable var provided on ProvideSliceElement() call -> error, further validation is skipped

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

func ResolveAll

func ResolveAll() errs.Errors

ResolveAll all deps

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 EIncompatibleTypesStorageImpl added in v0.15.0

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

EIncompatibleTypesStorageImpl error occurs if type of array or slice or map is incompatible to provided implementation

func (*EIncompatibleTypesStorageImpl) Error added in v0.15.0

type EIncompatibleTypesStorageKey added in v0.15.0

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

EIncompatibleTypesStorageKey error occurs if type of key map is incompatible to provided implementation

func (*EIncompatibleTypesStorageKey) Error added in v0.15.0

type EIncompatibleTypesStorageValue added in v0.15.0

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

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

func (*EIncompatibleTypesStorageValue) Error added in v0.15.0

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 or non-ptr) requirement is declared

func (*ENonAssignableRequirement) Error

func (e *ENonAssignableRequirement) Error() string

type EPackageNotUsed added in v0.10.0

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

EPackageNotUsed s.e.

func (*EPackageNotUsed) Error added in v0.10.0

func (e *EPackageNotUsed) Error() string

type EProvisionForNonAssignable added in v0.17.0

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

EProvisionForNonAssignable occurs if unassgnable var is provided on Provide() call (e.g. var of func type instead of ptr to var of func type)

func (*EProvisionForNonAssignable) Error added in v0.17.0

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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