Documentation ¶
Index ¶
- Variables
- func AppendFunction(fileContent string, function string) (modifiedContent string, err error)
- func AppendImports(fileContent string, imports ...ImportOptions) (string, error)
- func InsertGlobal(fileContent string, globalType GlobalType, globals ...GlobalOptions) (modifiedContent string, err error)
- func Inspect(n ast.Node, f func(n ast.Node) error) (err error)
- func ModifyFunction(fileContent, functionName string, functions ...FunctionOptions) (modifiedContent string, err error)
- func ParseDir(dir string) (*ast.Package, *token.FileSet, error)
- func ParseFile(filepath string) (*ast.File, *token.FileSet, error)
- type FunctionOptions
- func AppendFuncAtLine(code string, lineNumber uint64) FunctionOptions
- func AppendFuncCode(code string) FunctionOptions
- func AppendFuncParams(name, varType string, index int) FunctionOptions
- func AppendInsideFuncCall(callName, code string, index int) FunctionOptions
- func AppendInsideFuncStruct(structName, paramName, code string, index int) FunctionOptions
- func NewFuncReturn(returnVars ...string) FunctionOptions
- func ReplaceFuncBody(body string) FunctionOptions
- type GlobalOptions
- type GlobalType
- type ImportOptions
Constants ¶
This section is empty.
Variables ¶
var ErrStop = errors.New("ast stop")
Functions ¶
func AppendFunction ¶
AppendFunction appends a new function to the end of the Go source code content.
func AppendImports ¶
func AppendImports(fileContent string, imports ...ImportOptions) (string, error)
AppendImports appends import statements to the existing import block in Go source code content.
func InsertGlobal ¶
func InsertGlobal(fileContent string, globalType GlobalType, globals ...GlobalOptions) (modifiedContent string, err error)
InsertGlobal inserts global variables or constants into the provided Go source code content after the import section. The function parses the provided content, locates the import section, and inserts the global declarations immediately after it. The type of globals (variables or constants) is specified by the globalType parameter. Each global declaration is defined by calling WithGlobal function with appropriate arguments. The function returns the modified content with the inserted global declarations.
func Inspect ¶
Inspect is like ast.Inspect but with error handling. Unlike ast.Inspect the function parameter f returns an error and not a bool. The returned error is propagated to the caller, unless it is equal to ErrStop, which in that case indicates the child nodes shouldn't not be inspected (like returning false in the function of ast.Inspect).
func ModifyFunction ¶
func ModifyFunction(fileContent, functionName string, functions ...FunctionOptions) (modifiedContent string, err error)
ModifyFunction modify a function based in the options.
Types ¶
type FunctionOptions ¶
type FunctionOptions func(*functionOpts)
FunctionOptions configures code generation.
func AppendFuncAtLine ¶
func AppendFuncAtLine(code string, lineNumber uint64) FunctionOptions
AppendFuncAtLine append a new code at line.
func AppendFuncCode ¶
func AppendFuncCode(code string) FunctionOptions
AppendFuncCode append code before the end or the return, if exists, of a function in Go source code content.
func AppendFuncParams ¶
func AppendFuncParams(name, varType string, index int) FunctionOptions
AppendFuncParams add a new param value.
func AppendInsideFuncCall ¶
func AppendInsideFuncCall(callName, code string, index int) FunctionOptions
AppendInsideFuncCall add code inside another function call. For instances, the method have a parameter a call 'New(param1, param2)' and we want to add the param3 the result will be 'New(param1, param2, param3)'.
func AppendInsideFuncStruct ¶
func AppendInsideFuncStruct(structName, paramName, code string, index int) FunctionOptions
AppendInsideFuncStruct add code inside another function call. For instances, the struct have only one parameter 'Params{Param1: param1}' and we want to add the param2 the result will be 'Params{Param1: param1, Param2: param2}'.
func NewFuncReturn ¶
func NewFuncReturn(returnVars ...string) FunctionOptions
NewFuncReturn replaces return statements in a Go function with a new return statement.
func ReplaceFuncBody ¶
func ReplaceFuncBody(body string) FunctionOptions
ReplaceFuncBody replace all body of the function, the method will replace first and apply the other options after.
type GlobalOptions ¶
type GlobalOptions func(*globalOpts)
GlobalOptions configures code generation.
func WithGlobal ¶
func WithGlobal(name, varType, value string) GlobalOptions
WithGlobal add a new global.
type GlobalType ¶
type GlobalType string
GlobalType represents the global type.
const ( GlobalTypeVar GlobalType = "var" GlobalTypeConst GlobalType = "const" )
type ImportOptions ¶
type ImportOptions func(*importOpts)
ImportOptions configures code generation.
func WithImport ¶
func WithImport(repo string, index int) ImportOptions
WithImport add a new import. If the index is -1 will append in the end of the imports.
func WithLastImport ¶
func WithLastImport(repo string) ImportOptions
WithLastImport add a new import int the end.
func WithLastNamedImport ¶
func WithLastNamedImport(name, repo string) ImportOptions
WithLastNamedImport add a new import with name in the end of the imports.
func WithNamedImport ¶
func WithNamedImport(name, repo string, index int) ImportOptions
WithNamedImport add a new import with name. If the index is -1 will append in the end of the imports.