-
Notifications
You must be signed in to change notification settings - Fork 108
/
json.go
348 lines (295 loc) · 7.75 KB
/
json.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// SPDX-License-Identifier: MIT
package mock
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"strings"
"github.com/issue9/errwrap"
"github.com/issue9/validation/is"
"github.com/caixw/apidoc/v7/core"
"github.com/caixw/apidoc/v7/internal/ast"
"github.com/caixw/apidoc/v7/internal/locale"
)
type jsonValidator struct {
param *ast.Param
// 按顺序表示的状态
// 可以是 [ 表示在数组中,{ 表示在对象,: 表示下一个值必须是属性,空格表示其它状态
states []byte
names []string // 按顺序保存变量名称
}
func validJSON(p *ast.Request, content []byte) error {
if p == nil {
if bytes.Equal(content, []byte("null")) {
return nil
}
return core.NewError(locale.ErrInvalidFormat)
} else if p.Type.V() == ast.TypeNone && len(content) == 0 {
return nil
}
if !json.Valid(content) {
return core.NewError(locale.ErrInvalidFormat)
}
validator := newJSONValidator(p)
return validator.valid(json.NewDecoder(bytes.NewReader(content)))
}
func newJSONValidator(r *ast.Request) *jsonValidator {
return &jsonValidator{
param: r.Param(),
states: []byte{0}, // 状态有默认值
names: []string{},
}
}
func (validator *jsonValidator) valid(d *json.Decoder) error {
for {
token, err := d.Token()
if errors.Is(err, io.EOF) && token == nil { // 正常结束
return nil
}
if err != nil {
return err
}
if token == nil { // 对应 JSON null
return nil
}
switch v := token.(type) {
case string: // json string
switch validator.state() {
case ':': // 字符串类型的值
err = validator.validValue(ast.TypeString, v)
validator.popState()
validator.popName()
case '[':
err = validator.validValue(ast.TypeString, v)
case 0: // 表示数据为单个值,比如 "str"
err = validator.validValue(ast.TypeString, v)
// case ']', '}': // 格式错误,由 json.Valid 保证
default: // case '{' 属性名
validator.pushState(':')
validator.pushName(v)
}
if err != nil {
return err
}
case json.Delim: // [、]、{、}
switch v {
case '[':
validator.pushState('[')
case ']':
validator.popName()
validator.popState()
if validator.state() == ':' { // {xx: [] } 类似这种格式,需要同时弹出两个状态
validator.popState()
}
case '{':
validator.pushState('{')
case '}':
validator.popName()
validator.popState()
if validator.state() == ':' {
validator.popState()
}
}
case bool: // json bool
err = validator.validValue(ast.TypeBool, v)
if validator.state() != '[' {
validator.popState()
validator.popName()
}
case float64, json.Number: // json number
err = validator.validValue(ast.TypeNumber, v)
if validator.state() != '[' { // 只有键值对结束时,才弹出键名
validator.popState()
validator.popName()
}
}
if err != nil {
return err
}
}
}
var jsonNumberType = reflect.TypeOf(json.Number("1"))
// 如果 t == "" 表示不需要验证类型,比如 null 可以赋值给任何类型
func (validator *jsonValidator) validValue(t string, v interface{}) error {
field := strings.Join(validator.names, ".")
p := validator.find()
if p == nil {
return core.NewError(locale.ErrNotFound).WithField(field)
}
pt := p.Type.V()
if primitive, _ := ast.ParseType(pt); primitive != t {
return core.NewError(locale.ErrInvalidFormat).WithField(field)
}
switch pt {
case ast.TypeEmail:
if !is.Email(v) {
return core.NewError(locale.ErrInvalidFormat).WithField(field)
}
case ast.TypeURL:
if !is.URL(v) {
return core.NewError(locale.ErrInvalidFormat).WithField(field)
}
case ast.TypeDate:
vv, ok := v.(string)
if !ok {
return core.NewError(locale.ErrInvalidFormat).WithField(field)
}
if !isValidRFC3339Date(vv) {
return core.NewError(locale.ErrInvalidFormat).WithField(field)
}
case ast.TypeTime:
vv, ok := v.(string)
if !ok {
return core.NewError(locale.ErrInvalidFormat).WithField(field)
}
if !isValidRFC3339Time(vv) {
return core.NewError(locale.ErrInvalidFormat).WithField(field)
}
case ast.TypeDateTime:
vv, ok := v.(string)
if !ok {
return core.NewError(locale.ErrInvalidFormat).WithField(field)
}
if !isValidRFC3339DateTime(vv) {
return core.NewError(locale.ErrInvalidFormat).WithField(field)
}
case ast.TypeImage: // 可能是相对站点的根路径,不作类型检测
case ast.TypeInt, ast.TypeFloat: // 数值类型都被 json 解释为 float64,无法判断值是浮点还是整数。
}
if isEnum(p) {
for _, enum := range p.Enums {
if enum.Value.V() == fmt.Sprint(v) {
return nil
}
}
return core.NewError(locale.ErrInvalidValue).WithField(field)
}
return nil
}
// 返回当前的状态
func (validator *jsonValidator) state() byte {
if len(validator.states) > 0 {
return validator.states[len(validator.states)-1]
}
return 0
}
func (validator *jsonValidator) pushState(state byte) {
validator.states = append(validator.states, state)
}
func (validator *jsonValidator) popState() {
if len(validator.states) > 0 {
validator.states = validator.states[:len(validator.states)-1]
}
}
func (validator *jsonValidator) pushName(name string) {
validator.names = append(validator.names, name)
}
func (validator *jsonValidator) popName() {
if len(validator.names) > 0 {
validator.names = validator.names[:len(validator.names)-1]
}
}
// 如果 names 为空,返回 validator.param
func (validator *jsonValidator) find() *ast.Param {
p := validator.param
LOOP:
for _, name := range validator.names {
for _, pp := range p.Items {
if pp.Name.V() == name {
p = pp
continue LOOP
}
}
return nil
}
return p
}
type jsonBuilder struct {
w *errwrap.Buffer
deep int
indent string // 单次的缩进
}
func buildJSON(p *ast.Request, indent string, g *GenOptions) ([]byte, error) {
if p != nil && p.Type.V() == ast.TypeNone {
return nil, nil
}
builder := &jsonBuilder{
w: &errwrap.Buffer{},
indent: indent,
}
if err := builder.encode(p.Param(), true, g); err != nil {
return nil, err
}
return builder.w.Bytes(), nil
}
func (builder *jsonBuilder) encode(p *ast.Param, chkArray bool, g *GenOptions) error {
if p == nil {
return builder.writeValue(nil).w.Err
}
if p.Array.V() && chkArray {
builder.w.WString("[\n")
builder.deep++
size := g.generateSliceSize()
last := size - 1
for i := 0; i < size; i++ {
if err := builder.writeIndent().encode(p, false, g); err != nil {
return err
}
if i < last {
builder.w.WString(",\n")
} else {
builder.w.WString("\n")
}
}
builder.deep--
return builder.writeIndent().w.WString("]").Err
}
switch primitive, _ := ast.ParseType(p.Type.V()); primitive {
case ast.TypeNone:
builder.writeValue(nil)
case ast.TypeBool:
builder.writeValue(g.generateBool())
case ast.TypeNumber:
builder.writeValue(g.generateNumber(p))
case ast.TypeString:
builder.writeValue(g.generateString(p))
case ast.TypeObject:
builder.w.WString("{\n")
builder.deep++
last := len(p.Items) - 1
for index, item := range p.Items {
builder.writeIndent().w.WString(`"`).WString(item.Name.V()).WString(`"`).WString(": ")
if err := builder.encode(item, true, g); err != nil {
return err
}
if index < last {
builder.w.WString(",\n")
} else {
builder.w.WString("\n")
}
}
builder.deep--
builder.writeIndent().w.WString("}")
}
return builder.w.Err
}
func (builder *jsonBuilder) writeIndent() *jsonBuilder {
builder.w.WString(strings.Repeat(builder.indent, builder.deep))
return builder
}
// v 只能是基本类型
func (builder *jsonBuilder) writeValue(v interface{}) *jsonBuilder {
if builder.w.Err != nil {
return builder
}
vv, err := json.Marshal(v)
if err != nil {
builder.w.Err = err
return builder
}
builder.w.WBytes(vv)
return builder
}