1 Star 0 Fork 0

lipore/plume

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
interface.go 1.35 KB
一键复制 编辑 原始数据 按行查看 历史
lipore 提交于 2022-10-11 14:36 +08:00 . feat(option): option/ code gen tested
package code
import (
"go/ast"
"strings"
)
// InterfaceSpec is a definition of the interface
type InterfaceSpec struct {
Name string
Methods []*Function
}
// InterfaceSpecs is a group of InterfaceSpec model
type InterfaceSpecs []*InterfaceSpec
// ByName return interface by name Another return value shows whether there is an interface
// with that name exists.
func (intfs InterfaceSpecs) ByName(name string) (*InterfaceSpec, bool) {
for _, intf := range intfs {
if intf.Name == name {
return intf, true
}
}
return nil, false
}
// Param is a model of method parameter
type Param struct {
Name string
Type Type
}
func ExtractInterfaceSpec(spec *ast.TypeSpec, iType *ast.InterfaceType) *InterfaceSpec {
name := spec.Name.Name
intf := InterfaceSpec{
Name: name,
}
for _, method := range iType.Methods.List {
funcType, ok := method.Type.(*ast.FuncType)
if !ok {
continue
}
var methodName string
for _, n := range method.Names {
methodName = n.Name
break
}
var comments []string
if method.Doc != nil {
for _, comment := range method.Doc.List {
commentRunes := []rune(comment.Text)
commentText := strings.TrimSpace(string(commentRunes[2:]))
comments = append(comments, commentText)
}
}
meth := ExtractFunction(methodName, comments, funcType)
intf.Methods = append(intf.Methods, meth)
}
return &intf
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lipore/plume.git
git@gitee.com:lipore/plume.git
lipore
plume
plume
v1.7.10

搜索帮助