xast

package
v29.0.0-...-a2903dc Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStop = errors.New("ast stop")

Functions

func AppendFunction

func AppendFunction(fileContent string, function string) (modifiedContent string, err error)

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

func Inspect(n ast.Node, f func(n ast.Node) error) (err error)

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.

func ParseDir

func ParseDir(dir string) (*ast.Package, *token.FileSet, error)

ParseDir invokes ast.ParseDir and returns the first package found that is doesn't has the "_test" suffix.

func ParseFile

func ParseFile(filepath string) (*ast.File, *token.FileSet, error)

ParseFile invokes ast.ParseFile and returns the *ast.File.

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.

Jump to

Keyboard shortcuts

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