factory

package
v0.11.4 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package factory contains a "factory" pattern based on argmapper.

A Factory can be used to register factory methods to create some predefined type or interface implementation. These functions are argmapper functions so converters and so on can be used as part of instantiation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

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

Factory keeps track of named dependency-injected factory functions to create an implementation of an interface.

func New

func New(iface interface{}) (*Factory, error)

New creates a Factory for the interface iface. The parameter iface should be a nil pointer to the interface type. Example: (*iface)(nil).

func (*Factory) Copy

func (f *Factory) Copy() *Factory

Copy returns a copy of Factory. Any registrations on the copy will not reflect the original and vice versa.

func (*Factory) Func

func (f *Factory) Func(name string) *argmapper.Func

Func returns the factory function named name. This can then be used to call and instantiate the factory interface type.

func (*Factory) Register

func (f *Factory) Register(name string, fn interface{}) error

Register registers a factory function named name for the interface.

This will error if the function given doesn't result in a single non-error output that implements the interface registered with this factory. The function return signature can be: (T) or (T, error) where T implements the interface type for this factory.

T is allowed to be a literal interface{} type. In this case, it is the callers responsibility to ensure the result is the proper type.

fn may take any number and types of inputs. It is the callers responsibilty when using Func and Call to pass in the required parameters.

func (*Factory) Registered

func (f *Factory) Registered() []string

Registered returns the names registered with this factory.

Jump to

Keyboard shortcuts

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