gen

package
v0.0.0-...-6d43c30 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package gen contains the logic for generating the source code

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FactoryName

func FactoryName(typeName *types.Named) string

FactoryName returns the name of the factory function for the given name

func ProviderName

func ProviderName(typeName *types.Named) string

ProviderName returns the name of the provider function for the given name

func SanitizeName

func SanitizeName(name *types.Named) string

SanitizeName returns a name that can be used as a Go identifier from the given name. The generated identifier should not clash with any other types.Named, unless the underlying named object is the same

Types

type Assignment

type Assignment interface {
	// CastTo returns the type this assignment should be cast to. If nil is returned, then
	// the assignment is not cast to any type.
	CastTo() *types.Named

	// GetSourceAssignment returns the assignment as a string of source code.
	GetSourceAssignment() string
}

Assignment represents a way of getting a injected value, either by a provider or by an injectable factory method

func AssignmentForFieldType

func AssignmentForFieldType(
	componentReceiverName string,
	rawFieldType types.Type,
	providers map[string]resolver.ResolvedType,
	bindings map[string]*types.Named,
) (Assignment, error)

AssignmentForFieldType returns an assignment for the given field type

func NewFactoryAssignment

func NewFactoryAssignment(
	componentReceiverName string,
	typeName *types.Named,
) Assignment

NewFactoryAssignment returns a factory-method based assignment

func NewProviderAssignment

func NewProviderAssignment(
	componentReceiverName string,
	typeName *types.Named,
	castTo *types.Named,
) Assignment

NewProviderAssignment returns a component provided assignment

type GeneratedComponent

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

GeneratedComponent is the resolve of GenerateComponent and contains helper methods for converting this directly into source

func NewGeneratedComponent

func NewGeneratedComponent(
	componentName string,
	targets []*resolver.InjectionTarget,
	providers map[string]resolver.ResolvedType,
	bindings map[string]*types.Named,
) (*GeneratedComponent, error)

NewGeneratedComponent generates the source for the given component

func (*GeneratedComponent) ToSource

func (g *GeneratedComponent) ToSource(componentPackage string) map[string]string

ToSource returns a map of file names to file contents that represent the generated source of this component

type GeneratedFactory

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

GeneratedFactory contains information for generating a factory for an injected struct

The generated code from this factory looks something like this:

func TargetFactory(component *GeneratedComponent) *TargetType {
    target := &TargetType{}
    targetType.ProvidedType = component.provides_ProvidedType()
    targetType.InjectableType = InjectableFactory(component)
    return target
}

func NewGeneratedFactoryIfNeeded

func NewGeneratedFactoryIfNeeded(
	generatedComponentType string,
	generatedComponentReceiver string,
	targetName *types.Named,
	targetStruct *types.Struct,
	providers map[string]resolver.ResolvedType,
	bindings map[string]*types.Named,
) (*GeneratedFactory, error)

NewGeneratedFactoryIfNeeded generates a factory for the given struct. If a factory cannot be generated, e.g. if the struct is not injectable, nil is returned

func (*GeneratedFactory) ToSource

func (g *GeneratedFactory) ToSource(componentPackage string) string

ToSource converts this generated factory into Go source code. The source should be treated as a separate source file in the generated component package

type GeneratedModuleProvider

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

GeneratedModuleProvider is a single generated provider method on the component from a module source

func NewGeneratedProvider

func NewGeneratedProvider(
	generatedComponentType string,
	generatedComponentReceiver string,
	resolvedType *resolver.ModuleResolvedType,
	providers map[string]resolver.ResolvedType,
	bindings map[string]*types.Named,
) (*GeneratedModuleProvider, error)

NewGeneratedProvider generates a provider function for the given resolved type The generated function has the form:

func (generatedComponent *GeneratedComponent) provides_Name() *SomeType {
    return someModule.providerFunc(
	       component.provides_ProvidedType(),
        InjectableFactory(component),
    )
}

func (*GeneratedModuleProvider) ToSource

func (g *GeneratedModuleProvider) ToSource(componentPackage string) string

ToSource returns the source code for this provider.

Jump to

Keyboard shortcuts

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