forked from ghetzel/diecast
/
main.go
222 lines (179 loc) · 5.03 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"github.com/PerformLine/diecast"
"github.com/PerformLine/go-stockutil/rxutil"
"github.com/PerformLine/go-stockutil/typeutil"
)
var rxFnDocString = `//\s*fn\s*(?P<func>[^:]+):\s*(?P<docstring>.*)`
type docArg struct {
Arg string
Type reflect.Type
}
type functionDoc struct {
Name string
DocString string
Signature string
Returns string
}
type functionDocSet []*functionDoc
func (self functionDocSet) Len() int {
return len(self)
}
func (self functionDocSet) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (self functionDocSet) Less(i, j int) bool {
return self[i].Name < self[j].Name
}
func GenerateFunctionDocs(funcs diecast.FuncMap, sourcefile string) (functionDocSet, error) {
var docs = make(functionDocSet, 0)
if source, err := parser.ParseFile(token.NewFileSet(), sourcefile, nil, parser.ParseComments); err == nil {
NextComment:
for _, group := range source.Comments {
var doc = &functionDoc{}
for _, comment := range group.List {
if match := rxutil.Match(rxFnDocString, comment.Text); match != nil {
var fnname = match.Group(`func`)
var docstring = match.Group(`docstring`)
// if the comment refers to a function we know about, and the docstring
// portion is not empty, start building the functionDoc struct
if fn, ok := funcs[fnname]; ok && docstring != `` {
var argNames []string
if an := rxutil.Match(`(?:\*(\w+)\*)+`, docstring); an != nil {
argNames = an.AllCaptures()
}
if signature, outputs, err := getFnSignature(fn, argNames); err == nil {
doc.Name = fnname
doc.DocString = docstring
doc.Signature = signature
doc.Returns = outputs
} else {
fmt.Printf("signature failed: %v\n", err)
continue NextComment
}
}
} else if doc.Name != `` {
// if doc.Name is set, then we're nigh on a multi-line docstring, so append it.
doc.DocString += ` ` + strings.TrimSpace(
strings.TrimPrefix(comment.Text, `//`),
)
} else {
continue NextComment
}
}
docs = append(docs, doc)
}
return docs, nil
} else {
return nil, err
}
}
func getFnSignature(fn interface{}, inArgNames []string) (string, string, error) {
fn = typeutil.ResolveValue(fn)
if typeutil.IsKind(fn, reflect.Func) {
var args []string
var outs []string
var fnT = reflect.TypeOf(fn)
// figure out input arguments
for in := 0; in < fnT.NumIn(); in++ {
var inT = fnT.In(in)
var typename = inT.Name()
switch typename {
case `interface{}`, ``:
typename = `any`
}
if fnT.IsVariadic() && (in+1) == fnT.NumIn() {
typename = `[` + typename + ` ..]`
}
if in < len(inArgNames) {
args = append(args, fmt.Sprintf("%s %s", inArgNames[in], typename))
} else {
args = append(args, typename)
}
}
// figure out output arguments
for o := 0; o < fnT.NumOut(); o++ {
var outT = fnT.Out(o)
var typename = outT.Name()
switch typename {
case `interface{}`:
typename = `any`
}
outs = append(outs, typename)
}
var inArgs = strings.Join(args, ` `)
var outArgs = strings.Join(outs, ` `)
return inArgs, outArgs, nil
} else {
return ``, ``, fmt.Errorf("must provide a function to get a signature")
}
}
func main() {
if f, err := os.Open(`docs/functions_pre.md`); err == nil {
defer f.Close()
if data, err := ioutil.ReadAll(f); err == nil {
fmt.Printf("%s\n", string(data))
}
}
var docs functionDocSet
var standardFuncs = diecast.GetStandardFunctions(nil)
if funcFiles, err := filepath.Glob(`functions_defs_*.go`); err == nil {
for _, funcdefs := range funcFiles {
if d, err := GenerateFunctionDocs(standardFuncs, funcdefs); err == nil {
docs = append(docs, d...)
} else {
fmt.Printf("err: %v\n", err)
os.Exit(1)
}
}
} else {
fmt.Printf("err: %v\n", err)
os.Exit(1)
}
if d, err := GenerateFunctionDocs(new(diecast.Server).GetTemplateFunctions(nil, nil), `server.go`); err == nil {
docs = append(docs, d...)
} else {
fmt.Printf("err: %v\n", err)
os.Exit(1)
}
sort.Sort(docs)
fmt.Printf("## Function List\n\n")
for _, doc := range docs {
fmt.Printf("- [%s](#%s)\n", doc.Name, doc.Name)
}
fmt.Printf("## Function Usage\n\n")
for _, doc := range docs {
var returnSignature = doc.Returns
if returnSignature != `` {
var outSig = strings.Split(returnSignature, `,`)
if outSig[len(outSig)-1] == `error` {
outSig = outSig[:len(outSig)-1]
}
returnSignature = strings.Join(outSig, ` `)
if len(outSig) > 1 {
returnSignature = `(` + returnSignature + `)`
}
}
fmt.Printf("---\n\n")
fmt.Printf("<a name=\"%s\"></a>\n", doc.Name)
fmt.Printf("```\n")
fmt.Printf("%s %s -> %s\n", doc.Name, doc.Signature, returnSignature)
fmt.Printf("```\n")
fmt.Printf("%s\n\n", doc.DocString)
}
if f, err := os.Open(`docs/functions_post.md`); err == nil {
defer f.Close()
if data, err := ioutil.ReadAll(f); err == nil {
fmt.Printf("%s\n", string(data))
}
}
}