/
config.go
198 lines (166 loc) · 4.95 KB
/
config.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
package autodoc
import (
"mime/multipart"
"reflect"
"unsafe"
"net/http"
"encoding/json"
log "github.com/sirupsen/logrus"
"strings"
)
// 杂项配置
var (
// c.JSON 时,若传入的数据有零值,可以将其递归地填充为非零值(数值填充成 1,字符串填充成 "1",布尔值依然为 false)
FillZeroValue = false
SkipGen = "[skip gen]"
// 当返回值不为 200 时,打印 warning 信息
WarningWhenNotStatusOK = true
// 字段缺少描述时打印 warning 信息
WarningMissingFields = true
)
// Markdown 相关配置
var (
DefaultMarkdownHeader = `# 接口文档
## HTTP 接口`
DefaultMarkdownFooter = ""
DefaultFormFileDesc = "上传的文件"
)
// 默认 echo 操作返回值
// 若要精确设置,见下方的 AddCustomGetReturn 等
var (
DefaultCookie = http.Cookie{Value: "1"}
DefaultGetReturn interface{} = 1
DefaultQueryParamReturn = "1"
DefaultFormValueReturn = "1"
// TODO: DefaultPostJSONFieldValue interface{} = "1"
DefaultMultipartFileHeader = multipart.FileHeader{Filename: "example.txt", Size: 1}
DefaultMultipartFileHeaderContent = []byte("A")
)
func init() {
ModifyFileHeaderContent(&DefaultMultipartFileHeader, DefaultMultipartFileHeaderContent)
}
func ModifyFileHeaderContent(fh *multipart.FileHeader, content []byte) {
fhVal := reflect.Indirect(reflect.ValueOf(fh))
ptrToContent := (*[]byte)(unsafe.Pointer(fhVal.FieldByName("content").UnsafeAddr()))
*ptrToContent = content
}
// 自定义类型识别
var (
customGoTypeToJSONType = map[string]string{
"time.Time": "string",
"HashID": "string",
"JSONTime": "string",
}
)
func AddCustomGoTypeToJSONType(goType string, jsonType string) {
customGoTypeToJSONType[goType] = jsonType
}
// 精确设置 echo 操作返回值
var (
customContextGetReturnMap = map[string]interface{}{}
customQueryParamReturnMap = map[string]string{}
customFormValueReturnMap = map[string]string{}
// TODO: customPostJSONFieldValueMap = map[string]interface{}{}
)
func AddCustomGetReturn(key string, ret interface{}) {
customContextGetReturnMap[key] = ret
}
func AddCustomContextGetReturn(key string, ret interface{}) {
customContextGetReturnMap[key] = ret
}
func AddCustomQueryParamReturn(name string, str string) {
customQueryParamReturnMap[name] = str
}
func AddCustomFormValueReturn(name string, str string) {
customFormValueReturnMap[name] = str
}
// 解析参数时,若没有相关注释,使用下面的值(一般用于常见的入参出参)
var (
customContextGetParams = map[string]Param{}
customQueryParams = map[string]Param{}
customFormParams = map[string]Param{}
customPostJSONParams = map[string]Param{}
customResponseJSONParams = map[string]Param{}
globalResponseJSONParams []Param
ignoredResponseJSONParams = map[string]Param{}
)
func SetContextGetParams(params ...Param) {
for _, p := range params {
customContextGetParams[p.Name] = p
}
}
func SetQueryParams(params ...Param) {
for _, p := range params {
customQueryParams[p.Name] = p
}
}
func SetFormParams(params ...Param) {
for _, p := range params {
customFormParams[p.Name] = p
}
}
func SetPostJSONParams(params ...Param) {
for _, p := range params {
customPostJSONParams[p.Name] = p
}
}
func SetResponseJSONParams(params ...Param) {
for _, p := range params {
customResponseJSONParams[p.Name] = p
}
}
// globalResponseJSONParams 为所有返回的 JSON 都会有的字段
// 同时会设置 ignoredResponseJSONParams
func SetGloablResponseJSONParams(params ...Param) {
globalResponseJSONParams = append(globalResponseJSONParams, params...)
for _, p := range params {
ignoredResponseJSONParams[p.Name] = p
}
}
// 解析返回的 JSON 时,忽略这些字段
func SetIgnoredResponseJSONParams(params ...Param) {
for _, p := range params {
ignoredResponseJSONParams[p.Name] = p
}
}
//
type ContextJSON interface {
BeforeJSON(code int, i interface{})
AfterJSON(code int, i interface{})
}
var (
ContextJSONer ContextJSON = ErrorCodeContextJSONer
)
var (
EmptyContextJSONer = &emptyContextJSON{}
ErrorCodeContextJSONer = &errorCodeContextJSON{1000}
)
type emptyContextJSON struct{}
func (*emptyContextJSON) BeforeJSON(code int, i interface{}) {}
func (*emptyContextJSON) AfterJSON(code int, i interface{}) {}
type errorCodeContextJSON struct {
errorCodeOK int
}
func (cj *errorCodeContextJSON) BeforeJSON(code int, i interface{}) {
if code == http.StatusOK {
data, err := json.Marshal(i)
if err != nil {
return
}
if !strings.Contains(string(data), `"errcode"`) {
// log.Warn()
return
}
d := struct {
ErrCode int `json:"errcode"`
}{}
if err := json.Unmarshal(data, &d); err != nil {
log.WithError(err).Errorln("[c.JSON.BeforeJSON.json.Unmarshal]")
return
}
if d.ErrCode != cj.errorCodeOK {
log.WithError(err).Errorf("[c.JSON.BeforeJSON] d.ErrCode != cj.errorCodeOK (%d != %d)", d.ErrCode, cj.errorCodeOK)
}
}
}
func (*errorCodeContextJSON) AfterJSON(code int, i interface{}) {}