di

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoMatch = errors.New("no match")

Functions

This section is empty.

Types

type Aliases

type Aliases map[Dependency]Dependency

type Declaration

type Declaration interface {
	ID() string
	Dependencies() []Dependency
	Generate(gen Generator, inputs []*Variable) (outputs []*Variable)
}

type Dependency

type Dependency interface {
	ID() string
	ImportPath() string
	TypeName() string
	Find(Finder) (Declaration, error)
}

type Error

type Error struct {
}

Error type

func (*Error) Dependencies

func (*Error) Dependencies() (deps []Dependency)

func (*Error) Find

func (e *Error) Find(Finder) (Declaration, error)

func (*Error) Generate

func (*Error) Generate(gen Generator, inputs []*Variable) (outputs []*Variable)

func (*Error) ID

func (*Error) ID() string

func (*Error) ImportPath

func (*Error) ImportPath() string

func (*Error) TypeName

func (*Error) TypeName() string

type External

type External struct {
	Variable *Variable
	Key      string // Name to be used as a key in a struct
	Hoisted  bool   // True if this external was hoisted up
	FullType string // Type name including package name
}

type Finder

type Finder interface {
	Find(module *gomod.Module, dep Dependency) (Declaration, error)
}

Finder finds a declaration that will instantiate the data type

type Function

type Function struct {
	// Name of the function to generate
	Name string
	// Imports to pass through
	Imports *imports.Set
	// Params are the external parameters that are passed in
	Params []*Param
	// Results are the dependencies that need to be loaded
	Results []Dependency
	// Hoist dependencies that don't depend on externals, turning them into
	// externals. This is to avoid initializing these inner deps every time.
	// Useful for per-request dependency injection.
	Hoist bool
	// Aliases allow you to map one dependency to another. Useful to supporting
	// interfaces as inputs that are mapped to a concrete value.
	Aliases Aliases
	// Target import path where this function will be generated to
	Target string
}

Function is the top-level load function that we generate to provide all the dependencies

func (*Function) Dependencies

func (fn *Function) Dependencies() []Dependency

func (*Function) Generate

func (fn *Function) Generate(g Generator, ins []*Variable) (outs []*Variable)

Generate the function declaration that can be called to initialize the required dependencies

func (*Function) ID

func (fn *Function) ID() string

func (*Function) Signature added in v0.1.9

func (fn *Function) Signature() string

func (*Function) Validate

func (fn *Function) Validate() error

type Generator

type Generator interface {
	WriteString(code string) (n int, err error)
	Identifier(importPath, name string) string
	Variable(importPath, name string) string
	MarkError(hasError bool)
}

type Injector

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

func New

func New(fsys fs.FS, log log.Log, module *gomod.Module, parser *parser.Parser) *Injector

func (*Injector) Find

func (i *Injector) Find(currModule *gomod.Module, dep Dependency) (Declaration, error)

func (*Injector) Generate

func (i *Injector) Generate(fn *Function) (string, error)

GenerateFile generates a provider function into string

func (*Injector) GenerateFile

func (i *Injector) GenerateFile(fn *Function) (string, error)

GenerateFile generates a provider function into a Go file string

func (*Injector) Load

func (i *Injector) Load(fn *Function) (*Node, error)

Load the dependency graph, but don't generate any code. Load is intentionally low-level and used by higher-level APIs like Generate.

func (*Injector) Wire

func (i *Injector) Wire(fn *Function) (*Provider, error)

Wire up the provider function into provider state. The Provider has some helper functions that are useful when passed into a template.

type Node

type Node struct {
	// Type and import of the dependency we were looking for
	Import string
	Type   string

	// Declaration that would instantiate this type. This will be nil if the node
	// is External
	Declaration Declaration
	// Dependencies that the declaration relies on to be able to instantiate
	Dependencies []*Node
	// External is true if the type matches an external dependency. External types
	// are passed in, not instantiated.
	External bool
	// Hoisted is true if the dependency has been hoisted up. Hoisted types are
	// passed in, not instantiated.
	Hoist bool
}

node in the dependency injection graph

func Hoist

func Hoist(root *Node) *Node

Hoist the nodes that don't depend on the external nodes and turn these nodes into external nodes. This allows for dependencies that don't depend on externals to be initialized once, rather than each time the generated function is called.

Start with hoisting true, but if we encounter any external along the way, the hoisting of all children becomes false

func (*Node) Generate

func (n *Node) Generate(imports *imports.Set, fnName, target string) *Provider

Build a provider for the target import path

func (*Node) ID

func (n *Node) ID() string

func (*Node) Print

func (node *Node) Print() string

type Param added in v0.2.1

type Param struct {
	Import string
	Type   string
	Hoist  bool
}

func (*Param) ID added in v0.2.1

func (p *Param) ID() string

type Params

type Params []string

func (Params) String

func (params Params) String() string

type Provider

type Provider struct {
	Name      string            // Name of the function
	Target    string            // Target import path
	Imports   []*imports.Import // Imports needed
	Externals []*External       // External variables
	Code      string            // Body of the generated code
	Results   []*Variable       // Return variables
	// contains filtered or unexported fields
}

Provider is the result of generating. Provider can generate functions or files or be used for it's template variables.

func (*Provider) File

func (p *Provider) File() string

File wraps the body code in a file

func (*Provider) Function

func (p *Provider) Function() string

Function wraps the body code in a function

func (*Provider) Hoisted added in v0.2.0

func (p *Provider) Hoisted() (externals []*External)

Hoisted returns a list of hoisted externals

func (*Provider) Params

func (p *Provider) Params() (params Params)

func (*Provider) Variable

func (p *Provider) Variable(importType string) string

Variable returns the variable name of an external The importType key is importPath.dataType

func (*Provider) Variables

func (p *Provider) Variables(importTypes ...string) (vars varList)

Variables returns a list of external variable names The importType key is importPath.dataType

type Struct

type Struct struct {
	Import string
	Type   string
	Fields []*StructField
}

Struct is a dependency that can be defined in memory. Struct is also a declaration that can be referenced and be used to generate initializers.

func (*Struct) Dependencies

func (s *Struct) Dependencies() (deps []Dependency)

func (*Struct) Find

func (s *Struct) Find(finder Finder) (Declaration, error)

Find a declaration that provides this type

func (*Struct) Generate

func (s *Struct) Generate(gen Generator, inputs []*Variable) (outputs []*Variable)

func (*Struct) ID

func (s *Struct) ID() string

func (*Struct) ImportPath

func (s *Struct) ImportPath() string

func (*Struct) TypeName

func (s *Struct) TypeName() string

type StructField

type StructField struct {
	Name   string
	Import string
	Type   string
	// contains filtered or unexported fields
}

func (*StructField) Find

func (s *StructField) Find(finder Finder) (Declaration, error)

func (*StructField) ID

func (s *StructField) ID() string

func (*StructField) ImportPath

func (s *StructField) ImportPath() string

func (*StructField) TypeName

func (s *StructField) TypeName() string

type Type

type Type struct {
	Import string
	Type   string
	// contains filtered or unexported fields
}

func ToType

func ToType(importPath, dataType string) *Type

func (*Type) Find

func (t *Type) Find(finder Finder) (Declaration, error)

Find a declaration that provides this type

func (*Type) ID

func (t *Type) ID() string

func (*Type) ImportPath

func (t *Type) ImportPath() string

func (*Type) TypeName

func (t *Type) TypeName() string

type Variable

type Variable struct {
	Import string      // Import path
	Type   string      // Type of the variable
	Name   string      // Name of the variable
	Kind   parser.Kind // Kind of type (struct, interface, etc.)
}

func (*Variable) ID

func (v *Variable) ID() string

Jump to

Keyboard shortcuts

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