From 56441151cfa9f21633d2abc6e95eb0f2f0fed09b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 22 Oct 2024 17:14:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0json=E7=9A=84=E8=AF=B7?= =?UTF-8?q?=E6=B1=82Body=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- init.go | 12 +++++++++ request/parse_body/base.go | 46 +++++++++++++++++++++++++++++++++++ request/parse_body/execute.go | 21 ++++++++++++++++ request/parse_body/json.go | 41 +++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 init.go create mode 100644 request/parse_body/base.go diff --git a/init.go b/init.go new file mode 100644 index 0000000..5a03c6a --- /dev/null +++ b/init.go @@ -0,0 +1,12 @@ +// Package gin ... +// +// Description : gin ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 17:01 +package gin + +import ( + _ "git.zhangdeman.cn/zhangdeman/gin/request/parse_body" +) diff --git a/request/parse_body/base.go b/request/parse_body/base.go new file mode 100644 index 0000000..792fa8f --- /dev/null +++ b/request/parse_body/base.go @@ -0,0 +1,46 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:51 +package parse_body + +import ( + "bytes" + "github.com/gin-gonic/gin" + "io" +) + +type base struct { +} + +// Unmarshal 反序列化方法,可以不指定 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:54 2024/10/22 +func (b base) Unmarshal() func(sourceData []byte, receiver any) error { + return nil +} + +// DoParse 解析 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:08 2024/10/22 +func (b base) DoParse(ctx *gin.Context, receiver any, unmarshalFunc func(sourceData []byte, receiver any) error) ([]byte, error) { + data, err := io.ReadAll(ctx.Request.Body) + if nil != err { + return nil, err + } + ctx.Request.Body = io.NopCloser(bytes.NewBuffer(data)) + if nil == unmarshalFunc || nil == receiver { + return data, nil + } + if err := unmarshalFunc(data, receiver); nil != err { + return nil, err + } + return data, err +} diff --git a/request/parse_body/execute.go b/request/parse_body/execute.go index 0455ce4..22937e5 100644 --- a/request/parse_body/execute.go +++ b/request/parse_body/execute.go @@ -18,6 +18,15 @@ var ( requestBodyParseAdaptorTable = map[string]abstract.RequestBodyParseAdaptor{} ) +func init() { + adaptorList := []abstract.RequestBodyParseAdaptor{ + JsonAdaptor{}, + } + for _, itemAdaptor := range adaptorList { + Register(itemAdaptor) + } +} + // Register 注册适配器实例 // // Author : go_developer@163.com<白茶清欢> @@ -45,3 +54,15 @@ func Execute(ctx *gin.Context, receiver any) ([]byte, error) { return parseResult, err } } + +// ExecuteForMap 高层级包装,表单解析为map +func ExecuteForMap(ctx *gin.Context) (map[string]any, error) { + var ( + err error + result map[string]any + ) + if _, err = Execute(ctx, &result); nil != err { + return nil, err + } + return result, nil +} diff --git a/request/parse_body/json.go b/request/parse_body/json.go index 676d6c7..cb2301c 100644 --- a/request/parse_body/json.go +++ b/request/parse_body/json.go @@ -6,3 +6,44 @@ // // Date : 2024-10-22 16:42 package parse_body + +import ( + "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/serialize" + "github.com/gin-gonic/gin" +) + +type JsonAdaptor struct { + base +} + +// Parse 解析json请求体 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:13 2024/10/22 +func (j JsonAdaptor) Parse(ctx *gin.Context, receiver any) ([]byte, error) { + unmarshalFunc := j.Unmarshal() + if nil == unmarshalFunc { + unmarshalFunc = serialize.JSON.UnmarshalWithNumber + } + return j.DoParse(ctx, receiver, unmarshalFunc) +} + +// Unmarshal 指定解析方法 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:56 2024/10/22 +func (j JsonAdaptor) Unmarshal() func(sourceData []byte, receiver any) error { + return serialize.JSON.UnmarshalWithNumber +} + +// ContentType 请求类型固定返回json +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:59 2024/10/22 +func (j JsonAdaptor) ContentType() string { + return consts.MimeTypeJson +}