Documentation ¶
Index ¶
- Constants
- func AddCode(pkgs map[string][]fun, stmt map[string]StmtParams, replace bool) (map[string][]string, error)
- func AddImport(pkgs map[string][]fun, stmt map[string]StmtParams, modify map[string][]string, ...) error
- func ParseDir(dir string, filter func(info fs.FileInfo) bool) (map[string]*ast.Package, error)
- func Position(pkgs map[string]*ast.Package, ids map[string]struct{}) map[string][]fun
- type DeclParams
- type OperationKind
- type Pack
- type StmtDepend
- type StmtParam
- type StmtParams
- type StmtVarDepend
Constants ¶
const ( AddFuncWithoutDepends = 1 + iota AddDeferFuncStmt AddDeferFuncWithVarStmt AddFuncWithVarStmt AddReturnFuncWithoutVarStmt AddReturnFuncWithVarStmt AddFuncWithoutDependsWithInject )
const ( AddFuncWithoutDependsStr = "add-func-without-depends" AddFuncWithVarStmtStr = "add-func-with-var-depend" AddFuncWithoutDependsWithInjectStr = "add-func-without-depends-with-injection" AddDeferFuncStmtStr = "add-defer-func" AddDeferFuncWithVarStmtStr = "add-defer-func-with-var-depend" AddReturnFuncWithoutVarStmtStr = "add-return-func-without-var" AddReturnFuncWithVarStmtStr = "add-return-func-with-var" )
Variables ¶
This section is empty.
Functions ¶
func AddCode ¶
func AddCode(pkgs map[string][]fun, stmt map[string]StmtParams, replace bool) (map[string][]string, error)
AddCode Insert AOP code to source code files. `pkgs` is map that save file name and function names. `pkgs` is generated by `position` function.
stmt is a map, that save function declare, key is aop id. stmtParams save the AOP stmt params. There are some things need attentions.
Typically, in the StmtParams, `DeclStmt` and `Pack` can nil. But Stmts cannot be empty. Stmts save different OperationKind stmts. More detail info please reference StmtParam usage in types.go.
Replace used to indicate replace source file or not. If replace == true, it replaces at the end. Otherwise, it will not.
func AddImport ¶
func AddImport(pkgs map[string][]fun, stmt map[string]StmtParams, modify map[string][]string, replace bool) error
AddImport Add import package for build. pkgs is generated by `position` function.
stmt is standard data, usually pass by user. The stmt save aop id and it's params.
modify is generated by AddCode. It save the files that have modified.
If replace origin file, then set replace true, otherwise, set false.
func ParseDir ¶
ParseDir use `token.ParserDir` parser specify dir. And use `filter` for filter flile info at the same time If filter is nil, it will pass all files as default. When parse success, `ParseDir` will return a map save file name and package pointer. If failed, return a error
Types ¶
type DeclParams ¶
type DeclParams struct { VarName string // VarName is the variable name ,like 'x := 1', the x is var name. Stmt []string FuncName string // FuncName is the func name, like 'x := fmt.Sprintf', the `fmt.Sprintf` is func name. }
DeclParams store stmt insert behind specify variable
type OperationKind ¶
type OperationKind int
type StmtDepend ¶
type StmtDepend interface {
Depend() []string
}
type StmtParam ¶
type StmtParam struct { Kind OperationKind Stmt []string Depends []string FuncDepends []string }
StmtParam store the metadata of stmt. Kind decides to how and where to insert stmt. Stmt is the string of stmt, use parseStmt before use these. Depends are the dependence conditions
type StmtParams ¶
type StmtParams struct { DeclStmt []DeclParams Stmts []StmtParam Packs []Pack }
StmtParams The stmt will insert into function body. There are three kind of stmt.
FunStmt declares stander func stmt, e.g. func(){xxxx}() DeferStmt declares some stmt in defer block. e.g. defer func(){xxxx}() FunVarStmt declares some stmt in return function body, e.g.
If FunVarStmt = []string{" fmt.Println("xxxx") "}, then the result is:
return func(){ // Below stmts from FunVarStmt fmt.Println("xxxx") // insert end xxxx }
DeclStmt declares some stmt bellow specify variable. like that, If we put some string stmt in []string{ `
defer func(err error){ stmt block }(err)
` } Since we don't know the err variable position in source code, but we needn't care about it. `AddCode` will try to find the err declare position, then insert stmt bellow it. But one thing we should notice that DeclStmt now only support binding one variable. That means all stmts need use the same variable. For example, the previous stmt bind err is valid. If there has other stmts binding str(a new string variable). We can't find different variables at the same time, so we can not insert stmt right.
At last, Packs save the import data. Maybe user has import the same package, so named a unique name for avoid repeat is a good idea.
type StmtVarDepend ¶
type StmtVarDepend struct {
VarName []string
}
StmtVarDepend Variable dependency condition. VarName are all the variable string names. AOP will find all the variable init complete position
func (StmtVarDepend) Depend ¶
func (sd StmtVarDepend) Depend() []string