generator

package module
v0.0.0-...-2e3c43d Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2019 License: MIT Imports: 13 Imported by: 6

README

generator GoDoc Build Status

generator

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func First

func First(i interface{}) (interface{}, error)

First returns first element of given slice or interface

func Last

func Last(i interface{}) (interface{}, error)

Last returns last element of given slice or interface

func MixedCaps

func MixedCaps(s string) string

MixedCaps transform underscored string to mixed-caps string

func NormalizeImportPath

func NormalizeImportPath(path string) string

NormalizeImportPath takes path and return import path relative to deepest nested vendor dir

func PackageAbsPath

func PackageAbsPath(p interface{}) (string, error)

PackageAbsPath parses GOPATH environment variable and returns a first absolute path matched the package p import path

func PackageOf

func PackageOf(filePath string) (string, error)

Types

type Generator

type Generator struct {
	*loader.Program
	// contains filtered or unexported fields
}

Generator stores information about imports, loaded packages, registered template functions and global template variables

func New

func New(prog *loader.Program) *Generator

New creates new Generator and returns pointer to it

func (*Generator) AddBuildTags

func (g *Generator) AddBuildTags(tags ...string)

AddBuildTags adds build tags for generated files

func (*Generator) AddTemplateFunc

func (g *Generator) AddTemplateFunc(name string, f interface{})

AddTemplateFunc registers a helper function that can be called from any of templates that processed via generator

func (*Generator) AddTypePrefix

func (g *Generator) AddTypePrefix(typeName, prefix string)

func (*Generator) ConvertType

func (g *Generator) ConvertType(from, to string)

ConvertType sets type conversion rule that will be applied by generator to functions' arguments types structs' fields types and so on.

func (*Generator) Copy

func (g *Generator) Copy(n interface{}) error

Copy copies entity source to the generated body applying type conversion rules and correct packages aliases

func (*Generator) CopyType

func (g *Generator) CopyType(typeSpec *ast.TypeSpec) error

CopyType copies type declaration to the generated body

func (*Generator) CopyVal

func (g *Generator) CopyVal(vSpec *ast.ValueSpec) error

CopyValue copies var or constant declaration to the generated body

func (*Generator) ExpressionType

func (g *Generator) ExpressionType(e ast.Expr) (types.Type, error)

ExpressionType searches amoung all loaded packages and returns a type of given ast.Expression

func (*Generator) FuncParams

func (g *Generator) FuncParams(f interface{}) (ParamSet, error)

FuncParams returns a slice of function parameters

func (*Generator) FuncResults

func (g *Generator) FuncResults(f interface{}) (ParamSet, error)

FuncResults returns a slice of function results

func (*Generator) FuncSignature

func (g *Generator) FuncSignature(f interface{}) (string, error)

FuncSignature returns a signature of the function represented by f f can be one of: ast.Expr, ast.SelectorExpr, types.Type, types.Signature

func (*Generator) Import

func (g *Generator) Import(pkg interface{}) (path, selector string)

Import imports package and returns its path and selector(alias)

func (*Generator) ImportWithAlias

func (g *Generator) ImportWithAlias(pkg interface{}, alias string) (path string, err error)

ImportWithAlias places given package to the list of imported packages and assigns alias to it. It can be useful when you want to use certain alias for the package and give generated aliases for other packages that might have the same name as the given one.

func (*Generator) New

func (g *Generator) New(varName string, varType interface{}) (string, error)

New returns initialization string of the variable according to it's type, i.e. new(..) for pointer types, make(...) for maps/chans and slices, var for arrays and named types, etc

func (*Generator) PackagePathAndName

func (g *Generator) PackagePathAndName(pkg interface{}) (path string, name string)

PackagePathAndName takes package information that can be one of: string, *types.Package, *ast.ImportSpec and returns it's import path and name

func (*Generator) PackageSelector

func (g *Generator) PackageSelector(pkg interface{}) string

PackageSelector returns package selector with respect to packages imported with aliases and automatically generated aliases

func (*Generator) ProcessTemplate

func (g *Generator) ProcessTemplate(tmplName, tmpl string, data interface{}) error

ProcessTemplate adds declarations of the global variables registered via SetVar. parses template, registers helper functions to be accessible from template and executes template

func (*Generator) SetDefaultParamsPrefix

func (g *Generator) SetDefaultParamsPrefix(prefix string)

func (*Generator) SetDefaultResultsPrefix

func (g *Generator) SetDefaultResultsPrefix(prefix string)

func (*Generator) SetHeader

func (g *Generator) SetHeader(h string)

SetHeader sets header comment for generated files

func (*Generator) SetPackageName

func (g *Generator) SetPackageName(name string)

SetPackageName sets package name of the generated file

func (*Generator) SetVar

func (g *Generator) SetVar(varName, varValue string)

SetVar sets global variable that will be accessible from all templates processed with generator

func (*Generator) TypeOf

func (g *Generator) TypeOf(i interface{}) string

TypeOf returns a type name for the given argument with package selector. Package selector is generated with respect to packages imported with aliases and automatically generated aliases. Input argument type can be one of: ast.Expr, *Param, types.TypeAndValue, types.Type

func (*Generator) Write

func (g *Generator) Write(b []byte) (int, error)

Write implements io.Writer. Performs writing of the byte slice to the generated source body

func (*Generator) WriteTo

func (g *Generator) WriteTo(w io.Writer) (int64, error)

WriteTo generates source and writes result to w

func (*Generator) WriteToFilename

func (g *Generator) WriteToFilename(filename string) error

WriteToFilename write generated source to the file and performs formatting of the source with goimports (which is also doing gofmt internally)

type Param

type Param struct {
	Type         string
	Name         string
	OriginalType types.Type
	Variadic     bool
}

Param is a proxy struct that represents parameter name and it's type

func (*Param) Pass

func (p *Param) Pass() string

func (*Param) String

func (p *Param) String() string

type ParamSet

type ParamSet []*Param

ParamSet is a helper structure that represents list of input or result parameters of a function

func (ParamSet) Names

func (ps ParamSet) Names() string

Names returns a list of params names from the ParamSet separated by commas

func (ParamSet) Pass

func (ps ParamSet) Pass() string

Pass returns a string containing parameters list to pass as a list of arguments to the function that has same signature as a function that ParamSet is originated from

func (ParamSet) String

func (ps ParamSet) String() string

func (ParamSet) Types

func (ps ParamSet) Types() string

Jump to

Keyboard shortcuts

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