1 Star 0 Fork 0

lipore/plume

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
base.go 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
lipore 提交于 2023-12-07 21:07 . --wip-- [skip ci]
package models
import (
"go/ast"
"strings"
)
type PackageInfo struct {
Name string
Path string
Imports Imports
}
// Package is the package info
type Package struct {
PackageInfo
InterfaceInfos map[string]*InterfaceInfo
StructInfos map[string]*StructInfo
}
func (p *Package) Visit(node ast.Node) (w ast.Visitor) {
switch n := node.(type) {
case *ast.File:
for _, imp := range n.Imports {
i := &Import{}
p.Imports = append(p.Imports, i)
ast.Walk(i, imp)
}
case *ast.GenDecl:
genType := NewGenType(p)
return genType
case *ast.FuncDecl:
m := &MethodInfo{}
ast.Walk(m, n)
structName := strings.TrimPrefix(m.RecvType, "*")
p.StructInfos[structName].Methods = append(p.StructInfos[structName].Methods, m)
return nil
}
return p
}
func NewPackage(name, path string) *Package {
return &Package{
PackageInfo: PackageInfo{
Name: name,
Path: path,
Imports: make([]*Import, 0),
},
InterfaceInfos: make(map[string]*InterfaceInfo),
StructInfos: make(map[string]*StructInfo),
}
}
func getType(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + getType(t.X)
case *ast.SelectorExpr:
return getType(t.X) + "." + getType(t.Sel)
case *ast.ArrayType:
return "[]" + getType(t.Elt)
case *ast.MapType:
return "map[" + getType(t.Key) + "]" + getType(t.Value)
case *ast.InterfaceType:
return "interface{}"
case *ast.FuncType:
return "func(...)"
case *ast.ChanType:
return "chan " + getType(t.Value)
case *ast.Ellipsis:
return "..." + getType(t.Elt)
}
return ""
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lipore/plume.git
git@gitee.com:lipore/plume.git
lipore
plume
plume
v1.7.10

搜索帮助