cell

package
v1.13.5 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: Apache-2.0 Imports: 17 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllSettings

type AllSettings map[string]any

type Cell

type Cell interface {
	// Info provides a structural summary of the cell for printing purposes.
	Info(container) Info

	// Apply the cell to the dependency graph container.
	Apply(container) error
}

Cell is the modular building block of the hive.

A cell can be constructed with:

  • Module(): Create a named set of cells.
  • Provide(): Provide object constructors.
  • Invoke(): Invoke a function to instantiate objects.
  • Decorate(): Decorate a set of cells to augment an object.
  • Config(): Cell providing a configuration struct.

func Config

func Config[Cfg Flagger](def Cfg) Cell

Config constructs a new config cell.

The configuration struct `T` needs to implement the Flags method that registers the flags. The structure is populated and provided via dependency injection by Hive.Run(). The underlying mechanism for populating the struct is viper's Unmarshal().

func Decorate

func Decorate(dtor any, cells ...Cell) Cell

Decorate takes a decorator function and a set of cells and returns a decorator cell.

A decorator function is a function that takes as arguments objects in the hive and returns one or more augmented objects. The cells wrapped with a decorator will be provided the returned augmented objects.

Example:

cell.Decorate(
	func(e Example) Example {
		return e.WithMoreMagic()
	},
	cell.Invoke(func(e Example) {
		// e now has more magic
	},
)

func Invoke

func Invoke(funcs ...any) Cell

Invoke constructs a cell for invoke functions. The invoke functions are executed when the hive is started to instantiate all objects via the constructors.

func Module

func Module(id, title string, cells ...Cell) Cell

Module creates a scoped set of cells with a given identifier.

The id and title will be included in the object dump (hive.PrintObjects). The id must be lower-case, at most 30 characters and only contain [a-z0-9-_]. Title can contain [a-zA-Z0-9_- ] and must be shorter than 80 characters.

Private constructors with a module (ProvidePrivate) are only accessible within this module and its sub-modules.

func Provide

func Provide(ctors ...any) Cell

Provide constructs a new named cell with the given name and constructors. Constructor is any function that takes zero or more parameters and returns one or more values and optionally an error. For example, the following forms are accepted:

func() A
func(A, B, C) (D, error).

If the constructor depends on a type that is not provided by any constructor the hive will fail to run with an error pointing at the missing type.

A constructor can also take as parameter a structure of parameters annotated with `cell.In`, or return a struct annotated with `cell.Out`:

type params struct {
	cell.In
	Flower *Flower
	Sun *Sun
}

type out struct {
	cell.Out
	Honey *Honey
	Nectar *Nectar
}

func newBee(params) (out, error)

func ProvidePrivate

func ProvidePrivate(ctors ...any) Cell

ProvidePrivate is like Private, but the constructed objects are only available within the module it is defined and nested modules.

type Flagger

type Flagger interface {
	// Flags registers the configuration options as command-line flags.
	//
	// By convention a flag name matches the field name
	// if they're the same under case-insensitive comparison when dashes are
	// removed. E.g. "my-config-flag" matches field "MyConfigFlag". The
	// correspondence to the flag can be also specified with the mapstructure
	// tag: MyConfigFlag `mapstructure:"my-config-flag"`.
	//
	// Exported fields that are not found from the viper settings will cause
	// hive.Run() to fail. Unexported fields are ignored.
	//
	// See https://pkg.go.dev/github.com/mitchellh/mapstructure for more info.
	Flags(*pflag.FlagSet)
}

Flagger is implemented by configuration structs to provide configuration for a cell.

type In

type In = dig.In

In when embedded into a struct used as constructor parameter makes the exported values of that struct become dependency injected values. In other words, it allows moving a long list of constructor parameters into a struct.

Struct fields can be annotated with `optional:"true"` to make the dependency optional. If the type is not found in the dependency graph, the value is set to the zero value.

See https://pkg.go.dev/go.uber.org/dig#In for more information.

type Info

type Info interface {
	Print(indent int, w *InfoPrinter)
}

Info provides a simple way of printing cells hierarchically in textual form.

type InfoLeaf

type InfoLeaf string

func (InfoLeaf) Print

func (l InfoLeaf) Print(indent int, w *InfoPrinter)

type InfoNode

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

func NewInfoNode

func NewInfoNode(header string) *InfoNode

func (*InfoNode) Add

func (n *InfoNode) Add(child Info)

func (*InfoNode) AddLeaf

func (n *InfoNode) AddLeaf(format string, args ...any)

func (*InfoNode) Print

func (n *InfoNode) Print(indent int, w *InfoPrinter)

type InfoPrinter

type InfoPrinter struct {
	io.Writer
	// contains filtered or unexported fields
}

func NewInfoPrinter

func NewInfoPrinter() *InfoPrinter

type InfoStruct

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

func (*InfoStruct) Print

func (n *InfoStruct) Print(indent int, w *InfoPrinter)

type InvokerList

type InvokerList interface {
	AppendInvoke(func() error)
}

type Out

type Out = dig.Out

Out when embedded into a struct that is returned by a constructor will make the values in the struct become objects in the dependency graph instead of the struct itself.

See https://pkg.go.dev/go.uber.org/dig#Out for more information.

Jump to

Keyboard shortcuts

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