-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspector.go
164 lines (127 loc) · 3.55 KB
/
inspector.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package lib
import (
"fmt"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"log"
"strings"
"golang.org/x/tools/imports"
)
// FormatCode formats the code
func FormatCode(code string) ([]byte, error) {
opts := &imports.Options{
TabIndent: true,
TabWidth: 2,
Fragment: true,
Comments: true,
}
return imports.Process("", []byte(code), opts)
}
// ParseStruct parses a struct
func ParseStruct(src []byte, structName string, copyDocuments bool, copyTypeDocuments bool, pkgName string) (methods []Method, imports []string, typeDoc string) {
var file *ast.File
var e error
fset := token.NewFileSet()
if file, e = parser.ParseFile(fset, "", src, parser.ParseComments); e != nil {
log.Fatal(e.Error())
}
for _, i := range file.Imports {
if i.Name != nil {
imports = append(imports, fmt.Sprintf("%s %s", i.Name.String(), i.Path.Value))
} else {
imports = append(imports, fmt.Sprintf("%s", i.Path.Value))
}
}
for _, d := range file.Decls {
if a, decl := GetReceiverTypeName(src, d); a == structName {
if !decl.Name.IsExported() {
continue
}
params := FormatFieldList(src, decl.Type.Params, pkgName)
ret := FormatFieldList(src, decl.Type.Results, pkgName)
method := fmt.Sprintf("%s(%s) (%s)", decl.Name.String(), strings.Join(params, ", "), strings.Join(ret, ", "))
var Documents []string
if decl.Doc != nil && copyDocuments {
for _, d := range decl.Doc.List {
Documents = append(Documents, string(src[d.Pos()-1:d.End()-1]))
}
}
methods = append(methods, Method{
Code: method,
Documents: Documents,
})
}
}
if copyTypeDocuments {
pkg := &ast.Package{Files: map[string]*ast.File{"": file}}
doc := doc.New(pkg, "", doc.AllDecls)
for _, t := range doc.Types {
if t.Name == structName {
typeDoc = strings.TrimSuffix(t.Doc, "\n")
}
}
}
return
}
// Method describes code and Documents for a method
type Method struct {
Code string
Documents []string
}
// Lines return a a slice of documentation and code
func (m *Method) Lines() []string {
var lines []string
lines = append(lines, m.Documents...)
lines = append(lines, m.Code)
return lines
}
// FormatFieldList returns a list of formatted list of fields
func FormatFieldList(src []byte, fieldList *ast.FieldList, pkgName string) (fields []string) {
if fieldList == nil {
return
}
// Loop through the list of fields in the field list
for _, l := range fieldList.List {
names := make([]string, len(l.Names))
for i, n := range l.Names {
names[i] = n.Name
}
t := string(src[l.Type.Pos()-1 : l.Type.End()-1])
t = strings.Replace(t, pkgName+".", "", -1)
if len(names) > 0 {
typeSharingArgs := strings.Join(names, ", ")
fields = append(fields, fmt.Sprintf("%s %s", typeSharingArgs, t))
} else {
fields = append(fields, t)
}
}
return fields
}
// GetFuncDeclarationReceiverType returns the receiver type of a function declaration
func GetFuncDeclarationReceiverType(fd *ast.FuncDecl) (expr ast.Expr, e error) {
if fd.Recv == nil {
e = fmt.Errorf("fd is a function, not a method")
return
}
expr = fd.Recv.List[0].Type
return
}
// GetReceiverTypeName returns the name of the receiver type and the declaration
func GetReceiverTypeName(src []byte, fl interface{}) (name string, funcDef *ast.FuncDecl) {
var ok bool
var expr ast.Expr
var e error
if funcDef, ok = fl.(*ast.FuncDecl); !ok {
return
}
if expr, e = GetFuncDeclarationReceiverType(funcDef); e != nil {
return
}
name = string(src[expr.Pos()-1 : expr.End()-1])
if len(name) > 0 && name[0] == '*' {
name = name[1:]
}
return
}