Skip to content

Commit 7a833fc

Browse files
committed
修复默认值
1 parent d74e67c commit 7a833fc

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

binder.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,24 @@ import (
1111
`strconv`
1212
`strings`
1313

14-
`github.com/creasty/defaults`
1514
`github.com/labstack/echo/v4`
15+
`github.com/storezhang/god`
1616
`github.com/vmihailenco/msgpack/v5`
1717
`google.golang.org/protobuf/proto`
1818
)
1919

2020
type binder struct {
21-
tagParam string
22-
tagQuery string
23-
tagForm string
24-
tagHeader string
21+
tagParam string
22+
tagQuery string
23+
tagForm string
24+
tagHeader string
25+
tagDefault string
2526
}
2627

2728
func (b *binder) Bind(value interface{}, ctx echo.Context) (err error) {
2829
// 处理默认值
2930
defer func() {
30-
err = defaults.Set(value)
31+
err = god.Set(value, god.Tag(b.tagDefault))
3132
}()
3233

3334
if err = b.params(ctx, value); nil != err {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/mitchellh/mapstructure v1.4.1 // indirect
1414
github.com/rs/xid v1.3.0
1515
github.com/storezhang/gox v1.8.1
16+
github.com/storezhang/god v0.0.2
1617
github.com/storezhang/validatorx v1.0.8
1718
github.com/vmihailenco/msgpack/v5 v5.3.4
1819
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect

option.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package echox
22

33
var defaultOptions = &options{
4-
addr: ":9000",
4+
addr: `:9000`,
55
binder: &binder{
6-
tagParam: "param",
7-
tagQuery: "query",
8-
tagForm: "form",
9-
tagHeader: "header",
6+
tagParam: `param`,
7+
tagQuery: `query`,
8+
tagForm: `form`,
9+
tagHeader: `header`,
10+
tagDefault: `default`,
1011
},
1112
validate: true,
1213
banner: false,
1314
error: errorHandlerFunc,
1415
crosEnable: true,
1516
cros: crosConfig{
16-
origins: []string{"*"},
17+
origins: []string{`*`},
1718
credentials: true,
1819
},
1920
panicStack: panicStackConfig{

option_binder.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ package echox
33
var _ option = (*optionBinder)(nil)
44

55
type optionBinder struct {
6-
param string
7-
query string
8-
form string
9-
header string
6+
param string
7+
query string
8+
form string
9+
header string
10+
_default string
11+
1012
disable bool
1113
}
1214

1315
// Binder 配置数据绑定
14-
func Binder(param string, query string, form string, header string) *optionBinder {
16+
func Binder(param string, query string, form string, header string, _default string) *optionBinder {
1517
return &optionBinder{
16-
param: param,
17-
query: query,
18-
form: form,
19-
header: header,
18+
param: param,
19+
query: query,
20+
form: form,
21+
header: header,
22+
_default: _default,
2023
}
2124
}
2225

@@ -35,5 +38,6 @@ func (b *optionBinder) apply(options *options) {
3538
options.binder.tagQuery = b.query
3639
options.binder.tagForm = b.form
3740
options.binder.tagHeader = b.header
41+
options.binder.tagDefault = b._default
3842
}
3943
}

0 commit comments

Comments
 (0)