/
main.go
178 lines (151 loc) · 3.65 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"os"
"strings"
)
func listFunctions(filePath string, shorten bool) {
fset := token.NewFileSet()
// Parse the file
file, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments+parser.AllErrors)
if err != nil {
fmt.Println(err)
return
}
// Iterate through the declarations and print function names
for _, decl := range file.Decls {
if f, ok := decl.(*ast.FuncDecl); ok {
// Get the doc comments for the function
c := f.Doc
if c == nil {
continue
}
comment := strings.TrimSpace(c.Text())
// relevant?
if !strings.HasPrefix(comment, "Doc: ") {
continue
}
// shorten?
result := f.Name.Name
if shorten {
result = shortenFuncName(result)
}
fmt.Println(result)
}
}
}
func getFunctionDoc(filePath, functionName string) {
file, err := getFileSet(filePath)
if err != nil {
errOut(err, 1)
}
// Iterate through the declarations to find the target function
for _, decl := range file.Decls {
if f, ok := decl.(*ast.FuncDecl); ok && f.Name.Name == functionName {
// Get the doc comments for the function
fmt.Println(strings.TrimPrefix(strings.TrimSpace(f.Doc.Text()), "Doc: "))
return
}
}
}
type FuncParamData struct {
Name string
Type string
Doc *ast.CommentGroup
}
type FuncResultData struct {
Name string
Type string
Doc *ast.CommentGroup
}
type FuncData struct {
TemplateName string
FunctionName string
Doc string
Params []FuncParamData
Results []FuncResultData
}
func printFunctionData(filePath string) {
result := make([]FuncData, 0)
file, err := getFileSet(filePath)
if err != nil {
errOut(err, 1)
}
const prefix = "Doc: "
// iterate through the relevant function declarations
for _, decl := range file.Decls {
if fn, ok := decl.(*ast.FuncDecl); ok {
if !strings.HasPrefix(fn.Doc.Text(), prefix) {
continue
}
params := make([]FuncParamData, 0, 3)
for _, p := range fn.Type.Params.List {
// https://stackoverflow.com/questions/50524607/go-lang-func-parameter-type/50525439#50525439
for i := 0; i < len(p.Names); i++ {
param := FuncParamData{
Doc: p.Doc,
Name: p.Names[i].Name,
Type: types.ExprString(p.Type),
}
params = append(params, param)
}
}
results := make([]FuncResultData, 0, 2)
for _, p := range fn.Type.Results.List {
// https://stackoverflow.com/questions/50524607/go-lang-func-parameter-type/50525439#50525439
// named?
names := []*ast.Ident{
{
NamePos: 0,
Name: "",
Obj: nil,
},
}
if len(p.Names) > 0 {
names = p.Names
}
for i := 0; i < len(names); i++ {
result := FuncResultData{
Doc: p.Doc,
Name: names[i].Name,
Type: types.ExprString(p.Type),
}
results = append(results, result)
}
}
f := FuncData{
TemplateName: shortenFuncName(fn.Name.Name),
FunctionName: fn.Name.Name,
Doc: strings.TrimPrefix(strings.TrimSpace(fn.Doc.Text()), prefix),
Params: params,
Results: results,
}
result = append(result, f)
}
}
json.NewEncoder(os.Stdout).Encode(result)
}
func errOut(err error, exitCode int) {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(exitCode)
}
func getFileSet(filePath string) (*ast.File, error) {
fileSet := token.NewFileSet()
return parser.ParseFile(fileSet, filePath, nil, parser.ParseComments+parser.AllErrors)
}
func shortenFuncName(input string) string {
return strings.TrimSuffix(input, "Func")
}
func main() {
command := os.Args[1]
switch command {
case "list_funcs":
printFunctionData(os.Args[2])
}
os.Exit(1)
}