merge master && fix both modify

This commit is contained in:
2025-01-26 14:32:59 +08:00
16 changed files with 536 additions and 153 deletions

View File

@ -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
}

View File

@ -10,6 +10,7 @@ package request
import (
"bytes"
"errors"
"github.com/mcuadros/go-defaults"
"io"
"net/http"
"strings"
@ -48,21 +49,27 @@ func (f *form) Parse(ctx *gin.Context, receiver interface{}) error {
method == http.MethodTrace ||
method == http.MethodConnect ||
method == http.MethodOptions {
return ctx.ShouldBindQuery(receiver)
}
if method == http.MethodPost ||
if err := ctx.ShouldBindQuery(receiver); nil != err {
return err
}
} else if method == http.MethodPost ||
method == http.MethodPut ||
method == http.MethodDelete {
if ContentType.IsJson(ctx) {
return ctx.ShouldBindJSON(receiver)
if err := ctx.ShouldBindJSON(receiver); nil != err {
return err
}
} else if ContentType.IsFormURLEncoded(ctx) {
if err := ctx.ShouldBind(receiver); nil != err {
return err
}
} else {
return errors.New(ctx.ContentType() + " is not support")
}
if ContentType.IsFormURLEncoded(ctx) {
return ctx.ShouldBind(receiver)
}
return errors.New(ctx.ContentType() + " is not support")
} else {
return errors.New(method + " : request method is not support")
}
return errors.New(method + " : request method is not support")
// 设置默认值
defaults.SetDefaults(receiver)
return nil
}

View File

@ -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
}

View File

@ -0,0 +1,70 @@
// 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{}
)
func init() {
adaptorList := []abstract.RequestBodyParseAdaptor{
JsonAdaptor{},
FormUrlEncode{},
XmlAdaptor{},
}
for _, itemAdaptor := range adaptorList {
Register(itemAdaptor)
}
}
// 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
}
}
// 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
}

View File

@ -0,0 +1,44 @@
// Package parse_body ...
//
// Description : parse_body ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-10-22 16:43
package parse_body
import (
"encoding/json"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/gin-gonic/gin"
)
type FormUrlEncode struct {
base
}
func (f FormUrlEncode) Parse(ctx *gin.Context, receiver any) ([]byte, error) {
if err := ctx.Request.ParseForm(); nil != err {
return nil, err
}
body := map[string]string{}
for paramName, itemParam := range ctx.Request.PostForm {
if len(itemParam) > 0 {
body[paramName] = itemParam[0]
} else {
body[paramName] = ""
}
}
byteData, _ := json.Marshal(body)
if nil != receiver {
if err := serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != receiver {
return nil, err
}
}
return byteData, nil
}
func (f FormUrlEncode) ContentType() string {
return consts.MimeTypeXWWWFormUrlencoded
}

View File

@ -0,0 +1,49 @@
// Package parse_body ...
//
// Description : parse_body ...
//
// Author : go_developer@163.com<白茶清欢>
//
// 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
}

42
request/parse_body/xml.go Normal file
View File

@ -0,0 +1,42 @@
// Package parse_body ...
//
// Description : parse_body ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-10-22 16:42
package parse_body
import (
"bytes"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/gin-gonic/gin"
"github.com/sbabiv/xml2map"
)
type XmlAdaptor struct {
base
}
func (x XmlAdaptor) Parse(ctx *gin.Context, receiver any) ([]byte, error) {
unmarshalFunc := x.Unmarshal()
if nil == unmarshalFunc {
unmarshalFunc = serialize.Xml.UnmarshalWithNumber
}
return x.DoParse(ctx, receiver, unmarshalFunc)
}
func (x XmlAdaptor) Unmarshal() func(sourceData []byte, receiver any) error {
return func(sourceData []byte, receiver any) error {
res, err := xml2map.NewDecoder(bytes.NewReader(sourceData)).Decode()
if nil != err {
return err
}
return serialize.JSON.Transition(res, receiver)
}
}
func (x XmlAdaptor) ContentType() string {
return consts.MimeTypeXml
}

View File

@ -0,0 +1,8 @@
// Package parse_body ...
//
// Description : parse_body ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-10-22 16:43
package parse_body

View File

@ -8,11 +8,9 @@
package request
import (
"bytes"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/gin/request/parse_body"
"git.zhangdeman.cn/zhangdeman/wrapper"
"github.com/gin-gonic/gin"
"io"
"strings"
)
@ -76,12 +74,12 @@ func (wh *wrapperHandle) GetScheme(ctx *gin.Context, defaultVal string) string {
return defaultVal
}
// GetQuery 获取query参数
// ParseQuery 获取query参数
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:03 2024/1/2
func (wh *wrapperHandle) GetQuery(ctx *gin.Context) map[string]string {
func (wh *wrapperHandle) ParseQuery(ctx *gin.Context) map[string]string {
query := make(map[string]string)
if nil != ctx && nil != ctx.Request && nil != ctx.Request.URL {
for paramName, valueList := range ctx.Request.URL.Query() {
@ -121,35 +119,27 @@ func (wh *wrapperHandle) GetContentType(ctx *gin.Context, defaultVal string) str
return wrapper.TernaryOperator.String(len(contentType) > 0, wrapper.String(contentType), wrapper.String(defaultVal)).Value()
}
// GetRequestBody 获取请求body
// GetDomain 获取请求Domain
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:12 2024/9/19
func (wh *wrapperHandle) GetDomain(ctx *gin.Context) string {
if nil == ctx {
return ""
}
return ctx.Request.Host
}
// ParseBody 获取请求body
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:25 2024/7/26
func (wh *wrapperHandle) GetRequestBody(ctx *gin.Context) map[string]any {
func (wh *wrapperHandle) ParseBody(ctx *gin.Context) map[string]any {
body := map[string]any{}
contentType := wh.GetContentType(ctx, "")
if strings.Contains(contentType, consts.MimeTypeXWWWFormUrlencoded) { // form请求
if err := ctx.Request.ParseForm(); nil != err {
return body
}
for paramName, itemParam := range ctx.Request.PostForm {
if len(itemParam) > 0 {
body[paramName] = itemParam[0]
} else {
body[paramName] = ""
}
}
return body
}
if strings.Contains(contentType, consts.MimeTypeJson) || nil != ctx.Request.Body { // json请求 或者非 json请求 存在 body
data, err := io.ReadAll(ctx.Request.Body)
if nil != err {
return body
}
ctx.Request.Body = io.NopCloser(bytes.NewBuffer(data))
return body
if _, err := parse_body.Execute(ctx, &body); nil != err {
return map[string]any{}
}
return body
@ -242,3 +232,39 @@ func (wh *wrapperHandle) GetCtxIntData(ctx *gin.Context, key string, defaultVal
}
return ctx.GetInt64(key)
}
// ParseHeader 解析header数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:04 2024/9/23
func (wh *wrapperHandle) ParseHeader(ctx *gin.Context) map[string]string {
headerData := map[string]string{}
if nil == ctx || nil == ctx.Request {
return headerData
}
for headerName, headerVal := range ctx.Request.Header {
if len(headerVal) > 0 {
headerData[headerName] = headerVal[0]
} else {
headerData[headerName] = ""
}
}
return headerData
}
// ParseCookie 解析cookie数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:06 2024/9/23
func (wh *wrapperHandle) ParseCookie(ctx *gin.Context) map[string]string {
cookieData := map[string]string{}
if nil == ctx || nil == ctx.Request {
return cookieData
}
for _, itemCookie := range ctx.Request.Cookies() {
cookieData[itemCookie.Name] = itemCookie.Value
}
return cookieData
}