generator

package
Version: v1.2.5 Latest Latest
Warning

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

Go to latest
Published: May 19, 2022 License: MIT Imports: 14 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

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

Generator generates decorators for the interface types

func NewGenerator

func NewGenerator(options Options) (*Generator, error)

NewGenerator returns Generator initialized with options

func (Generator) Generate

func (g Generator) Generate(w io.Writer) error

Generate generates code using header and body templates

type Method

type Method struct {
	Doc     []string
	Comment []string
	Name    string
	Params  ParamsSlice
	Results ParamsSlice

	ReturnsError   bool
	AcceptsContext bool
}

Method represents a method's signature

func NewMethod

func NewMethod(name string, fi *ast.Field, printer typePrinter) (*Method, error)

NewMethod returns pointer to Signature struct or error

func (Method) Call

func (m Method) Call() string

Call returns a string with the method call

func (Method) Declaration

func (m Method) Declaration() string

Declaration returns a method name followed by it's signature

func (Method) HasParams

func (m Method) HasParams() bool

HasParams returns true if method has params

func (Method) HasResults

func (m Method) HasResults() bool

HasResults returns true if method has results

func (Method) ParamsMap added in v1.1.0

func (m Method) ParamsMap() string

ParamsMap returns a string representation of the map[string]interface{} filled with method's params

func (Method) ParamsNames added in v1.1.0

func (m Method) ParamsNames() string

ParamsNames returns a list of method params names

func (Method) ParamsStruct added in v1.1.0

func (m Method) ParamsStruct() string

ParamsStruct returns a struct type with fields corresponding to the method params

func (Method) Pass

func (m Method) Pass(prefix string) string

Pass returns a return statement followed by the method call If method does not have any results it returns a method call followed by return statement

func (Method) ResultsMap added in v1.1.0

func (m Method) ResultsMap() string

ResultsMap returns a string representation of the map[string]interface{} filled with method's results

func (Method) ResultsNames

func (m Method) ResultsNames() string

ResultsNames returns a list of method results names

func (Method) ResultsStruct

func (m Method) ResultsStruct() string

ResultsStruct returns a struct type with fields corresponding to the method results

func (Method) ReturnStruct

func (m Method) ReturnStruct(structName string) string

ReturnStruct returns return statement with the return params taken from the structName

func (Method) Signature

func (m Method) Signature() string

Signature returns comma separated method's params followed by the comma separated method's results

type Options

type Options struct {
	//InterfaceName is a name of interface type
	InterfaceName string

	//Imports from the file with interface definition
	Imports []string

	//SourcePackage is an import path or a relative path of the package that contains the source interface
	SourcePackage string

	//SourcePackageAlias is an import selector defauls is source package name
	SourcePackageAlias string

	//OutputFile name which is used to detect destination package name and also to fix imports in the resulting source
	OutputFile string

	//HeaderTemplate is used to generate package clause and comment over the generated source
	HeaderTemplate string

	//BodyTemplate generates import section, decorator constructor and methods
	BodyTemplate string

	//Vars additional vars that are passed to the templates from the command line
	Vars map[string]interface{}

	//HeaderVars header specific variables
	HeaderVars map[string]interface{}

	//Funcs is a map of helper functions that can be used within a template
	Funcs template.FuncMap

	//LocalPrefix is a comma-separated string of import path prefixes, which, if set, instructs Process to sort the import
	//paths with the given prefixes into another group after 3rd-party packages.
	LocalPrefix string
}

Options of the NewGenerator constructor

type Param

type Param struct {
	Doc      []string
	Comment  []string
	Name     string
	Type     string
	Variadic bool
}

Param represents fuction argument or result

func NewParam

func NewParam(name string, fi *ast.Field, usedNames map[string]bool, printer typePrinter) (*Param, error)

NewParam returns Param struct

func (Param) Pass

func (p Param) Pass() string

Pass returns a name of the parameter If parameter is variadic it returns a name followed by a ...

type ParamsSlice added in v1.1.0

type ParamsSlice []Param

ParamsSlice slice of parameters

func (ParamsSlice) Pass added in v1.1.0

func (ps ParamsSlice) Pass() string

Pass returns comma separated params names to be passed to a function call with respect to variadic functions

func (ParamsSlice) String added in v1.1.0

func (ps ParamsSlice) String() string

String implements fmt.Stringer

type TemplateInputInterface added in v1.1.0

type TemplateInputInterface struct {
	Name string
	// Type of the interface, with package name qualifier (e.g. sort.Interface)
	Type string
	// Methods name keyed map of method information
	Methods map[string]Method
}

TemplateInputInterface subset of interface information used for template generation

type TemplateInputs added in v1.1.0

type TemplateInputs struct {
	// Interface information for template
	Interface TemplateInputInterface
	// Vars additional vars to pass to the template, see Options.Vars
	Vars    map[string]interface{}
	Imports []string
}

TemplateInputs information passed to template for generation

func (TemplateInputs) Import added in v1.1.10

func (t TemplateInputs) Import(imports ...string) string

Import generates an import statement using a list of imports from the source file along with the ones from the template itself

Jump to

Keyboard shortcuts

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