47 lines
985 B
Go
47 lines
985 B
Go
// 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
|
|
}
|