wire

package
v0.0.0-...-da76615 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package wire provides compile-time dependency injection logic as a Go library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(bctx *build.Context, wd string, pkg string) ([]byte, []error)

Generate performs dependency injection for a single package, returning the gofmt'd Go source code.

Types

type IfaceBinding

type IfaceBinding struct {
	// Iface is the interface type, which is what can be injected.
	Iface types.Type

	// Provided is always a type that is assignable to Iface.
	Provided types.Type

	// Pos is the position where the binding was declared.
	Pos token.Pos
}

An IfaceBinding declares that a type should be used to satisfy inputs of the given interface type.

type Info

type Info struct {
	Fset *token.FileSet

	// Sets contains all the provider sets in the initial packages.
	Sets map[ProviderSetID]*ProviderSet

	// Injectors contains all the injector functions in the initial packages.
	// The order is undefined.
	Injectors []*Injector
}

Info holds the result of Load.

func Load

func Load(bctx *build.Context, wd string, pkgs []string) (*Info, []error)

Load finds all the provider sets in the given packages, as well as the provider sets' transitive dependencies. It may return both errors and Info.

type Injector

type Injector struct {
	ImportPath string
	FuncName   string
}

An Injector describes an injector function.

func (*Injector) String

func (in *Injector) String() string

String returns the injector name as ""path/to/pkg".Foo".

type ProvidedType

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

ProvidedType is a pointer to a Provider or a Value. The zero value is a nil pointer. It also holds the concrete type that the Provider or Value provided.

func (ProvidedType) ConcreteType

func (pv ProvidedType) ConcreteType() types.Type

ConcreteType returns the concrete type that was provided.

func (ProvidedType) IsNil

func (pv ProvidedType) IsNil() bool

IsNil reports whether pv is the zero value.

func (ProvidedType) IsProvider

func (pv ProvidedType) IsProvider() bool

IsProvider reports whether pv points to a Provider.

func (ProvidedType) IsValue

func (pv ProvidedType) IsValue() bool

IsValue reports whether pv points to a Value.

func (ProvidedType) Provider

func (pv ProvidedType) Provider() *Provider

Provider returns pv as a Provider pointer. It panics if pv points to a Value.

func (ProvidedType) Value

func (pv ProvidedType) Value() *Value

Value returns pv as a Value pointer. It panics if pv points to a Provider.

type Provider

type Provider struct {
	// ImportPath is the package path that the Go object resides in.
	ImportPath string

	// Name is the name of the Go object.
	Name string

	// Pos is the source position of the func keyword or type spec
	// defining this provider.
	Pos token.Pos

	// Args is the list of data dependencies this provider has.
	Args []ProviderInput

	// IsStruct is true if this provider is a named struct type.
	// Otherwise it's a function.
	IsStruct bool

	// Fields lists the field names to populate. This will map 1:1 with
	// elements in Args.
	Fields []string

	// Out is the set of types this provider produces. It will always
	// contain at least one type.
	Out []types.Type

	// HasCleanup reports whether the provider function returns a cleanup
	// function.  (Always false for structs.)
	HasCleanup bool

	// HasErr reports whether the provider function can return an error.
	// (Always false for structs.)
	HasErr bool
}

Provider records the signature of a provider. A provider is a single Go object, either a function or a named struct type.

type ProviderInput

type ProviderInput struct {
	Type types.Type
}

ProviderInput describes an incoming edge in the provider graph.

type ProviderSet

type ProviderSet struct {
	// Pos is the position of the call to wire.NewSet or wire.Build that
	// created the set.
	Pos token.Pos
	// PkgPath is the import path of the package that declared this set.
	PkgPath string
	// VarName is the variable name of the set, if it came from a package
	// variable.
	VarName string

	Providers []*Provider
	Bindings  []*IfaceBinding
	Values    []*Value
	Imports   []*ProviderSet
	// contains filtered or unexported fields
}

A ProviderSet describes a set of providers. The zero value is an empty ProviderSet.

func (*ProviderSet) For

func (set *ProviderSet) For(t types.Type) ProvidedType

For returns a ProvidedType for the given type, or the zero ProvidedType.

func (*ProviderSet) Outputs

func (set *ProviderSet) Outputs() []types.Type

Outputs returns a new slice containing the set of possible types the provider set can produce. The order is unspecified.

type ProviderSetID

type ProviderSetID struct {
	ImportPath string
	VarName    string
}

A ProviderSetID identifies a named provider set.

func (ProviderSetID) String

func (id ProviderSetID) String() string

String returns the ID as ""path/to/pkg".Foo".

type Value

type Value struct {
	// Pos is the source position of the expression defining this value.
	Pos token.Pos

	// Out is the type this value produces.
	Out types.Type
	// contains filtered or unexported fields
}

Value describes a value expression.

Jump to

Keyboard shortcuts

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