This repository has been archived by the owner on Aug 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_helpers.go
193 lines (175 loc) · 5.23 KB
/
template_helpers.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
package godocmd
import (
"bytes"
"fmt"
"go/ast"
"go/doc"
"go/printer"
"go/token"
"path/filepath"
"strings"
"text/template"
)
type Package struct {
*doc.Package
// FuncsFiltered filtered out examples and benchmarks
FuncsFiltered []*doc.Func
FuncsName map[string]*doc.Func
}
func NewPackage(mainPkg, testPkg *doc.Package) *Package {
pkg := &Package{mainPkg,
make([]*doc.Func, 0),
make(map[string]*doc.Func)}
pkg.init(testPkg)
return pkg
}
func filter(ary []*doc.Func, f func(fun *doc.Func) bool) []*doc.Func {
fs := make([]*doc.Func, 0)
for _, a := range ary {
if f(a) {
fs = append(fs, a)
}
}
return fs
}
func stringStartsWith(in string, patterns []string) bool {
for _, p := range patterns {
if strings.HasPrefix(in, p) {
return true
}
}
return false
}
func (pkg *Package) init(testPkg *doc.Package) {
pkg.FuncsFiltered = filter(pkg.Funcs, func(fun *doc.Func) bool {
pkg.FuncsName[fun.Name] = fun
return !stringStartsWith(fun.Name, []string{"Benchmark", "Test", "Example"})
})
if testPkg != nil {
for _, f := range testPkg.Funcs {
if stringStartsWith(f.Name, []string{"Example"}) {
pkg.FuncsName[f.Name] = f
}
}
}
}
func templateFuncMap(fset *token.FileSet, pkg *Package, workingDir, outfile string) *template.FuncMap {
return &template.FuncMap{
"functionSignature": functionSignature(fset),
"anyTypeSourceString": anyTypeSourceString(fset),
"sourceFileLink": sourceFileLink(fset, workingDir, outfile),
"anchorFunc": functionAnchor(fset),
"getExampleForFunc": getExampleForFunc(pkg),
"getOtherExamplesForType": getOtherExamplesForType(pkg),
"codeBlock": func() string { return "```" },
"codeBlockGolang": func() string { return "```go" },
}
}
func anyTypeSourceStringFunc(fset *token.FileSet, _type interface{}) string {
var buffer bytes.Buffer
printer.Fprint(&buffer, fset, _type)
return buffer.String()
}
func anyTypeSourceString(fset *token.FileSet) func(interface{}) string {
return func(_type interface{}) string {
return anyTypeSourceStringFunc(fset, _type)
}
}
func functionSignature(fset *token.FileSet) func(*doc.Func) string {
return func(f *doc.Func) string {
functionGet := functionParam(fset)
return fmt.Sprintf("func %s%s%s", f.Decl.Name.Name, functionGet("params", f.Decl), functionGet("results", f.Decl))
}
}
// functionParam from ast.FuncDecl, this is for both parameters, and results
func functionParam(fset *token.FileSet) func(string, *ast.FuncDecl) string {
return func(_type string, decl *ast.FuncDecl) string {
var params []string
if decl != nil {
offset := decl.Pos()
if decl.Doc != nil {
offset = decl.Doc.Pos()
}
paramDeclString := anyTypeSourceStringFunc(fset, decl)
if fields, ok := map[string]*ast.FieldList{"params": decl.Type.Params, "results": decl.Type.Results}[_type]; ok && fields != nil {
for _, field := range fields.List {
fieldType := paramDeclString[field.Type.Pos()-offset : field.Type.End()-offset]
params = append(params, getParamNames(field.Names, fieldType))
}
}
}
if len(params) > 0 {
return fmt.Sprintf("(%s)", strings.Join(params, ", "))
} else if _type == "params" {
return "()"
}
return ""
}
}
// sourceFileLink generate links for the source file, need to rework base on relative path of the doc
func sourceFileLink(fset *token.FileSet, workingDir, outfile string) func(string) string {
outLevel := len(filepath.SplitList(outfile))
sourceRel := strings.Repeat("../", outLevel)
return func(_filepath string) string {
relPath, _ := filepath.Rel(workingDir, _filepath)
return fmt.Sprintf("[%s](%s%s)", filepath.Base(_filepath), sourceRel, relPath)
}
}
func functionAnchor(fset *token.FileSet) func(string, interface{}) string {
return func(linkType string, _f interface{}) string {
var name, objType string
switch f := _f.(type) {
case *doc.Type:
name = f.Name
objType = "type"
case *doc.Func:
name = f.Decl.Name.Name
objType = "func"
default:
panic(fmt.Errorf("unhandle type %v", _f))
}
if t, ok := map[string]string{
"link": strings.ToLower(fmt.Sprintf("#%s-%s", objType, name)),
"anchor": fmt.Sprintf("%s-%s", objType, name),
}[linkType]; ok {
return t
} else {
return ""
}
}
}
func getExampleForFunc(pkg *Package) func(*doc.Type, *doc.Func) []*doc.Func {
return func(t *doc.Type, f *doc.Func) []*doc.Func {
ret := make([]*doc.Func, 0)
for _, prefix := range []string{"Example", "Benchmark"} {
var funcKey string
switch {
case t != nil && f != nil:
funcKey = fmt.Sprintf("%s%s_%s", prefix, t.Name, f.Name)
case t == nil && f != nil:
funcKey = fmt.Sprintf("%s%s", prefix, f.Name)
case t != nil:
funcKey = fmt.Sprintf("%s%s", prefix, t.Name)
default:
return nil
}
if e, ok := pkg.FuncsName[funcKey]; ok {
ret = append(ret, e)
//delete (pkg.FuncsName, funcKey)
}
}
return ret
}
}
func getOtherExamplesForType(pkg *Package) func(*doc.Type) []*doc.Func {
return func(t *doc.Type) []*doc.Func {
ret := make([]*doc.Func, 0)
for key, e := range pkg.FuncsName {
if strings.HasPrefix(key, "Example"+t.Name) || strings.HasPrefix(key, "Benchmark"+t.Name) {
fmt.Printf("FUN %s\n", t.Name, e)
ret = append(ret, e)
}
}
return ret
}
}