Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 16 additions & 16 deletions ctx.go
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 7 additions & 5 deletions ctx_test.go
Expand Up @@ -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 {
Expand All @@ -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",
})
Expand Down