wire

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: Apache-2.0 Imports: 21 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

This section is empty.

Types

type Field added in v0.3.0

type Field struct {
	// Parent is the struct or pointer to the struct that the field belongs to.
	Parent types.Type
	// Name is the field name.
	Name string
	// Pkg is the package that the struct resides in.
	Pkg *types.Package
	// Pos is the source position of the field declaration.
	// defining these fields.
	Pos token.Pos
	// Out is the field's provided types. The first element provides the
	// field type. If the field is coming from a pointer to a struct,
	// there will be a second element providing a pointer to the field.
	Out []types.Type
}

Field describes a specific field selected from a struct.

type GenerateOptions added in v0.3.0

type GenerateOptions struct {
	// Header will be inserted at the start of each generated file.
	Header           []byte
	PrefixOutputFile string
	Tags             string
}

GenerateOptions holds options for Generate.

type GenerateResult

type GenerateResult struct {
	// PkgPath is the package's PkgPath.
	PkgPath string
	// OutputPath is the path where the generated output should be written.
	// May be empty if there were errors.
	OutputPath string
	// Content is the gofmt'd source code that was generated. May be nil if
	// there were errors during generation.
	Content []byte
	// Errs is a slice of errors identified during generation.
	Errs []error
}

GenerateResult stores the result for a package from a call to Generate.

func Generate

func Generate(ctx context.Context, wd string, env []string, patterns []string, opts *GenerateOptions) ([]GenerateResult, []error)

Generate performs dependency injection for the packages that match the given patterns, return a GenerateResult for each package. The package pattern is defined by the underlying build system. For the go tool, this is described at https://golang.org/cmd/go/#hdr-Package_lists_and_patterns

wd is the working directory and env is the set of environment variables to use when loading the package specified by pkgPattern. If env is nil or empty, it is interpreted as an empty set of variables. In case of duplicate environment variables, the last one in the list takes precedence.

Generate may return one or more errors if it failed to load the packages.

func (GenerateResult) Commit

func (gen GenerateResult) Commit() error

Commit writes the generated file to disk.

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(ctx context.Context, wd string, env []string, tags string, patterns []string) (*Info, []error)

Load finds all the provider sets in the packages that match the given patterns, as well as the provider sets' transitive dependencies. It may return both errors and Info. The patterns are defined by the underlying build system. For the go tool, this is described at https://golang.org/cmd/go/#hdr-Package_lists_and_patterns

wd is the working directory and env is the set of environment variables to use when loading the packages specified by patterns. If env is nil or empty, it is interpreted as an empty set of variables. In case of duplicate environment variables, the last one in the list takes precedence.

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 InjectorArg

type InjectorArg struct {
	// Args is the full set of arguments.
	Args *InjectorArgs
	// Index is the index into Args.Tuple for this argument.
	Index int
}

InjectorArg describes a specific argument passed to an injector function.

type InjectorArgs

type InjectorArgs struct {
	// Name is the name of the injector function.
	Name string
	// Tuple represents the arguments.
	Tuple *types.Tuple
	// Pos is the source position of the injector function.
	Pos token.Pos
}

InjectorArgs describes the arguments passed to an injector function.

type ProvidedType

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

ProvidedType represents a type provided from a source. The source can be a *Provider (a provider function), a *Value (wire.Value), or an *InjectorArgs (arguments to the injector function). The zero value has none of the above, and returns true for IsNil.

func (ProvidedType) Arg

func (pt ProvidedType) Arg() *InjectorArg

Arg returns pt as an *InjectorArg representing an injector argument. It panics if pt does not point to an arg.

func (ProvidedType) Field added in v0.3.0

func (pt ProvidedType) Field() *Field

Field returns pt as a Field pointer. It panics if pt does not point to a struct Field.

func (ProvidedType) IsArg

func (pt ProvidedType) IsArg() bool

IsArg reports whether pt points to an injector argument.

func (ProvidedType) IsField added in v0.3.0

func (pt ProvidedType) IsField() bool

IsField reports whether pt points to a Fields.

func (ProvidedType) IsNil

func (pt ProvidedType) IsNil() bool

IsNil reports whether pt is the zero value.

func (ProvidedType) IsProvider

func (pt ProvidedType) IsProvider() bool

IsProvider reports whether pt points to a Provider.

func (ProvidedType) IsValue

func (pt ProvidedType) IsValue() bool

IsValue reports whether pt points to a Value.

func (ProvidedType) Provider

func (pt ProvidedType) Provider() *Provider

Provider returns pt as a Provider pointer. It panics if pt does not point to a Provider.

func (ProvidedType) Type added in v0.2.1

func (pt ProvidedType) Type() types.Type

Type returns the output type.

  • For a function provider, this is the first return value type.
  • For a struct provider, this is either the struct type or the pointer type whose element type is the struct type.
  • For a value, this is the type of the expression.
  • For an argument, this is the type of the argument.

func (ProvidedType) Value

func (pt ProvidedType) Value() *Value

Value returns pt as a Value pointer. It panics if pt does not point to a Value.

type Provider

type Provider struct {
	// Pkg is the package that the Go object resides in.
	Pkg *types.Package

	// 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

	// Varargs is true if the provider function is variadic.
	Varargs bool

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

	// 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

	// If the provider is a struct, FieldName will be the field name to set.
	FieldName string
}

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
	Fields    []*Field
	Imports   []*ProviderSet
	// InjectorArgs is only filled in for wire.Build.
	InjectorArgs *InjectorArgs
	// 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