xast

package
v28.4.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 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 added in v28.4.0

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 added in v28.4.0

func AppendImports(fileContent string, imports ...ImportOptions) (string, error)

AppendImports appends import statements to the existing import block in Go source code content.

func InsertGlobal added in v28.4.0

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 added in v28.4.0

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 added in v28.4.0

type FunctionOptions func(*functionOpts)

FunctionOptions configures code generation.

func AppendFuncAtLine added in v28.4.0

func AppendFuncAtLine(code string, lineNumber uint64) FunctionOptions

AppendFuncAtLine append a new code at line.

func AppendFuncCode added in v28.4.0

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 added in v28.4.0

func AppendFuncParams(name, varType string, index int) FunctionOptions

AppendFuncParams add a new param value.

func AppendInsideFuncCall added in v28.4.0

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 added in v28.4.0

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 added in v28.4.0

func NewFuncReturn(returnVars ...string) FunctionOptions

NewFuncReturn replaces return statements in a Go function with a new return statement.

func ReplaceFuncBody added in v28.4.0

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 added in v28.4.0

type GlobalOptions func(*globalOpts)

GlobalOptions configures code generation.

func WithGlobal added in v28.4.0

func WithGlobal(name, varType, value string) GlobalOptions

WithGlobal add a new global.

type GlobalType added in v28.4.0

type GlobalType string

GlobalType represents the global type.

const (
	GlobalTypeVar   GlobalType = "var"
	GlobalTypeConst GlobalType = "const"
)

type ImportOptions added in v28.4.0

type ImportOptions func(*importOpts)

ImportOptions configures code generation.

func WithImport added in v28.4.0

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 added in v28.4.0

func WithLastImport(repo string) ImportOptions

WithLastImport add a new import int the end.

func WithLastNamedImport added in v28.4.0

func WithLastNamedImport(name, repo string) ImportOptions

WithLastNamedImport add a new import with name in the end of the imports.

func WithNamedImport added in v28.4.0

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