diff --git a/request/abstract/parse_body.go b/request/abstract/parse_body.go new file mode 100644 index 0000000..2b4a497 --- /dev/null +++ b/request/abstract/parse_body.go @@ -0,0 +1,26 @@ +// Package abstract ... +// +// Description : abstract ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:38 +package abstract + +import ( + "github.com/gin-gonic/gin" +) + +// RequestBodyParseAdaptor 解析请求body的接口适配器约束 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:39 2024/10/22 +type RequestBodyParseAdaptor interface { + // Parse 解析Body数据,解析结果会反序列化至 receiver , 同时, 会以 map 结构返回 + Parse(ctx *gin.Context, receiver any) ([]byte, error) + // Unmarshal 自定义反序列化的方法, 为 nil 则使用内置的序列化方式 + Unmarshal() func(sourceData []byte, receiver any) error + // ContentType 当前适配器用与解析何种type + ContentType() string +} diff --git a/request/parse_body/execute.go b/request/parse_body/execute.go new file mode 100644 index 0000000..0455ce4 --- /dev/null +++ b/request/parse_body/execute.go @@ -0,0 +1,47 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:43 +package parse_body + +import ( + "errors" + "git.zhangdeman.cn/zhangdeman/gin/request/abstract" + "github.com/gin-gonic/gin" + "strings" +) + +var ( + requestBodyParseAdaptorTable = map[string]abstract.RequestBodyParseAdaptor{} +) + +// Register 注册适配器实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:45 2024/10/22 +func Register(adaptor abstract.RequestBodyParseAdaptor) { + if nil == adaptor { + return + } + requestBodyParseAdaptorTable[adaptor.ContentType()] = adaptor +} + +// Execute 解析请求BODY数据 +func Execute(ctx *gin.Context, receiver any) ([]byte, error) { + contentType := strings.ToLower(strings.ReplaceAll(ctx.ContentType(), " ", "")) + // 裁剪出真实的类型,之所以截取,是因为 content_type 中可能还包含编码信息, 如 : application/json;charset=utf8 + contentTypeArr := strings.Split(contentType, ";") + contentType = contentTypeArr[0] + if _, exist := requestBodyParseAdaptorTable[contentType]; !exist { + return nil, errors.New(contentType + " : adaptor not found") + } + if parseResult, err := requestBodyParseAdaptorTable[contentType].Parse(ctx, receiver); nil != err { + return nil, err + } else { + return parseResult, err + } +} diff --git a/request/parse_body/form_url_encode.go b/request/parse_body/form_url_encode.go new file mode 100644 index 0000000..8a18ca3 --- /dev/null +++ b/request/parse_body/form_url_encode.go @@ -0,0 +1,8 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:43 +package parse_body diff --git a/request/parse_body/json.go b/request/parse_body/json.go new file mode 100644 index 0000000..676d6c7 --- /dev/null +++ b/request/parse_body/json.go @@ -0,0 +1,8 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:42 +package parse_body diff --git a/request/parse_body/xml.go b/request/parse_body/xml.go new file mode 100644 index 0000000..676d6c7 --- /dev/null +++ b/request/parse_body/xml.go @@ -0,0 +1,8 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:42 +package parse_body diff --git a/request/parse_body/yml.go b/request/parse_body/yml.go new file mode 100644 index 0000000..8a18ca3 --- /dev/null +++ b/request/parse_body/yml.go @@ -0,0 +1,8 @@ +// Package parse_body ... +// +// Description : parse_body ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-22 16:43 +package parse_body