Skip to content

Commit

Permalink
♻️ Option to overide form decoder setting - Rename function (#1555)
Browse files Browse the repository at this point in the history
* 🔥 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
  • Loading branch information
rockcreation7 committed Oct 4, 2021
1 parent 95a9e50 commit a9b66b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
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

0 comments on commit a9b66b3

Please sign in to comment.