-
Notifications
You must be signed in to change notification settings - Fork 1
/
flatten_params.go
59 lines (47 loc) · 1.19 KB
/
flatten_params.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
package core
import (
"context"
"reflect"
"github.com/octohelm/courier/pkg/validator"
reflectx "github.com/octohelm/x/reflect"
typesx "github.com/octohelm/x/types"
)
type FlattenParams struct {
Parameters []RequestParameter
}
type TransformerAndOption struct {
Transformer
Option Option
}
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 (params *FlattenParams) Validate(v any) error {
rv, ok := v.(reflect.Value)
if !ok {
rv = reflect.ValueOf(v)
}
errSet := validator.NewErrorSet()
rv = reflectx.Indirect(rv)
for i := range params.Parameters {
p := params.Parameters[i]
fieldValue := p.FieldValue(rv)
if p.Validator != nil {
if err := p.Validator.Validate(fieldValue); err != nil {
errSet.AddErr(err, p.Name)
}
}
}
return errSet.Err()
}
func (params *FlattenParams) CollectParams(ctx context.Context, typ typesx.Type) error {
err := EachRequestParameter(ctx, typesx.Deref(typ), func(rp *RequestParameter) {
params.Parameters = append(params.Parameters, *rp)
})
return err
}