pkg

package
v3.0.0-alpha.10 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2025 License: BSD-3-Clause Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoConfigFile         = fmt.Errorf("no config file exists")
	ErrNoGoFilesFoundInRoot = fmt.Errorf("no go files found in root search path")
	ErrPkgNotFound          = fmt.Errorf("package not found in config")
	ErrGoModNotFound        = fmt.Errorf("no go.mod file found")
	ErrGoModInvalid         = fmt.Errorf("go.mod file has no module line")
)
View Source
var ErrInfiniteLoop = fmt.Errorf("infinite loop in template variables detected")

Functions

func IsAutoGenerated

func IsAutoGenerated(path *pathlib.Path) (bool, error)

Types

type Config

type Config struct {
	All             bool                   `mapstructure:"all"`
	Anchors         map[string]any         `mapstructure:"_anchors"`
	BoilerplateFile string                 `mapstructure:"boilerplate-file"`
	BuildTags       string                 `mapstructure:"tags"`
	Config          string                 `mapstructure:"config"`
	Dir             string                 `mapstructure:"dir"`
	Exclude         []string               `mapstructure:"exclude"`
	ExcludeRegex    string                 `mapstructure:"exclude-regex"`
	FileName        string                 `mapstructure:"filename"`
	Formatter       string                 `mapstructure:"formatter"`
	IncludeRegex    string                 `mapstructure:"include-regex"`
	LogLevel        string                 `mapstructure:"log-level"`
	MockBuildTags   string                 `mapstructure:"mock-build-tags"`
	MockName        string                 `mapstructure:"mockname"`
	PkgName         string                 `mapstructure:"pkgname"`
	Packages        map[string]interface{} `mapstructure:"packages"`
	Recursive       bool                   `mapstructure:"recursive"`
	Template        string                 `mapstructure:"template"`
	TemplateData    map[string]any         `mapstructure:"template-data"`
	UnrollVariadic  bool                   `mapstructure:"unroll-variadic"`
	Version         bool                   `mapstructure:"version"`
	// contains filtered or unexported fields
}

func NewConfigFromViper

func NewConfigFromViper(v *viper.Viper) (*Config, error)

func (*Config) CfgAsMap

func (c *Config) CfgAsMap(ctx context.Context) (map[string]any, error)

CfgAsMap reads in the config file and returns a map representation, instead of a struct representation. This is mainly needed because viper throws away case-sensitivity in the `packages` section, which won't work when defining interface names 😞

func (*Config) ClearCfgAsMap

func (c *Config) ClearCfgAsMap()

func (*Config) ExcludePath

func (c *Config) ExcludePath(path string) bool

func (*Config) FilePath

func (c *Config) FilePath(ctx context.Context) *pathlib.Path

func (*Config) GetInterfaceConfig

func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, interfaceName string) ([]*Config, error)

func (*Config) GetPackageConfig

func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Config, error)

GetPackageConfig returns a struct representation of the package's config as provided in yaml. If the package did not specify a config section, this method will inject the top-level config into the package's config. This is especially useful as it allows us to lazily evaluate a package's config. If the package does specify config, this method takes care to merge the top-level config with the values specified for this package.

func (*Config) GetPackages

func (c *Config) GetPackages(ctx context.Context) ([]string, error)

GetPackages returns a list of the packages that are defined in the `packages` config section.

func (*Config) Initialize

func (c *Config) Initialize(ctx context.Context) error

func (*Config) ParseTemplates

func (c *Config) ParseTemplates(ctx context.Context, iface *Interface, srcPkg *packages.Package) error

ParseTemplates parses various templated strings in the config struct into their fully defined values. This mutates the config object passed. An *Interface object can be supplied to satisfy template variables that need information about the original interface being mocked. If this argument is nil, interface-specific template variables will be set to the empty string. The srcPkg is also needed to satisfy template variables regarding the source package.

func (*Config) ShouldGenerateInterface

func (c *Config) ShouldGenerateInterface(ctx context.Context, packageName, interfaceName string) (bool, error)

func (*Config) TagName

func (c *Config) TagName(name string) string

type Formatter

type Formatter string
const (
	FORMAT_GOFMT      Formatter = "gofmt"
	FORMAT_GOIMPORRTS Formatter = "goimports"
	FORMAT_NOOP       Formatter = "noop"
)

type Interface

type Interface struct {
	Name     string // Name of the type to be mocked.
	FileName string
	File     *ast.File
	Pkg      *packages.Package
	Config   *Config
}

func NewInterface

func NewInterface(name string, filename string, file *ast.File, pkg *packages.Package, config *Config) *Interface

type NodeVisitor

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

func NewNodeVisitor

func NewNodeVisitor(ctx context.Context) *NodeVisitor

func (*NodeVisitor) DeclaredInterfaces

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

func (*NodeVisitor) Visit

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

type Parser

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

func NewParser

func NewParser(buildTags []string) *Parser

func (*Parser) ParsePackages

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

type TemplateGenerator

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

func NewTemplateGenerator

func NewTemplateGenerator(
	ctx context.Context,
	srcPkg *packages.Package,
	outPkgFSPath *pathlib.Path,
	templateName string,
	formatter Formatter,
	pkgConfig *Config,
) (*TemplateGenerator, error)

func (*TemplateGenerator) Generate

func (g *TemplateGenerator) Generate(
	ctx context.Context,
	interfaces []*Interface,
) ([]byte, error)

Jump to

Keyboard shortcuts

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