-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc_markdown.go
202 lines (184 loc) · 5.75 KB
/
doc_markdown.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
package pick
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
"strings"
"time"
reflecti "github.com/hopeio/utils/reflect"
"github.com/hopeio/utils/reflect/mock"
stringsi "github.com/hopeio/utils/strings"
"github.com/hopeio/utils/validation/validator"
)
type ApiDocInfo struct {
ApiInfo *apiInfo
Method reflect.Type
}
type GroupApiInfo struct {
Describe string
Infos []*ApiDocInfo
}
var groupApiInfos []*GroupApiInfo
func RegisterApiInfo(apiInfo *GroupApiInfo) {
groupApiInfos = append(groupApiInfos, apiInfo)
}
// 有swagger,有没有必要做
func Markdown(filePath, modName string) {
buf, err := genFile(filePath, modName)
if err != nil {
log.Println(err)
}
defer buf.Close()
fmt.Fprintln(buf, "[TOC]")
if modName != "" {
fmt.Fprintf(buf, "# %s接口文档 \n", modName)
fmt.Fprintln(buf, "----------")
}
for _, groupApiInfo := range groupApiInfos {
fmt.Fprintf(buf, "# %s \n", groupApiInfo.Describe)
fmt.Fprintln(buf, "----------")
for _, methodInfo := range groupApiInfo.Infos {
//title
apiinfo := methodInfo.ApiInfo
if apiinfo.deprecated != nil {
fmt.Fprintf(buf, "## ~~%s-v%d(废弃)(`%s`)~~ \n", apiinfo.title, apiinfo.version, apiinfo.path)
} else {
fmt.Fprintf(buf, "## %s-v%d(`%s`) \n", apiinfo.title, apiinfo.version, apiinfo.path)
}
//api
fmt.Fprintf(buf, "**%s** `%s` _(Principal %s)_ \n", apiinfo.method, apiinfo.path, apiinfo.getPrincipal())
fmt.Fprint(buf, "### 接口记录 \n")
fmt.Fprint(buf, "|版本|操作|时间|负责人|日志| \n")
fmt.Fprint(buf, "| :----: | :----: | :----: | :----: | :----: | \n")
fmt.Fprintf(buf, "|%s|%s|%s|%s|%s| \n", apiinfo.createlog.version, "创建", apiinfo.createlog.date, apiinfo.createlog.auth, apiinfo.createlog.log)
if len(apiinfo.changelog) != 0 || apiinfo.deprecated != nil {
for _, clog := range apiinfo.changelog {
fmt.Fprintf(buf, "|%s|%s|%s|%s|%s| \n", clog.version, "变更", clog.date, clog.auth, clog.log)
}
if apiinfo.deprecated != nil {
fmt.Fprintf(buf, "|%s|%s|%s|%s|%s| \n", apiinfo.deprecated.version, "删除", apiinfo.deprecated.date, apiinfo.deprecated.auth, apiinfo.deprecated.log)
}
}
fmt.Fprint(buf, "### 参数信息 \n")
if methodInfo.Method.NumIn() == 3 {
fmt.Fprint(buf, "|字段名称|字段类型|字段描述|校验要求| \n")
fmt.Fprint(buf, "| :---- | :----: | :----: | :----: | \n")
params := getParamTable(methodInfo.Method.In(2).Elem(), "")
for i := range params {
fmt.Fprintf(buf, "|%s|%s|%s|%s| \n", params[i].json, params[i].typ, params[i].comment, params[i].validator)
}
} else {
fmt.Fprint(buf, "无需参数")
}
fmt.Fprint(buf, "__请求示例__ \n")
fmt.Fprint(buf, "```json \n")
newParam := reflect.New(methodInfo.Method.In(2).Elem()).Interface()
mock.Mock(newParam)
data, _ := json.MarshalIndent(newParam, "", "\t")
fmt.Fprint(buf, string(data), " \n")
fmt.Fprint(buf, "``` \n")
fmt.Fprint(buf, "### 返回信息 \n")
fmt.Fprint(buf, "|字段名称|字段类型|字段描述| \n")
fmt.Fprint(buf, "| :---- | :----: | :----: | \n")
params := getParamTable(methodInfo.Method.Out(0).Elem(), "")
for i := range params {
fmt.Fprintf(buf, "|%s|%s|%s| \n", params[i].json, params[i].typ, params[i].comment)
}
fmt.Fprint(buf, "__返回示例__ \n")
fmt.Fprint(buf, "```json \n")
newRes := reflect.New(methodInfo.Method.Out(0).Elem()).Interface()
mock.Mock(newRes)
data, _ = json.MarshalIndent(newRes, "", "\t")
fmt.Fprint(buf, string(data), " \n")
fmt.Fprint(buf, "``` \n")
}
}
}
func genFile(filePath, modName string) (*os.File, error) {
filePath = filePath + modName
err := os.MkdirAll(filePath, os.ModePerm)
if err != nil {
return nil, err
}
filePath = filepath.Join(filePath, modName+".apidoc.md")
if _, err := os.Stat(filePath); err == nil {
os.Remove(filePath)
}
var file *os.File
file, err = os.Create(filePath)
if err != nil {
return nil, err
}
return file, nil
}
type ParamTable struct {
json, comment, typ, validator string
}
func getParamTable(param reflect.Type, pre string) []*ParamTable {
param = reflecti.DerefType(param)
newParam := reflect.New(param).Interface()
var res []*ParamTable
for i := 0; i < param.NumField(); i++ {
/* if param.AssignableTo(reflect.TypeOf(response.File{})) {
return "下载文件"
}*/
var p ParamTable
field := param.Field(i)
if field.Anonymous {
continue
}
json := strings.Split(field.Tag.Get("json"), ",")[0]
if json == "-" {
continue
}
if json == "" {
p.json = pre + stringsi.SnakeToCamel(json)
} else {
p.json = pre + json
}
p.comment = field.Tag.Get("comment")
if p.comment == "-" {
p.comment = p.json
}
p.typ = getJsType(field.Type)
if valid := validator.Trans(validator.Validator.StructPartial(newParam, field.Name)); valid != "" {
p.validator = valid[len(p.comment):]
}
if p.typ == "object" || p.typ == "[]object" {
p.json = "**" + p.json + "**"
res = append(res, &p)
sub := getParamTable(field.Type, json+".")
res = append(res, sub...)
} else {
res = append(res, &p)
}
}
return res
}
func getJsType(typ reflect.Type) string {
t := time.Time{}
if typ == reflect.TypeOf(t) || typ == reflect.TypeOf(&t) {
return "date"
}
switch typ.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return "number"
case reflect.Array, reflect.Slice:
if typ.Elem().Kind() == reflect.Uint8 {
return "string"
}
return "[]" + getJsType(typ.Elem())
case reflect.Ptr:
return getJsType(typ.Elem())
case reflect.Struct:
return "object"
case reflect.Bool:
return "boolean"
}
return "string"
}