-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_parameter.go
89 lines (70 loc) · 1.99 KB
/
request_parameter.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
package core
import (
"context"
"reflect"
"github.com/octohelm/courier/pkg/validator"
typesx "github.com/octohelm/x/types"
)
type RequestParameter struct {
Parameter
TransformerOption Option
Transformer Transformer
Validator validator.Validator
}
func EachRequestParameter(ctx context.Context, tpe typesx.Type, each func(rp *RequestParameter)) error {
errSet := validator.NewErrorSet()
EachParameter(ctx, tpe, func(p *Parameter) bool {
rp := &RequestParameter{}
rp.Parameter = *p
rp.TransformerOption.Name = rp.Name
if flagTags, ok := rp.Tags["name"]; ok {
rp.TransformerOption.Omitempty = flagTags.HasFlag("omitempty")
}
if mimeFlags, ok := rp.Tags["mime"]; ok {
rp.TransformerOption.MIME = mimeFlags.Name()
rp.TransformerOption.Strict = mimeFlags.HasFlag("strict")
rp.TransformerOption.Omitempty = rp.TransformerOption.Strict
}
if rp.In == "path" {
rp.TransformerOption.Omitempty = false
}
switch rp.Type.Kind() {
case reflect.Array, reflect.Slice:
if !isBytes(rp.Type) {
rp.TransformerOption.Explode = true
}
_, ok := typesx.EncodingTextMarshalerTypeReplacer(rp.Type)
if ok {
rp.TransformerOption.Explode = false
}
default:
}
getTransformer := func() (Transformer, error) {
if rp.TransformerOption.Explode {
return NewTransformer(ctx, rp.Type.Elem(), rp.TransformerOption)
}
return NewTransformer(ctx, rp.Type, rp.TransformerOption)
}
tt, err := getTransformer()
if err != nil {
errSet.AddErr(err, rp.Name)
return false
}
rp.Transformer = tt
parameterValidator, err := NewValidator(ctx, rp.Type, rp.Tags, rp.TransformerOption.Omitempty, tt)
if err != nil {
errSet.AddErr(err, rp.Name)
return false
}
rp.Validator = parameterValidator
each(rp)
return true
})
if errSet.Len() == 0 {
return nil
}
return errSet.Err()
}
func isBytes(t typesx.Type) bool {
return t.PkgPath() == "" && t.Kind() == reflect.Slice && t.Elem().PkgPath() == "" && t.Elem().Kind() == reflect.Uint8
}