-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
187 lines (152 loc) · 3.29 KB
/
doc.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
package gos
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"reflect"
"strings"
"sync"
"text/template"
"github.com/russross/blackfriday/v2"
"golang.org/x/text/encoding/simplifiedchinese"
)
//go:embed embed/doc.html
var docHtml string
//go:embed embed/doc_index.gohtml
var docIndexHtml string
//go:embed embed/doc.gomd
var docMd string
var mdTpl *template.Template
func init() {
mdTpl = template.Must(
template.New("").Funcs(
template.FuncMap{
"json": func(value interface{}) string {
d, _ := json.MarshalIndent(value, "", " ")
return string(d)
},
"form": func(value interface{}) string {
s := formEncode(value)
return strings.ReplaceAll(s, "&", "\n&")
},
},
).Parse(docMd),
)
}
type Doc struct {
Name string //名称
Description string //描述
Path string //路径
Errors map[int]M
Input interface{}
Output interface{}
InputParams []DocParamItem
parsed bool
sync.Mutex
}
type DocParamItem struct {
Name string //名称
Type string //类型
Description string //描述
Required bool //是否必须
Example string //Example
}
type Empty struct {
Code string `json:"code,omitempty"`
}
func (ad *Doc) parse() {
ad.Lock()
defer ad.Unlock()
if ad.parsed {
return
}
ad.parsed = true
if ad.Input == nil || len(ad.InputParams) > 0 {
return
}
ad.InputParams = parseParams(reflect.ValueOf(ad.Input))
}
func (ad *Doc) Markdown(path string) (md []byte, html string) {
ad.parse()
ad.Path = path
var buf bytes.Buffer
if err := mdTpl.Execute(&buf, ad); err != nil {
return []byte(fmt.Sprintf("`%s`", err.Error())), err.Error()
}
md = buf.Bytes()
html = strings.NewReplacer(
"{{ .Title }}", ad.Name,
"{{ .Body }}", string(blackfriday.Run(md)),
).Replace(docHtml)
return
}
func parseParams(vRef reflect.Value) []DocParamItem {
var params []DocParamItem
tRef := vRef.Type()
for i := 0; i < vRef.NumField(); i++ {
fieldT := tRef.Field(i)
fieldV := vRef.Field(i)
if fieldT.Anonymous {
params = append(params, parseParams(fieldV)...)
continue
}
var item DocParamItem
tag := fieldT.Tag.Get("doc")
item.Description = tag
jTag := fieldT.Tag.Get("json")
if jTag == "-" {
continue
}
if jTags := strings.SplitN(jTag, ",", 2); len(jTags) > 0 {
item.Name = strings.TrimSpace(jTags[0])
}
if item.Name == "" {
item.Name = fieldT.Name
}
vTag := fieldT.Tag.Get("validate")
item.Required = strings.Contains(vTag, "required") || !strings.Contains(jTag, ",omitempty")
v, _ := json.Marshal(fieldV.Interface())
item.Example = string(v)
item.Type = fieldV.Kind().String()
params = append(params, item)
}
return params
}
func wrapDoc(v interface{}) *Doc {
switch o := v.(type) {
case func() *Doc:
return o()
case func(*Doc):
docH := &Doc{}
o(docH)
return docH
case Doc:
return &o
case *Doc:
return o
}
return nil
}
var gb18130 = simplifiedchinese.GB18030.NewEncoder()
type docNs []*Doc
func (g docNs) Len() int {
return len(g)
}
func (g docNs) Less(i, j int) bool {
a, _ := gb18130.Bytes([]byte(g[i].Name))
b, _ := gb18130.Bytes([]byte(g[j].Name))
l := len(b)
for idx, chr := range a {
if idx > l-1 {
return false
}
if chr != b[idx] {
return chr < b[idx]
}
}
return true
}
func (g docNs) Swap(i, j int) {
g[i], g[j] = g[j], g[i]
}