-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethod.go
More file actions
65 lines (54 loc) · 1.26 KB
/
Copy pathmethod.go
File metadata and controls
65 lines (54 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package aster
import (
"go/ast"
"strings"
)
type MethodType struct {
FuncType
Receiver *FieldType `json:",omitemtpy"`
FuncDecl *ast.FuncDecl `json:"-"`
}
func NewMethodType(astFuncDecl *ast.FuncDecl) (*MethodType, error) {
funcType, err := NewFuncTypeByASTDecl(astFuncDecl)
if err != nil {
return nil, err
}
recvier := astFuncDecl.Recv.List[0]
fieldTypes, err := NewFieldTypes(recvier)
if err != nil {
return nil, err
}
// recevier没有省略语法的场景
fieldType := fieldTypes[0]
methodType := &MethodType{
FuncType: *funcType,
FuncDecl: astFuncDecl,
Receiver: fieldType,
}
return methodType, nil
}
func (this *MethodType) String() string {
sb := strings.Builder{}
sb.WriteString("func (" + this.Receiver.Name + " " + this.Receiver.Type.GetDecl() + ") " + this.Name + "(")
for i, paramType := range this.Params {
sb.WriteString(paramType.GetDecl())
if i < len(this.Params)-1 {
sb.WriteString(", ")
}
}
sb.WriteString(") ")
if len(this.Results) > 1 {
sb.WriteString("(")
}
for i, resultType := range this.Results {
sb.WriteString(resultType.GetDecl())
if i < len(this.Results)-1 {
sb.WriteString(", ")
}
}
if len(this.Results) > 1 {
sb.WriteString(")")
}
sb.WriteString(" {\n\t// ...\n}\n")
return sb.String()
}