util

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IdentNil    = "nil"
	IdentTrue   = "true"
	IdentFalse  = "false"
	IdentIgnore = "_"
)
View Source
const (
	GoBuildIgnoreComment = "//go:build ignore"
	GoModFile            = "go.mod"
	GoSumFile            = "go.sum"
	GoWorkSumFile        = "go.work.sum"
	DebugLogFile         = "debug.log"
	TempBuildDir         = ".otel-build"
)
View Source
const (
	BuildPattern    = "-p"
	BuildGoVer      = "-goversion"
	BuildPgoProfile = "-pgoprofile"
	BuildModeVendor = "-mod=vendor"
	BuildModeMod    = "-mod=mod"
	BuildWork       = "-work"
)
View Source
const (
	PInvalid    = "invalid"
	PPreprocess = "preprocess"
	PInstrument = "instrument"
)

Variables

View Source
var Guarantee = Assert // More meaningful name:)

Functions

func AddImportForcely added in v0.8.0

func AddImportForcely(root *dst.File, paths ...string) *dst.GenDecl

func AddStructField added in v0.8.0

func AddStructField(decl dst.Decl, name string, typ string)

func AddressOf added in v0.8.0

func AddressOf(expr dst.Expr) *dst.UnaryExpr

AST Construction

func ArrayType added in v0.8.0

func ArrayType(elem dst.Expr) *dst.ArrayType

func Assert

func Assert(cond bool, format string, args ...interface{})

func AssertGoBuild added in v0.8.0

func AssertGoBuild(args []string)

func AssignStmt added in v0.8.0

func AssignStmt(lhs, rhs dst.Expr) *dst.AssignStmt

func Block added in v0.8.0

func Block(stmt dst.Stmt) *dst.BlockStmt

func BlockStmts added in v0.8.0

func BlockStmts(stmts ...dst.Stmt) *dst.BlockStmt

func BoolFalse added in v0.8.0

func BoolFalse() *dst.BasicLit

func BoolTrue added in v0.8.0

func BoolTrue() *dst.BasicLit

func CallTo added in v0.8.0

func CallTo(name string, args []dst.Expr) *dst.CallExpr

func CopyDir added in v0.4.0

func CopyDir(src string, dst string) error

func CopyFile

func CopyFile(src, dst string) error

func DeferStmt added in v0.8.0

func DeferStmt(call *dst.CallExpr) *dst.DeferStmt

func DefineStmts added in v0.8.0

func DefineStmts(lhs, rhs []dst.Expr) *dst.AssignStmt

func DereferenceOf added in v0.8.0

func DereferenceOf(expr dst.Expr) dst.Expr

func EmptyStmt added in v0.8.0

func EmptyStmt() *dst.EmptyStmt

func ExprStmt added in v0.8.0

func ExprStmt(expr dst.Expr) *dst.ExprStmt

func Exprs added in v0.8.0

func Exprs(exprs ...dst.Expr) []dst.Expr

func FindFuncDecl added in v0.8.0

func FindFuncDecl(root *dst.File, name string) *dst.FuncDecl

func FindImport added in v0.8.0

func FindImport(root *dst.File, path string) *dst.ImportSpec

func GetInstrumentLogPath added in v0.8.0

func GetInstrumentLogPath(name string) string

func GetLogPath added in v0.8.0

func GetLogPath(name string) string

func GetLoggerPath added in v0.8.0

func GetLoggerPath() string

func GetPreprocessLogPath added in v0.8.0

func GetPreprocessLogPath(name string) string

func GetTempBuildDir added in v0.8.0

func GetTempBuildDir() string

func GetTempBuildDirWith added in v0.8.0

func GetTempBuildDirWith(name string) string

func GetToolName added in v0.5.0

func GetToolName() (string, error)

func GetVarNameOfFunc added in v0.8.0

func GetVarNameOfFunc(fn string) string

func GuaranteeInInstrument added in v0.5.0

func GuaranteeInInstrument()

func GuaranteeInPreprocess added in v0.5.0

func GuaranteeInPreprocess()

func HasGoBuildComment added in v0.8.0

func HasGoBuildComment(text string) bool

func HasReceiver added in v0.8.0

func HasReceiver(fn *dst.FuncDecl) bool

func Ident added in v0.8.0

func Ident(name string) *dst.Ident

func IfNotNilStmt added in v0.8.0

func IfNotNilStmt(cond dst.Expr, body, elseBody *dst.BlockStmt) *dst.IfStmt

func IfStmt added in v0.8.0

func IfStmt(init dst.Stmt, cond dst.Expr,
	body, elseBody *dst.BlockStmt) *dst.IfStmt

func InInstrument added in v0.5.0

func InInstrument() bool

func InPreprocess added in v0.5.0

func InPreprocess() bool

func IndexExpr added in v0.8.0

func IndexExpr(x dst.Expr, index dst.Expr) *dst.IndexExpr

func IntLit added in v0.8.0

func IntLit(value int) *dst.BasicLit

func InterfaceType added in v0.8.0

func InterfaceType() *dst.InterfaceType

func IsCompileCommand added in v0.8.0

func IsCompileCommand(line string) bool

func IsEllipsis added in v0.8.0

func IsEllipsis(typ dst.Expr) bool

func IsGoFile added in v0.8.0

func IsGoFile(path string) bool

func IsGoModFile added in v0.8.0

func IsGoModFile(path string) bool

func IsGoSumFile added in v0.8.0

func IsGoSumFile(path string) bool

func IsGoTestFile added in v0.8.0

func IsGoTestFile(path string) bool

func IsInterfaceType added in v0.8.0

func IsInterfaceType(typ dst.Expr) bool

func IsModPath added in v0.8.0

func IsModPath(path string) bool

IsModPath checks if the provided module path is valid.

func IsStringLit added in v0.8.0

func IsStringLit(expr dst.Expr, val string) bool

func IsUnix added in v0.3.0

func IsUnix() bool

func IsUnusedIdent added in v0.8.0

func IsUnusedIdent(ident *dst.Ident) bool

func IsWindows added in v0.3.0

func IsWindows() bool

func Jsonify added in v0.8.0

func Jsonify(v interface{}) string

func ListFiles

func ListFiles(dir string) ([]string, error)

func Log added in v0.5.0

func Log(format string, args ...interface{})

func LogFatal added in v0.5.0

func LogFatal(format string, args ...interface{})

func MakeUnusedIdent added in v0.8.0

func MakeUnusedIdent(ident *dst.Ident) *dst.Ident

func MatchFuncDecl added in v0.8.0

func MatchFuncDecl(decl dst.Decl, function string, receiverType string) bool

func MatchStructDecl added in v0.8.0

func MatchStructDecl(decl dst.Decl, structType string) bool

func NewField added in v0.8.0

func NewField(name string, typ dst.Expr) *dst.Field

func NewVarDecl added in v0.8.0

func NewVarDecl(name string, paramTypes *dst.FieldList) *dst.GenDecl

func ParenExpr added in v0.8.0

func ParenExpr(x dst.Expr) *dst.ParenExpr

func ParseAstFromFile added in v0.8.0

func ParseAstFromFile(filePath string) (*dst.File, error)

ParseAstFromFile parses the AST from complete source file.

func ParseAstFromFileFast added in v0.8.0

func ParseAstFromFileFast(filePath string) (*dst.File, error)

func ParseAstFromFileOnlyPackage added in v0.8.0

func ParseAstFromFileOnlyPackage(filePath string) (*dst.File, error)

func PathExists

func PathExists(path string) bool

func PathNotExists added in v0.8.0

func PathNotExists(path string) bool

func PhaseTimer added in v0.4.0

func PhaseTimer(name string) func()

func RandomString

func RandomString(n int) string

RandomString generates a globally unique random string of length n

func ReadFile

func ReadFile(filePath string) (string, error)

func RemoveGoBuildComment added in v0.8.0

func RemoveGoBuildComment(text string) string

func RemoveImport added in v0.8.0

func RemoveImport(root *dst.File, path string) *dst.ImportSpec

func RenamePackage added in v0.8.0

func RenamePackage(source, newPkgName string) string

func ReturnStmt added in v0.8.0

func ReturnStmt(results []dst.Expr) *dst.ReturnStmt

func RunCmd

func RunCmd(args ...string) error

func SelectorExpr added in v0.8.0

func SelectorExpr(x dst.Expr, sel string) *dst.SelectorExpr

func SetLogger added in v0.8.0

func SetLogger(w *os.File)

Be caution it's not thread safe

func SetRunPhase added in v0.5.0

func SetRunPhase(phase RunPhase)

func ShouldNotReachHere

func ShouldNotReachHere()

func ShouldNotReachHereT

func ShouldNotReachHereT(msg string)

func SplitCmds added in v0.8.0

func SplitCmds(input string) []string

SplitCmds splits the command line by space, but keep the quoted part as a whole. For example, "a b" c will be split into ["a b", "c"].

func Stmts added in v0.8.0

func Stmts(stmts ...dst.Stmt) []dst.Stmt

func StringLit added in v0.8.0

func StringLit(value string) *dst.BasicLit

func SwitchCase added in v0.8.0

func SwitchCase(list []dst.Expr, stmts []dst.Stmt) *dst.CaseClause

func TypeAssertExpr added in v0.8.0

func TypeAssertExpr(x dst.Expr, typ dst.Expr) *dst.TypeAssertExpr

func Unimplemented

func Unimplemented()

func UnimplementedT

func UnimplementedT(msg string)

func WriteAstToFile added in v0.8.0

func WriteAstToFile(astRoot *dst.File, filePath string) (string, error)

WriteAstToFile writes the AST to source file.

func WriteFile added in v0.4.0

func WriteFile(filePath string, content string) (string, error)

Types

type AstParser added in v0.9.0

type AstParser struct {
	// contains filtered or unexported fields
}

func NewAstParser added in v0.9.0

func NewAstParser() *AstParser

func (*AstParser) FindPosition added in v0.9.0

func (ap *AstParser) FindPosition(node dst.Node) token.Position

func (*AstParser) ParseFile added in v0.9.0

func (ap *AstParser) ParseFile(filePath string, mode parser.Mode) (*dst.File, error)

func (*AstParser) ParseSnippet added in v0.9.0

func (ap *AstParser) ParseSnippet(codeSnippnet string) ([]dst.Stmt, error)

ParseSnippet parses the AST from incomplete source code snippet.

func (*AstParser) ParseSource added in v0.9.0

func (ap *AstParser) ParseSource(source string) (*dst.File, error)

ParseSource parses the AST from complete source code.

type RunPhase added in v0.5.0

type RunPhase string

func GetRunPhase added in v0.5.0

func GetRunPhase() RunPhase

func (RunPhase) String added in v0.5.0

func (rp RunPhase) String() string

Jump to

Keyboard shortcuts

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