From a9b66b328efc5013bece3e65f0e05018a01bcd5d Mon Sep 17 00:00:00 2001 From: Rock Date: Mon, 4 Oct 2021 14:37:24 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Option=20to=20overide=20fo?= =?UTF-8?q?rm=20decoder=20setting=20-=20Rename=20function=20(#1555)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🔥 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 --- ctx.go | 32 ++++++++++++++++---------------- ctx_test.go | 12 +++++++----- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/ctx.go b/ctx.go index af551711..b0a2a747 100644 --- a/ctx.go +++ b/ctx.go @@ -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 } diff --git a/ctx_test.go b/ctx_test.go index c33745e6..8e9caa53 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -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", })