♻️ Option to overide form decoder setting - Rename function (#1555)

* 🔥 add function to overide form decoder setting

🔥 Feature : SetBodyParserDecoder map all option to form decoder, use with BodyParserConfig, BodyParserType

🚨 Test : Test_Ctx_BodyParser_WithSetBodyParserDecoder

* 🔥 Use decoder builder function with default setting on init decoderPool

* ♻️ rename SetBodyParserDecoder to SetParserDecoder

BodyParserType > ParserType
bodyParserConfig > parserConfig
BodyParserConfig > ParserConfig
pull/1562/head
Rock 2021-10-04 14:37:24 +08:00 committed by GitHub
parent 95a9e5091e
commit a9b66b328e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 21 deletions

32
ctx.go
View File

@ -86,18 +86,18 @@ type Views interface {
Render(io.Writer, string, interface{}, ...string) error
}
// BodyParserType require two element, type and converter for register.
// Use BodyParserType with BodyParser for parsing custom type in form data.
type BodyParserType struct {
// ParserType require two element, type and converter for register.
// Use ParserType with BodyParser for parsing custom type in form data.
type ParserType struct {
Customtype interface{}
Converter func(string) reflect.Value
}
// BodyParserConfig form decoder config for SetBodyParserDecoder
type BodyParserConfig struct {
// ParserConfig form decoder config for SetParserDecoder
type ParserConfig struct {
IgnoreUnknownKeys bool
SetAliasTag string
BodyParserType []BodyParserType
ParserType []ParserType
ZeroEmpty bool
}
@ -287,29 +287,29 @@ func (c *Ctx) Body() []byte {
// decoderPool helps to improve BodyParser's and QueryParser's performance
var decoderPool = &sync.Pool{New: func() interface{} {
return decoderBuilder(BodyParserConfig{
return decoderBuilder(ParserConfig{
IgnoreUnknownKeys: true,
ZeroEmpty: true,
})
}}
// SetBodyParserDecoder allow globally change the option of form decoder, update decoderPool
func SetBodyParserDecoder(bodyParserConfig BodyParserConfig) {
// SetParserDecoder allow globally change the option of form decoder, update decoderPool
func SetParserDecoder(parserConfig ParserConfig) {
decoderPool = &sync.Pool{New: func() interface{} {
return decoderBuilder(bodyParserConfig)
return decoderBuilder(parserConfig)
}}
}
func decoderBuilder(bodyParserConfig BodyParserConfig) interface{} {
func decoderBuilder(parserConfig ParserConfig) interface{} {
var decoder = schema.NewDecoder()
decoder.IgnoreUnknownKeys(bodyParserConfig.IgnoreUnknownKeys)
if bodyParserConfig.SetAliasTag != "" {
decoder.SetAliasTag(bodyParserConfig.SetAliasTag)
decoder.IgnoreUnknownKeys(parserConfig.IgnoreUnknownKeys)
if parserConfig.SetAliasTag != "" {
decoder.SetAliasTag(parserConfig.SetAliasTag)
}
for _, v := range bodyParserConfig.BodyParserType {
for _, v := range parserConfig.ParserType {
decoder.RegisterConverter(reflect.ValueOf(v.Customtype).Interface(), v.Converter)
}
decoder.ZeroEmpty(bodyParserConfig.ZeroEmpty)
decoder.ZeroEmpty(parserConfig.ZeroEmpty)
return decoder
}

View File

@ -399,8 +399,8 @@ func Test_Ctx_BodyParser(t *testing.T) {
testDecodeParserError(MIMEMultipartForm+`;boundary="b"`, "--b")
}
// go test -run Test_Ctx_BodyParser_WithSetBodyParserDecoder
func Test_Ctx_BodyParser_WithSetBodyParserDecoder(t *testing.T) {
// go test -run Test_Ctx_BodyParser_WithSetParserDecoder
func Test_Ctx_BodyParser_WithSetParserDecoder(t *testing.T) {
type CustomTime time.Time
var timeConverter = func(value string) reflect.Value {
@ -410,14 +410,16 @@ func Test_Ctx_BodyParser_WithSetBodyParserDecoder(t *testing.T) {
return reflect.Value{}
}
customTime := BodyParserType{
customTime := ParserType{
Customtype: CustomTime{},
Converter: timeConverter,
}
SetBodyParserDecoder(BodyParserConfig{
SetParserDecoder(ParserConfig{
IgnoreUnknownKeys: true,
BodyParserType: []BodyParserType{customTime},
ParserType: []ParserType{customTime},
ZeroEmpty: true,
SetAliasTag: "form",
})