factory

package
v0.0.0-...-f97d723 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeHook

func DecodeHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error)

DecodeHook is a mapstructure.DecodeHook that serializes the given data into a InputConfig with a name and a Valuable object. mapstructure cannot nested objects, so we need to serialize the data into a map[string]interface{} and then deserialize it into a InputConfig.

@see https://pkg.go.dev/github.com/mitchellh/mapstructure#DecodeHookFunc for more details.

func Register

func Register(factory IFactory) error

Register a new factory in the factory map with the built-in factory name

Types

type Factory

type Factory struct {

	// Name is the name of the factory function
	Name string
	// ID is the unique ID of the factory
	ID string
	// Fn is the factory function
	Fn RunFunc

	// Config is the configuration for the factory function
	Config map[string]interface{}
	// Inputs is the inputs of the factory
	Inputs []*Var
	// Outputs is the outputs of the factory
	Outputs []*Var
	// contains filtered or unexported fields
}

Factory represents a factory that can be executed by the pipeline.

func GetFactoryByName

func GetFactoryByName(name string) (*Factory, bool)

GetFactoryByName returns true if the function name is contained in the map

func (*Factory) DeepCopy

func (f *Factory) DeepCopy() *Factory

DeepCopy creates a deep copy of the pipeline.

func (*Factory) Identifier

func (f *Factory) Identifier() string

Identifier will return the id of the factory or the name of the factory if the id is not set.

func (*Factory) Input

func (f *Factory) Input(name string) (v *Var, ok bool)

Input retrieve the input variable of the given name. @param name the name of the input variable @return the input variable of the given name @return true if the input variable was found

func (*Factory) Output

func (f *Factory) Output(name string, value interface{}) *Factory

Output store the output variable of the given name. @param name the name of the output variable @param value the value of the output variable @return the factory

func (*Factory) Run

func (f *Factory) Run() error

Run executes the factory function

func (*Factory) WithConfig

func (f *Factory) WithConfig(config map[string]interface{}) *Factory

WithConfig sets the config of the factory. @param config the config of the factory @return the factory

func (*Factory) WithID

func (f *Factory) WithID(id string) *Factory

WithID sets the id of the factory. @param id the id of the factory @return the factory

func (*Factory) WithInput

func (f *Factory) WithInput(name string, value interface{}) *Factory

WithInput adds the given input to the factory. @param name the name of the input variable @param value the value of the input variable @return the factory

type IFactory

type IFactory interface {
	// Name is the name of the factory function
	// The name must be unique in the registry
	// @return the name of the factory function
	Name() string
	// DefinedInputs returns the wanted inputs of the factory used
	// by the function during the execution of the pipeline
	DefinedInpus() []*Var
	// DefinedOutputs returns the wanted outputs of the factory used
	// by the function during the execution of the pipeline
	DefinedOutputs() []*Var
	// Func is used to build the factory function
	// @return the factory function
	Func() RunFunc
}

IFactory is an interface that represents a factory.

type InputConfig

type InputConfig struct {
	valuable.Valuable
	Name string `mapstructure:"name"`
}

InputConfig is a struct that contains the name and the value of an input. It is used to store the inputs of a factory. The name is used to retrieve the value of the input from the factory.

This is used to load the inputs of a factory from the configuration file.

type Pipeline

type Pipeline struct {
	WantedResult interface{}
	LastResults  []interface{}

	Inputs map[string]interface{}

	Outputs map[string]map[string]interface{}
	// contains filtered or unexported fields
}

Pipeline is a struct that contains informations about the pipeline. It is used to store the inputs and outputs of all factories executed by the pipeline and secure the result of the pipeline.

func NewPipeline

func NewPipeline() *Pipeline

NewPipeline initializes a new pipeline

func (*Pipeline) AddFactory

func (p *Pipeline) AddFactory(f *Factory) *Pipeline

AddFactory adds a new factory to the pipeline. New Factory is added to the end of the pipeline.

func (*Pipeline) CheckResult

func (p *Pipeline) CheckResult() bool

CheckResult checks if the pipeline result is the same as the wanted result. type and value of the result must be the same as the last result

func (*Pipeline) DeepCopy

func (p *Pipeline) DeepCopy() *Pipeline

DeepCopy creates a deep copy of the pipeline.

func (*Pipeline) FactoryCount

func (p *Pipeline) FactoryCount() int

FactoryCount returns the number of factories in the pipeline.

func (*Pipeline) HasFactories

func (p *Pipeline) HasFactories() bool

HasFactories returns true if the pipeline has at least one factory.

func (*Pipeline) Run

func (p *Pipeline) Run() *Factory

Run executes the pipeline. Factories are executed in the order they were added to the pipeline. The last factory is returned

@return the last factory

func (*Pipeline) WantResult

func (p *Pipeline) WantResult(result interface{}) *Pipeline

WantResult sets the wanted result of the pipeline. the result is compared to the last result of the pipeline. type and value of the result must be the same as the last result

func (*Pipeline) WithInput

func (p *Pipeline) WithInput(name string, value interface{}) *Pipeline

WithInput adds a new input to the pipeline. The input is added safely to prevent concurrent map writes error.

type RunFunc

type RunFunc func(factory *Factory, configRaw map[string]interface{}) error

RunFunc is a function that is used to run a factory. It is used to run a factory in a pipeline. @param factory the factory to run @param configRaw the raw configuration of the factory

type Var

type Var struct {
	// Internal is to specify if the variable is an internal provided variable
	Internal bool
	// Type is the type of the wanted variable
	Type reflect.Type
	// Name is the name of the variable
	Name string
	// Value is the value of the variable, type can be retrieved from Type field
	Value interface{}
}

Var is a struct that contains the name and the value of an input or output. It is used to store the inputs and outputs of a factory.

func GetVar

func GetVar(list []*Var, name string) (*Var, bool)

GetVar returns the variable with the given name from the given slice. @param list the Var slice to search in @param name the name of the variable to search for @return the variable with the given name from the given slice @return true if the variable was found

Jump to

Keyboard shortcuts

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