Skip to content

Commit d3b8f9d

Browse files
committed
update
1 parent 1ca1695 commit d3b8f9d

104 files changed

Lines changed: 934 additions & 926 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

binder.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import (
99
type (
1010
// Binder is the interface that wraps the Bind method.
1111
Binder interface {
12-
Bind(interface{}, Context, ...FormDataFilter) error
13-
BindAndValidate(interface{}, Context, ...FormDataFilter) error
14-
MustBind(interface{}, Context, ...FormDataFilter) error
15-
MustBindAndValidate(interface{}, Context, ...FormDataFilter) error
12+
Bind(any, Context, ...FormDataFilter) error
13+
BindAndValidate(any, Context, ...FormDataFilter) error
14+
MustBind(any, Context, ...FormDataFilter) error
15+
MustBindAndValidate(any, Context, ...FormDataFilter) error
1616

17-
BindWithDecoder(interface{}, Context, BinderValueCustomDecoders, ...FormDataFilter) error
18-
BindAndValidateWithDecoder(interface{}, Context, BinderValueCustomDecoders, ...FormDataFilter) error
19-
MustBindWithDecoder(interface{}, Context, BinderValueCustomDecoders, ...FormDataFilter) error
20-
MustBindAndValidateWithDecoder(interface{}, Context, BinderValueCustomDecoders, ...FormDataFilter) error
17+
BindWithDecoder(any, Context, BinderValueCustomDecoders, ...FormDataFilter) error
18+
BindAndValidateWithDecoder(any, Context, BinderValueCustomDecoders, ...FormDataFilter) error
19+
MustBindWithDecoder(any, Context, BinderValueCustomDecoders, ...FormDataFilter) error
20+
MustBindAndValidateWithDecoder(any, Context, BinderValueCustomDecoders, ...FormDataFilter) error
2121
}
2222
binder struct {
23-
decoders map[string]func(interface{}, Context, BinderValueCustomDecoders, ...FormDataFilter) error
23+
decoders map[string]func(any, Context, BinderValueCustomDecoders, ...FormDataFilter) error
2424
}
2525

2626
BeforeBind interface {
@@ -29,14 +29,14 @@ type (
2929

3030
// for tag
3131

32-
BinderValueDecoder func(field string, values []string, params string) (interface{}, error)
33-
BinderValueEncoder func(field string, value interface{}, params string) []string
32+
BinderValueDecoder func(field string, values []string, params string) (any, error)
33+
BinderValueEncoder func(field string, value any, params string) []string
3434

3535
// for function argument
3636

37-
BinderValueCustomDecoder func(values []string) (interface{}, error)
37+
BinderValueCustomDecoder func(values []string) (any, error)
3838
BinderValueCustomDecoders map[string]BinderValueCustomDecoder // 这里的 key 为结构体字段或map的key层级路径
39-
BinderValueCustomEncoder func(interface{}) []string
39+
BinderValueCustomEncoder func(any) []string
4040
BinderValueCustomEncoders map[string]BinderValueCustomEncoder // 这里的 key 为表单字段层级路径
4141

4242
ValueDecodersGetter interface {
@@ -81,23 +81,23 @@ func NewBinder(e *Echo) Binder {
8181
}
8282
}
8383

84-
func (b *binder) MustBind(i interface{}, c Context, filter ...FormDataFilter) error {
84+
func (b *binder) MustBind(i any, c Context, filter ...FormDataFilter) error {
8585
return b.MustBindWithDecoder(i, c, nil, filter...)
8686
}
8787

88-
func (b *binder) MustBindAndValidate(i interface{}, c Context, filter ...FormDataFilter) error {
88+
func (b *binder) MustBindAndValidate(i any, c Context, filter ...FormDataFilter) error {
8989
return b.MustBindAndValidateWithDecoder(i, c, nil, filter...)
9090
}
9191

92-
func (b *binder) Bind(i interface{}, c Context, filter ...FormDataFilter) (err error) {
92+
func (b *binder) Bind(i any, c Context, filter ...FormDataFilter) (err error) {
9393
return b.BindWithDecoder(i, c, nil, filter...)
9494
}
9595

96-
func (b *binder) BindAndValidate(i interface{}, c Context, filter ...FormDataFilter) error {
96+
func (b *binder) BindAndValidate(i any, c Context, filter ...FormDataFilter) error {
9797
return b.BindAndValidateWithDecoder(i, c, nil, filter...)
9898
}
9999

100-
func (b *binder) MustBindWithDecoder(i interface{}, c Context, valueDecoders BinderValueCustomDecoders, filter ...FormDataFilter) error {
100+
func (b *binder) MustBindWithDecoder(i any, c Context, valueDecoders BinderValueCustomDecoders, filter ...FormDataFilter) error {
101101
if f, y := i.(BeforeBind); y {
102102
if err := f.BeforeBind(c); err != nil {
103103
return err
@@ -114,22 +114,22 @@ func (b *binder) MustBindWithDecoder(i interface{}, c Context, valueDecoders Bin
114114
return ErrUnsupportedMediaType
115115
}
116116

117-
func (b *binder) MustBindAndValidateWithDecoder(i interface{}, c Context, valueDecoders BinderValueCustomDecoders, filter ...FormDataFilter) error {
117+
func (b *binder) MustBindAndValidateWithDecoder(i any, c Context, valueDecoders BinderValueCustomDecoders, filter ...FormDataFilter) error {
118118
if err := b.MustBindWithDecoder(i, c, valueDecoders, filter...); err != nil {
119119
return err
120120
}
121121
return ValidateStruct(c, i)
122122
}
123123

124-
func (b *binder) BindWithDecoder(i interface{}, c Context, valueDecoders BinderValueCustomDecoders, filter ...FormDataFilter) (err error) {
124+
func (b *binder) BindWithDecoder(i any, c Context, valueDecoders BinderValueCustomDecoders, filter ...FormDataFilter) (err error) {
125125
err = b.MustBindWithDecoder(i, c, valueDecoders, filter...)
126126
if err == ErrUnsupportedMediaType {
127127
err = nil
128128
}
129129
return
130130
}
131131

132-
func (b *binder) BindAndValidateWithDecoder(i interface{}, c Context, valueDecoders BinderValueCustomDecoders, filter ...FormDataFilter) error {
132+
func (b *binder) BindAndValidateWithDecoder(i any, c Context, valueDecoders BinderValueCustomDecoders, filter ...FormDataFilter) error {
133133
if err := b.MustBindWithDecoder(i, c, valueDecoders, filter...); err != nil {
134134
if err != ErrUnsupportedMediaType {
135135
return err
@@ -138,10 +138,10 @@ func (b *binder) BindAndValidateWithDecoder(i interface{}, c Context, valueDecod
138138
return ValidateStruct(c, i)
139139
}
140140

141-
func (b *binder) SetDecoders(decoders map[string]func(interface{}, Context, BinderValueCustomDecoders, ...FormDataFilter) error) {
141+
func (b *binder) SetDecoders(decoders map[string]func(any, Context, BinderValueCustomDecoders, ...FormDataFilter) error) {
142142
b.decoders = decoders
143143
}
144144

145-
func (b *binder) AddDecoder(mime string, decoder func(interface{}, Context, BinderValueCustomDecoders, ...FormDataFilter) error) {
145+
func (b *binder) AddDecoder(mime string, decoder func(any, Context, BinderValueCustomDecoders, ...FormDataFilter) error) {
146146
b.decoders[mime] = decoder
147147
}

binder_stringer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ var (
99
Ignored = param.Ignored
1010
)
1111

12-
func TranslateStringer(t Translator, args ...interface{}) param.Stringer {
13-
return param.StringerFunc(func(v interface{}) string {
12+
func TranslateStringer(t Translator, args ...any) param.Stringer {
13+
return param.StringerFunc(func(v any) string {
1414
return t.T(param.AsString(v), args...)
1515
})
1616
}
1717

18-
func ignoreValues(v interface{}) []string {
18+
func ignoreValues(v any) []string {
1919
return nil
2020
}
2121

@@ -25,7 +25,7 @@ func FormStringer(s param.Stringer) BinderValueCustomEncoder {
2525
return ignoreValues
2626
}
2727
}
28-
return func(v interface{}) []string {
28+
return func(v any) []string {
2929
return []string{s.String(v)}
3030
}
3131
}

binder_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type TestForm struct {
2525
type TestData struct {
2626
File *TestFile
2727
Extra H
28-
List []interface{}
28+
List []any
2929
Strings param.StringSlice
3030
SStrings []param.String
3131
String param.String
@@ -209,7 +209,7 @@ func TestMapToStruct2(t *testing.T) {
209209
`key`: `v1`,
210210
`key2`: []string{`v1`, `v2`},
211211
},
212-
List: []interface{}{`v1`, `v2`},
212+
List: []any{`v1`, `v2`},
213213
},
214214
IDs: `1,2,3`,
215215
Interval: 5 * time.Minute,
@@ -445,10 +445,10 @@ func TestBinderConvertor(t *testing.T) {
445445
`item[env]`: {"A:ONE\nB:TWO"},
446446
`result`: {"A", "B"},
447447
}, ``, BinderValueCustomDecoders{
448-
`Item.Options`: func(values []string) (interface{}, error) {
448+
`Item.Options`: func(values []string) (any, error) {
449449
return com.SplitKVRows(values[0], `:`), nil
450450
},
451-
`Result`: func(values []string) (interface{}, error) {
451+
`Result`: func(values []string) (any, error) {
452452
return strings.Join(values, `/`), nil
453453
},
454454
})
@@ -472,7 +472,7 @@ func TestBinderConvertor(t *testing.T) {
472472

473473
ctx2 := e.NewContext(mock.NewRequest(), mock.NewResponse())
474474
StructToForm(ctx2, expected2, ``, MakeArrayFieldNameFormatter(com.LowerCaseFirst), param.StringerMap{
475-
`item[options]`: param.StringerFunc(func(value interface{}) string {
475+
`item[options]`: param.StringerFunc(func(value any) string {
476476
m, y := value.(map[string]string)
477477
if !y {
478478
return ``

binder_toform.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/webx-top/tagfast"
1313
)
1414

15-
func SetFormValue(f engine.URLValuer, fName string, index int, value interface{}) {
15+
func SetFormValue(f engine.URLValuer, fName string, index int, value any) {
1616
if index == 0 {
1717
f.Set(fName, param.AsString(value))
1818
} else {
@@ -31,7 +31,7 @@ func SetFormValues(f engine.URLValuer, fName string, values []string) {
3131
}
3232

3333
// FlatStructToForm 映射struct到form
34-
func FlatStructToForm(ctx Context, m interface{}, fieldNameFormatter FieldNameFormatter, formatters ...param.StringerMap) {
34+
func FlatStructToForm(ctx Context, m any, fieldNameFormatter FieldNameFormatter, formatters ...param.StringerMap) {
3535
StructToForm(ctx, m, ``, fieldNameFormatter, formatters...)
3636
}
3737

@@ -53,7 +53,7 @@ func (e *Echo) binderValueEncode(name string, typev reflect.Type, tv reflect.Val
5353

5454
// StructToForm 映射struct到form
5555

56-
func StructToForm(ctx Context, m interface{}, topName string, fieldNameFormatter FieldNameFormatter, formatters ...param.StringerMap) {
56+
func StructToForm(ctx Context, m any, topName string, fieldNameFormatter FieldNameFormatter, formatters ...param.StringerMap) {
5757
var stringers param.StringerMap // 这里的 key 为表单字段 name 属性值
5858
if len(formatters) > 0 {
5959
stringers = formatters[0]
@@ -84,7 +84,7 @@ func StructToForm(ctx Context, m interface{}, topName string, fieldNameFormatter
8484
structToForm(ctx, m, topName, fieldNameFormatter, valueEncoders)
8585
}
8686

87-
func structToForm(ctx Context, m interface{}, topName string, fieldNameFormatter FieldNameFormatter, valueEncoders BinderValueCustomEncoders) {
87+
func structToForm(ctx Context, m any, topName string, fieldNameFormatter FieldNameFormatter, valueEncoders BinderValueCustomEncoders) {
8888
vc := reflect.ValueOf(m)
8989
tc := reflect.TypeOf(m)
9090
if tc.Kind() == reflect.Pointer {
@@ -248,7 +248,7 @@ func fieldToForm(ctx Context, parentTyp reflect.Type, fStruct reflect.StructFiel
248248
for k, v := range sl {
249249
SetFormValue(f, fName, k, v)
250250
}
251-
case []interface{}:
251+
case []any:
252252
for k, v := range sl {
253253
SetFormValue(f, fName, k, v)
254254
}

binder_tostruct.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,31 @@ func FormNames(s string) []string {
4848
}
4949

5050
// NamedStructMap 自动将map值映射到结构体
51-
func NamedStructMap(e *Echo, m interface{}, data map[string][]string, topName string, filters ...FormDataFilter) error {
51+
func NamedStructMap(e *Echo, m any, data map[string][]string, topName string, filters ...FormDataFilter) error {
5252
return namedStructMap(e, m, data, topName, nil, filters)
5353
}
5454

55-
func NamedStructMapWithDecoder(e *Echo, m interface{}, data map[string][]string, topName string, valueDecoders BinderValueCustomDecoders, filters ...FormDataFilter) error {
55+
func NamedStructMapWithDecoder(e *Echo, m any, data map[string][]string, topName string, valueDecoders BinderValueCustomDecoders, filters ...FormDataFilter) error {
5656
return namedStructMap(e, m, data, topName, valueDecoders, filters)
5757
}
5858

59-
func FormToStruct(e *Echo, m interface{}, data map[string][]string, topName string, filters ...FormDataFilter) error {
59+
func FormToStruct(e *Echo, m any, data map[string][]string, topName string, filters ...FormDataFilter) error {
6060
return namedStructMap(e, m, data, topName, nil, filters)
6161
}
6262

63-
func FormToStructWithDecoder(e *Echo, m interface{}, data map[string][]string, topName string, valueDecoders BinderValueCustomDecoders, filters ...FormDataFilter) error {
63+
func FormToStructWithDecoder(e *Echo, m any, data map[string][]string, topName string, valueDecoders BinderValueCustomDecoders, filters ...FormDataFilter) error {
6464
return namedStructMap(e, m, data, topName, valueDecoders, filters)
6565
}
6666

67-
func FormToMap(e *Echo, m interface{}, data map[string][]string, topName string, filters ...FormDataFilter) error {
67+
func FormToMap(e *Echo, m any, data map[string][]string, topName string, filters ...FormDataFilter) error {
6868
return namedStructMap(e, m, data, topName, nil, filters)
6969
}
7070

71-
func FormToMapWithDecoder(e *Echo, m interface{}, data map[string][]string, topName string, valueDecoders BinderValueCustomDecoders, filters ...FormDataFilter) error {
71+
func FormToMapWithDecoder(e *Echo, m any, data map[string][]string, topName string, valueDecoders BinderValueCustomDecoders, filters ...FormDataFilter) error {
7272
return namedStructMap(e, m, data, topName, valueDecoders, filters)
7373
}
7474

75-
func namedStructMap(e *Echo, m interface{}, data map[string][]string, topName string, valueDecoders BinderValueCustomDecoders, filters []FormDataFilter) error {
75+
func namedStructMap(e *Echo, m any, data map[string][]string, topName string, valueDecoders BinderValueCustomDecoders, filters []FormDataFilter) error {
7676
vc := reflect.ValueOf(m)
7777
tc := reflect.TypeOf(m)
7878

@@ -159,7 +159,7 @@ func (e *Echo) valueDecode(valueDecoders BinderValueCustomDecoders, propPath str
159159
return nil
160160
}
161161

162-
func (e *Echo) parseFormItem(keyNormalizer func(string) string, m interface{}, typev reflect.Type, value reflect.Value, names []string, propPath string, checkPath string, values []string, valueDecoders BinderValueCustomDecoders, filters []FormDataFilter) error {
162+
func (e *Echo) parseFormItem(keyNormalizer func(string) string, m any, typev reflect.Type, value reflect.Value, names []string, propPath string, checkPath string, values []string, valueDecoders BinderValueCustomDecoders, filters []FormDataFilter) error {
163163
length := len(names)
164164
vc := value
165165
tc := typev
@@ -421,7 +421,7 @@ func (e *Echo) binderValueDecode(name string, typev reflect.Type, tv reflect.Val
421421
return ErrNotImplemented
422422
}
423423

424-
func (e *Echo) binderDecodeValue(decoder string, name string, values []string) (interface{}, error) {
424+
func (e *Echo) binderDecodeValue(decoder string, name string, values []string) (any, error) {
425425
parts := strings.SplitN(decoder, `:`, 2)
426426
decoder = parts[0]
427427
var params string
@@ -509,7 +509,7 @@ func (e *Echo) setMap(logger logger.Logger,
509509
return err
510510
}
511511

512-
func SetReflectValue(source interface{}, dest reflect.Value) bool {
512+
func SetReflectValue(source any, dest reflect.Value) bool {
513513
fv := reflect.ValueOf(source)
514514
destT := dest.Type()
515515
if destT.Name() == fv.Type().Name() {
@@ -545,7 +545,7 @@ func setField(logger logger.Logger, parentT reflect.Type, tv reflect.Value, f re
545545
ok, _ := strconv.ParseBool(v)
546546
SetReflectValue(ok, tv)
547547
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
548-
var l interface{}
548+
var l any
549549
dateformat := tagfast.Value(parentT, f, `form_format`)
550550
if len(dateformat) > 0 {
551551
t, err := time.ParseInLocation(dateformat, v, time.Local)
@@ -566,7 +566,7 @@ func setField(logger logger.Logger, parentT reflect.Type, tv reflect.Value, f re
566566
}
567567
SetReflectValue(l, tv)
568568
case reflect.Int64:
569-
var l interface{}
569+
var l any
570570
switch tv.Interface().(type) {
571571
case time.Duration:
572572
l, _ = time.ParseDuration(v)
@@ -628,7 +628,7 @@ func setField(logger logger.Logger, parentT reflect.Type, tv reflect.Value, f re
628628
logger.Warnf(`binder: arg %q as uint: %v`, v, err)
629629
}
630630
}
631-
var l interface{}
631+
var l any
632632
switch kind {
633633
case reflect.Uint:
634634
l = uint(x)

0 commit comments

Comments
 (0)