-
Notifications
You must be signed in to change notification settings - Fork 34
/
util_param.go
261 lines (213 loc) · 5.21 KB
/
util_param.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
package transformer
import (
"context"
"go/ast"
"reflect"
"sync"
"github.com/machinefi/w3bstream/pkg/depends/kit/validator"
vldterr "github.com/machinefi/w3bstream/pkg/depends/kit/validator/errors"
"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
"github.com/machinefi/w3bstream/pkg/depends/x/reflectx"
"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
)
type Param struct {
In string
Name string
Field typesx.StructField
Type typesx.Type
Tags map[string]reflectx.StructTag
Loc []int
}
func (p *Param) FieldValue(rv reflect.Value) reflect.Value {
rv = reflectx.Indirect(rv)
n := len(p.Loc)
fv := rv
for i := 0; i < n; i++ {
loc := p.Loc[i]
fv = fv.Field(loc)
// last loc should keep ptr value
if i < n-1 {
for fv.Kind() == reflect.Ptr {
// notice the ptr struct ensure only for Ptr Anonymous Field
if fv.IsNil() {
fv.Set(reflectx.New(fv.Type()))
}
fv = fv.Elem()
}
}
}
return fv
}
func EachParameter(_ context.Context, t typesx.Type, each func(p *Param) bool) {
var walk func(tpe typesx.Type, parents ...int)
walk = func(t typesx.Type, parents ...int) {
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if !ast.IsExported(f.Name()) {
continue
}
loc := append(parents, i)
flags := reflectx.ParseStructTag(string(f.Tag()))
display := f.Name()
tagIn, hasIn := flags["in"]
tagName, hasName := flags["name"]
if hasName {
if name := tagName.Name(); name == "-" {
continue // skip name:"-"
} else {
if name != "" {
display = name
}
}
}
if f.Anonymous() && (!hasIn && !hasName) {
ft := f.Type()
_, ok := typesx.EncodingTextMarshalerTypeReplacer(ft)
if !ok {
for ft.Kind() == reflect.Ptr {
ft = ft.Elem()
}
if ft.Kind() == reflect.Struct {
walk(ft, loc...)
continue
}
}
}
p := &Param{
In: tagIn.Name(),
Name: display,
Field: f,
Type: f.Type(),
Tags: flags,
Loc: append([]int{}, loc...),
}
if !each(p) {
break
}
}
}
walk(t)
}
type FlattenParams struct {
Params []ReqParam
}
func (FlattenParams) NewValidator(ctx context.Context, typ typesx.Type) (validator.Validator, error) {
p := &FlattenParams{}
err := p.CollectParams(ctx, typ)
return p, err
}
func (FlattenParams) String() string { return "@flatten" }
func (ps *FlattenParams) Validate(v interface{}) error {
rv, ok := v.(reflect.Value)
if !ok {
rv = reflect.ValueOf(v)
}
errs := vldterr.NewErrorSet()
rv = reflectx.Indirect(rv)
for i := range ps.Params {
pi := ps.Params[i]
fieldValue := pi.FieldValue(rv)
if pi.Validator != nil {
if err := pi.Validator.Validate(fieldValue); err != nil {
errs.AddErr(err, pi.Name)
}
}
}
return errs.Err()
}
func (ps *FlattenParams) CollectParams(ctx context.Context, typ typesx.Type) error {
err := EachReqParam(ctx, typesx.DeRef(typ), func(rp *ReqParam) {
ps.Params = append(ps.Params, *rp)
})
return err
}
type ReqParam struct {
Param
Option Option
Tsf Transformer
Validator validator.Validator
}
func EachReqParam(ctx context.Context, tpe typesx.Type, each func(rp *ReqParam)) error {
errs := vldterr.NewErrorSet()
EachParameter(ctx, tpe, func(p *Param) bool {
rp := &ReqParam{}
rp.Param = *p
rp.Option.Name = rp.Name
if tag, ok := rp.Tags["name"]; ok {
rp.Option.Omitempty = tag.HasFlag("omitempty")
}
if tag, ok := rp.Tags["mime"]; ok {
rp.Option.MIME = tag.Name()
}
if rp.In == "path" {
rp.Option.Omitempty = false
}
switch rp.Type.Kind() {
case reflect.Array, reflect.Slice:
elem := rp.Type.Elem()
if !(elem.PkgPath() == "" && elem.Kind() == reflect.Uint8) {
rp.Option.Explode = true
}
}
newtsf := func() (Transformer, error) {
if rp.Option.Explode {
return NewTransformer(ctx, rp.Type.Elem(), rp.Option)
}
return NewTransformer(ctx, rp.Type, rp.Option)
}
tsf, err := newtsf()
if err != nil {
errs.AddErr(err, rp.Name)
return false
}
rp.Tsf = tsf
// TODO check if current struct or field impled Validator
paramVldt, err := NewValidator(ctx, rp.Type, rp.Tags, rp.Option.Omitempty, tsf)
if err != nil {
errs.AddErr(err, rp.Name)
return false
}
rp.Validator = paramVldt
each(rp)
return true
})
if errs.Len() == 0 {
return nil
}
return errs.Err()
}
type ParamAndValue struct {
Param
Value reflect.Value
}
type GroupedParams = map[string][]Param
type ckGroupedParamsSet struct{}
// dftGroupedParamsSet stores typesx.Type => GroupedParams
var dftGroupedParamsSet = &sync.Map{}
func GroupedParamSetFromContext(ctx context.Context) *sync.Map {
if m, ok := ctx.Value(ckGroupedParamsSet{}).(*sync.Map); ok {
return m
}
return dftGroupedParamsSet
}
func WithGroupedParamSet(ctx context.Context, m *sync.Map) context.Context {
return contextx.WithValue(ctx, ckGroupedParamsSet{}, m)
}
func CollectGroupedParam(ctx context.Context, tpe typesx.Type) GroupedParams {
if tpe.Kind() != reflect.Struct {
return nil
}
m := GroupedParamSetFromContext(ctx)
if gp, ok := m.Load(tpe); ok {
return gp.(GroupedParams)
}
gp := GroupedParams{}
defer func() {
m.Store(tpe, gp)
}()
EachParameter(ctx, tpe, func(p *Param) bool {
gp[p.In] = append(gp[p.In], *p)
return true
})
return gp
}