1 Star 0 Fork 0

lipore/plume

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
function.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
lipore 提交于 2022-10-11 14:36 . feat(option): option/ code gen tested
package code
import "go/ast"
type Function struct {
Name string
Params []Param
Returns []Type
Comments []string
}
type Method struct {
Function
Receiver Receiver
}
type Receiver struct {
Type Type
Name string
}
func ExtractFunction(name string, commonts []string, funcType *ast.FuncType) *Function {
funct := Function{
Name: name,
Comments: commonts,
}
for _, param := range funcType.Params.List {
paramType := getType(param.Type)
if len(param.Names) == 0 {
funct.Params = append(funct.Params, Param{Type: paramType})
continue
}
for _, name := range param.Names {
funct.Params = append(funct.Params, Param{
Name: name.Name,
Type: paramType,
})
}
}
if funcType.Results != nil {
for _, result := range funcType.Results.List {
funct.Returns = append(funct.Returns, getType(result.Type))
}
}
return &funct
}
func ExtractMethod(funcDecl *ast.FuncDecl) *Method {
name := funcDecl.Name.Name
funcType := funcDecl.Type
function := ExtractFunction(name, nil, funcType)
recv := funcDecl.Recv
if recv != nil && len(recv.List) > 0 {
meth := Method{
Function: *function,
Receiver: Receiver{
Type: getType(recv.List[0].Type),
Name: recv.List[0].Names[0].Name,
},
}
return &meth
}
return &Method{
Function: *function,
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lipore/plume.git
git@gitee.com:lipore/plume.git
lipore
plume
plume
v1.7.10

搜索帮助