代码拉取完成,页面将自动刷新
package proto_parser
import (
"fmt"
"go/ast"
)
// ecInfo 是 errorCode info 的简称
type ecInfo struct {
pkg string
name string
fullName string
}
// getFuncDeclName 获取 FuncDecl 函数名称
func getFuncDeclName(f *ast.FuncDecl) string {
return f.Name.Name
}
// getFuncDeclBody 获取 FuncDecl body
func getFuncDeclBody(f *ast.FuncDecl) *ast.BlockStmt {
return f.Body
}
// iterBodyToGetCreateErrorStmt 获取 CreateError
func iterBodyToGetCreateErrorStmt(body interface{}) (bool, []ecInfo) {
var res []ecInfo
switch t := body.(type) {
case *ast.BlockStmt:
// 是 body 取行 再递归
for _, stmt := range t.List {
ok, r := iterBodyToGetCreateErrorStmt(stmt)
if ok {
res = append(res, r...)
}
}
case *ast.AssignStmt:
// 是申明
for _, rh := range t.Rhs {
ok, r := iterBodyToGetCreateErrorStmt(rh)
if ok {
res = append(res, r...)
}
}
case *ast.CallExpr:
fun, ok := t.Fun.(*ast.SelectorExpr)
if ok {
x, xok := fun.X.(*ast.Ident)
if !xok {
return false, nil
}
if x.Name != "core" {
return false, nil
}
if fun.Sel.Name != "CreateError" && fun.Sel.Name != "CreateErrorWithMsg" {
return false, nil
}
if len(t.Args) < 1 {
return false, nil
}
val, aok := t.Args[0].(*ast.SelectorExpr)
if !aok {
return false, nil
}
pkg, pok := val.X.(*ast.Ident)
if !pok {
return false, nil
}
code := val.Sel.Name
res = append(res, ecInfo{
pkg: pkg.Name,
name: code,
fullName: fmt.Sprintf("%s.%s", pkg.Name, code),
})
return true, res
}
case *ast.ReturnStmt:
for _, result := range t.Results {
ok, r := iterBodyToGetCreateErrorStmt(result)
if ok {
res = append(res, r...)
}
}
case *ast.IfStmt:
ok, r := iterBodyToGetCreateErrorStmt(t.Body)
if ok {
res = append(res, r...)
}
case *ast.ForStmt:
ok, r := iterBodyToGetCreateErrorStmt(t.Body)
if ok {
res = append(res, r...)
}
case *ast.SwitchStmt:
ok, r := iterBodyToGetCreateErrorStmt(t.Body)
if ok {
res = append(res, r...)
}
case *ast.SelectStmt:
ok, r := iterBodyToGetCreateErrorStmt(t.Body)
if ok {
res = append(res, r...)
}
}
if len(res) != 0 {
return true, res
}
return false, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。