-
Notifications
You must be signed in to change notification settings - Fork 0
/
apidoc.go
193 lines (163 loc) · 3.76 KB
/
apidoc.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 goapidoc
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"reflect"
"regexp"
"strconv"
"strings"
)
func New(api ApiDef) {
if api.Skip {
return
}
api.group = api.extractGroupFromRoute()
api.version = getVersion()
file := getFilePath(fmt.Sprintf("src/%s.js", api.group))
docs := parseDocs(file)
doc := docs.match(api)
olderDoc := *doc
if doc.ApiVersion != api.version {
doc = new(docDef)
docs = append(docs, doc)
}
doc.generateWithOldDoc(api, olderDoc)
docs.saveAs(file).toHtmlDoc(getFilePath("src/apidoc.json"), getFilePath("api"))
}
func (api ApiDef) extractFieldTypeFromResponse() map[string]string {
var data map[string]any
fieldMap := make(map[string]string)
err := json.Unmarshal(api.ResponseBody, &data)
if err == nil {
extractFieldTypes("", data, fieldMap)
}
return fieldMap
}
func (api ApiDef) extractGroupFromRoute() string {
// eg: /api/v1/user
matches := regexp.MustCompile(`/?(?:[\w-_]+/)*v\d+/([\w-_]+)/?`).FindStringSubmatch(api.Path)
if len(matches) > 1 {
return matches[1]
}
// eg: /api/user
matches = regexp.MustCompile(`/?(?:[\w-_]+/)?api/([\w-_]+)/?`).FindStringSubmatch(api.Path)
if len(matches) > 1 {
return matches[1]
}
// 使用字符串分割提取路径中的分组
pathParts := strings.Split(strings.Trim(api.Path, "/"), "/")
if len(pathParts) > 0 {
return pathParts[0]
}
return ""
}
func getVersion() string {
apidoc, err := os.ReadFile(getFilePath("src/apidoc.json"))
if err != nil {
slog.Warn("no version found, use default[0.0.1]. err: ", err)
return "0.0.1"
}
var ver struct {
Version string
}
err = json.Unmarshal(apidoc, &ver)
if err != nil {
slog.Warn("no version found, use default[0.0.1]. err: ", err)
return "0.0.1"
}
return ver.Version
}
func getFilePath(file string) string {
dirPath := []string{"doc/", "../doc/", "../../doc/", "template/"}
fullPath := ""
for _, path := range dirPath {
if _, err := os.Stat(path); err == nil {
fullPath = path
break
}
}
fullPath += file
return fullPath
}
func extractFieldTypes(prefix string, data any, fieldMap map[string]string) {
switch value := data.(type) {
case map[string]any:
for key, val := range value {
// {"data":xxxx} 这种格式默认跳过[data.]前缀
if key == "data" {
extractFieldTypes("", val, fieldMap)
continue
}
fieldName := strings.TrimPrefix(fmt.Sprintf("%s.%s", prefix, key), ".")
if _, ok := fieldMap[fieldName]; ok {
continue
}
var fieldType string
switch val.(type) {
case map[string]any:
fieldType = "Object"
case []any:
fieldType = ""
case string:
fieldType = "String"
case bool:
fieldType = "Boolean"
default:
fieldType = "Number"
}
fieldMap[fieldName] = fieldType
extractFieldTypes(fieldName, val, fieldMap)
}
case []any:
if len(value) == 0 && fieldMap[prefix] == "" {
fieldMap[prefix] = "[]"
}
for _, val := range value {
valType := reflect.TypeOf(val)
switch valType.Kind() {
case reflect.Map, reflect.Slice:
if fieldMap[prefix] == "" {
fieldMap[prefix] = "Object[]"
}
extractFieldTypes(prefix, val, fieldMap)
default:
if fieldMap[prefix] == "" {
switch val.(type) {
case string:
fieldMap[prefix] = "String[]"
default:
fieldMap[prefix] = "Number[]"
}
}
}
}
}
}
func generateParamDef(key, val, typ string) paramDef {
if typ == "" {
typ = "String"
if i, err := strconv.Atoi(val); err != nil && i > 0 {
typ = "Number"
}
}
// 移除Bearer
if strings.Contains(val, "Bearer") || strings.Contains(val, "bearer") {
val = "Bearer "
}
param := paramDef{
Type: typeDef{
Type: typ,
Size: "",
Allows: nil,
},
Field: fieldDef{
Name: key,
Required: false,
Default: val,
},
Description: "",
}
return param
}