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 ModifyCaller(content, callerExpr string, modifiers func([]string) ([]string, error)) (string, error)
- func ModifyFunction(content string, funcName string, functions ...FunctionOptions) (string, error)
- func ModifyGlobalArrayVar(fileContent, globalName string, options ...GlobalArrayOpts) (string, error)
- func ModifyStruct(fileContent, structName string, options ...StructOpts) (string, 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 AppendFuncStruct(name, param, code string) FunctionOptions
- func AppendFuncTestCase(testCase string) FunctionOptions
- func AppendInsideFuncCall(callName, code string, index int) FunctionOptions
- func AppendSwitchCase(condition, switchCase, switchBody string) FunctionOptions
- func NewFuncReturn(returnVars ...string) FunctionOptions
- func ReplaceFuncBody(body string) FunctionOptions
- type GlobalArrayOpts
- type GlobalOptions
- type GlobalType
- type ImportOptions
- type StructOpts
Constants ¶
This section is empty.
Variables ¶
var AppendFuncCodeAtLine = AppendFuncAtLine
AppendFuncCodeAtLine inserts code at a specific line number.
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 ModifyCaller ¶
func ModifyCaller(content, callerExpr string, modifiers func([]string) ([]string, error)) (string, error)
ModifyCaller replaces all arguments of a specific function call in the given content. The callerExpr should be in the format "pkgname.FuncName" or just "FuncName". The modifiers function is called with the existing arguments and should return the new arguments.
func ModifyFunction ¶
func ModifyFunction(content string, funcName string, functions ...FunctionOptions) (string, error)
ModifyFunction modifies a function in Go source code using functional options.
func ModifyGlobalArrayVar ¶ added in v29.4.0
func ModifyGlobalArrayVar(fileContent, globalName string, options ...GlobalArrayOpts) (string, error)
ModifyGlobalArrayVar modifies an array global array variable in the provided Go source code by appending new values.
func ModifyStruct ¶
func ModifyStruct(fileContent, structName string, options ...StructOpts) (string, error)
ModifyStruct modifies a struct in the provided Go source code.
Types ¶
type FunctionOptions ¶
type FunctionOptions func(*functionOpts)
FunctionOptions configures code generation.
func AppendFuncAtLine ¶
func AppendFuncAtLine(code string, lineNumber uint64) FunctionOptions
AppendFuncAtLine inserts code at a specific line number.
func AppendFuncCode ¶
func AppendFuncCode(code string) FunctionOptions
AppendFuncCode adds code to the end of a function, if exists, of a function in Go source code content.
func AppendFuncParams ¶
func AppendFuncParams(name, varType string, index int) FunctionOptions
AppendFuncParams adds a new parameter to a function.
func AppendFuncStruct ¶
func AppendFuncStruct(name, param, code string) FunctionOptions
AppendFuncStruct adds a field to a struct literal. For instance, // the struct has only one parameter 'Params{Param1: param1}' and we want to add // the param2 the result will be 'Params{Param1: param1, Param2: param2}'.
func AppendFuncTestCase ¶
func AppendFuncTestCase(testCase string) FunctionOptions
AppendFuncTestCase adds a test case to a test function, if exists, of a function in Go source code content.
func AppendInsideFuncCall ¶
func AppendInsideFuncCall(callName, code string, index int) FunctionOptions
AppendInsideFuncCall adds an argument to a 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 AppendSwitchCase ¶ added in v29.4.0
func AppendSwitchCase(condition, switchCase, switchBody string) FunctionOptions
AppendSwitchCase inserts a new case with the code at a specific switch condition statement.
func NewFuncReturn ¶
func NewFuncReturn(returnVars ...string) FunctionOptions
NewFuncReturn replaces return statements in a function.
func ReplaceFuncBody ¶
func ReplaceFuncBody(body string) FunctionOptions
ReplaceFuncBody replaces the entire body of a function, the method will replace first and apply the other options after.
type GlobalArrayOpts ¶ added in v29.4.0
type GlobalArrayOpts func(*globalArrayOpts)
GlobalArrayOpts configures global array variable changes.
func AppendGlobalArrayValue ¶ added in v29.4.0
func AppendGlobalArrayValue(value string) GlobalArrayOpts
AppendGlobalArrayValue add a new value inside a global array variable. For instances, the variable have only one field '[]]' and we want to add the `test2 int` the result will be 'test struct{ test1 string, test int }'.
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) ImportOptions
WithImport add a new import at the end of the imports.
func WithNamedImport ¶
func WithNamedImport(name, repo string) ImportOptions
WithNamedImport add a new import with name at the end of the imports.
type StructOpts ¶
type StructOpts func(*structOpts)
StructOpts configures struct changes.
func AppendStructValue ¶
func AppendStructValue(value, valueType string) StructOpts
AppendStructValue add a new value inside struct. For instances, the struct have only one field 'test struct{ test1 string }' and we want to add the `test2 int` the result will be 'test struct{ test1 string, test int }'.