pkg

package
v2.30.2 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: BSD-3-Clause Imports: 23 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInfiniteLoop = fmt.Errorf("infintie loop in template variables detected")
View Source
var ErrNotInterface = errors.New("expression not an interface")

ErrNotInterface is returned when the given type is not an interface type.

View Source
var ErrNotSetup = errors.New("not setup")

ErrNotSetup is returned when the generator is not configured.

Functions

func DetermineOutputPackageName added in v2.21.0

func DetermineOutputPackageName(
	interfaceFileName string,
	interfacePackageName string,
	packageNamePrefix string,
	packageName string,
	keepTree bool,
	inPackage bool,
) string

Types

type Cleanup

type Cleanup func() error

type FileOutputStreamProvider

type FileOutputStreamProvider struct {
	Config                    config.Config
	BaseDir                   string
	InPackage                 bool
	InPackageSuffix           bool
	TestOnly                  bool
	Case                      string
	KeepTree                  bool
	KeepTreeOriginalDirectory string
	FileName                  string
}

func (*FileOutputStreamProvider) GetWriter

func (p *FileOutputStreamProvider) GetWriter(ctx context.Context, iface *Interface) (io.Writer, error, Cleanup)

type Generator

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

Generator is responsible for generating the string containing imports and the mock struct that will later be written out as file.

func NewGenerator

func NewGenerator(ctx context.Context, c GeneratorConfig, iface *Interface, pkg string) *Generator

NewGenerator builds a Generator.

func (*Generator) Generate

func (g *Generator) Generate(ctx context.Context) error

Generate builds a string that constitutes a valid go source file containing the mock of the relevant interface.

func (*Generator) GenerateAll added in v2.21.0

func (g *Generator) GenerateAll(ctx context.Context) error

func (*Generator) GenerateBoilerplate added in v2.5.0

func (g *Generator) GenerateBoilerplate(boilerplate string)

GenerateBoilerplate adds a boilerplate text. It should be called before any other generator methods to ensure the text is on top.

func (*Generator) GeneratePrologue

func (g *Generator) GeneratePrologue(ctx context.Context, pkg string)

GeneratePrologue generates the prologue of the mock.

func (*Generator) GeneratePrologueNote

func (g *Generator) GeneratePrologueNote(note string)

GeneratePrologueNote adds a note after the prologue to the output string.

func (*Generator) Write

func (g *Generator) Write(w io.Writer) error

type GeneratorConfig added in v2.21.0

type GeneratorConfig struct {
	Boilerplate          string
	DisableVersionString bool
	Exported             bool
	InPackage            bool
	KeepTree             bool
	Note                 string
	PackageName          string
	PackageNamePrefix    string
	StructName           string
	UnrollVariadic       bool
	WithExpecter         bool
	ReplaceType          []string
}

type GeneratorVisitor

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

func NewGeneratorVisitor added in v2.21.0

func NewGeneratorVisitor(
	config GeneratorVisitorConfig,
	outputStream OutputStreamProvider,
	dryRun bool,
) *GeneratorVisitor

func (*GeneratorVisitor) VisitWalk

func (v *GeneratorVisitor) VisitWalk(ctx context.Context, iface *Interface) error

type GeneratorVisitorConfig added in v2.21.0

type GeneratorVisitorConfig struct {
	Boilerplate          string
	DisableVersionString bool
	Exported             bool
	InPackage            bool
	KeepTree             bool
	Note                 string
	// The name of the output package, if InPackage is false (defaults to "mocks")
	PackageName       string
	PackageNamePrefix string
	StructName        string
	UnrollVariadic    bool
	WithExpecter      bool
	ReplaceType       []string
}

type Interface

type Interface struct {
	Name            string // Name of the type to be mocked.
	QualifiedName   string // Path to the package of the target type.
	FileName        string
	File            *ast.File
	Pkg             TypesPackage
	NamedType       *types.Named
	IsFunction      bool             // If true, this instance represents a function, otherwise it's an interface.
	ActualInterface *types.Interface // Holds the actual interface type, in case it's an interface.
	SingleFunction  *Method          // Holds the function type information, in case it's a function type.
}

Interface type represents the target type that we will generate a mock for. It could be an interface, or a function type. Function type emulates: an interface it has 1 method with the function signature and a general name, e.g. "Execute".

func (*Interface) Methods added in v2.1.0

func (iface *Interface) Methods() []*Method

type Method added in v2.1.0

type Method struct {
	Name      string
	Signature *types.Signature
}

type NodeVisitor

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

func NewNodeVisitor

func NewNodeVisitor() *NodeVisitor

func (*NodeVisitor) DeclaredInterfaces

func (nv *NodeVisitor) DeclaredInterfaces() []string

func (*NodeVisitor) Visit

func (nv *NodeVisitor) Visit(node ast.Node) ast.Visitor

type OutputStreamProvider

type OutputStreamProvider interface {
	GetWriter(context.Context, *Interface) (io.Writer, error, Cleanup)
}

type Outputter added in v2.21.0

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

Outputter wraps the Generator struct. It calls the generator to create the mock implementations in-memory, then has additional logic to determine where the mock should be written to on disk.

func NewOutputter added in v2.21.0

func NewOutputter(
	config *config.Config,
	boilerplate string,
	dryRun bool,
) *Outputter

func (*Outputter) Generate added in v2.21.0

func (m *Outputter) Generate(ctx context.Context, iface *Interface) error

type Parser

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

func NewParser

func NewParser(buildTags []string) *Parser

func (*Parser) Find

func (p *Parser) Find(name string) (*Interface, error)

func (*Parser) Interfaces

func (p *Parser) Interfaces() []*Interface

func (*Parser) Load

func (p *Parser) Load() error

func (*Parser) Parse

func (p *Parser) Parse(ctx context.Context, path string) error

func (*Parser) ParsePackages added in v2.21.0

func (p *Parser) ParsePackages(ctx context.Context, packageNames []string) error

type StdoutStreamProvider

type StdoutStreamProvider struct{}

func (*StdoutStreamProvider) GetWriter

func (*StdoutStreamProvider) GetWriter(ctx context.Context, iface *Interface) (io.Writer, error, Cleanup)

type TypesPackage added in v2.21.0

type TypesPackage interface {
	Name() string
	Path() string
}

type Walker

type Walker struct {
	config.Config
	BaseDir   string
	Recursive bool
	Filter    *regexp.Regexp
	LimitOne  bool
	BuildTags []string
}

func (*Walker) Walk

func (w *Walker) Walk(ctx context.Context, visitor WalkerVisitor) (generated bool)

type WalkerVisitor

type WalkerVisitor interface {
	VisitWalk(context.Context, *Interface) error
}

Jump to

Keyboard shortcuts

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