|
|
@@ -22,7 +22,13 @@ type Context struct {
|
|
|
*gin.Context
|
|
|
}
|
|
|
|
|
|
-// GetFileHeaderBytes 获取传递的文件名和文件内容
|
|
|
+// GetFileHeaderBytes 获取Multipart中传递的文件名和文件内容
|
|
|
+// 参数:
|
|
|
+// - fileHeader: Multipart的文件头
|
|
|
+// 返回值:
|
|
|
+// - 文件名
|
|
|
+// - 文件字节
|
|
|
+// - 错误
|
|
|
func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
|
|
|
file, err := fileHeader.Open()
|
|
|
if err != nil {
|
|
|
@@ -45,24 +51,10 @@ func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string,
|
|
|
return fileHeader.Filename, contentBytes, nil
|
|
|
}
|
|
|
|
|
|
-type Header struct {
|
|
|
- c *Context
|
|
|
- header map[string]string
|
|
|
-}
|
|
|
-
|
|
|
-func (header *Header) Set(key string, value string) {
|
|
|
- header.header[key] = value
|
|
|
- header.c.Set(headerKey, header.header)
|
|
|
-}
|
|
|
-
|
|
|
-func (header *Header) Get(key string) string {
|
|
|
- return header.header[key]
|
|
|
-}
|
|
|
-
|
|
|
-func (header *Header) Map() map[string]string {
|
|
|
- return header.header
|
|
|
-}
|
|
|
-
|
|
|
+// GetHeader 获取上下文中的Header
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 上下文中的Header
|
|
|
func (c *Context) GetHeader() *Header {
|
|
|
savedHeader, exist := c.Get(headerKey)
|
|
|
if exist {
|
|
|
@@ -86,41 +78,12 @@ func (c *Context) GetHeader() *Header {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-type CacheBody struct {
|
|
|
- c *Context
|
|
|
- bytesBody []byte
|
|
|
-}
|
|
|
-
|
|
|
-func (cacheBody *CacheBody) Set(bytesBody []byte) {
|
|
|
- cacheBody.bytesBody = bytesBody
|
|
|
- cacheBody.c.Set(bodyKey, cacheBody.bytesBody)
|
|
|
-}
|
|
|
-
|
|
|
-func (cacheBody *CacheBody) Bytes() []byte {
|
|
|
- return cacheBody.bytesBody
|
|
|
-}
|
|
|
-
|
|
|
-func (cacheBody *CacheBody) Marshal(output any) error {
|
|
|
- bytesBody, err := json.Marshal(output)
|
|
|
- if err != nil {
|
|
|
- return errors.New(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- cacheBody.Set(bytesBody)
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (cacheBody *CacheBody) Unmarshal(output any) error {
|
|
|
- err := json.Unmarshal(cacheBody.Bytes(), output)
|
|
|
- if err != nil {
|
|
|
- return errors.New(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (c *Context) GetBytesBody() (*CacheBody, error) {
|
|
|
+// GetBytesBody 获取上下文中的字节Body
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 上下文中的字节Body
|
|
|
+// - 错误
|
|
|
+func (c *Context) GetBytesBody() (*BytesBody, error) {
|
|
|
body, exist := c.Get(bodyKey)
|
|
|
if !exist {
|
|
|
bytesBody, err := c.readOriginBody()
|
|
|
@@ -128,7 +91,9 @@ func (c *Context) GetBytesBody() (*CacheBody, error) {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- return &CacheBody{
|
|
|
+ c.Set(bodyKey, bytesBody)
|
|
|
+
|
|
|
+ return &BytesBody{
|
|
|
c: c,
|
|
|
bytesBody: bytesBody,
|
|
|
}, nil
|
|
|
@@ -136,7 +101,7 @@ func (c *Context) GetBytesBody() (*CacheBody, error) {
|
|
|
|
|
|
switch b := body.(type) {
|
|
|
case []byte:
|
|
|
- return &CacheBody{
|
|
|
+ return &BytesBody{
|
|
|
c: c,
|
|
|
bytesBody: b,
|
|
|
}, nil
|
|
|
@@ -146,7 +111,7 @@ func (c *Context) GetBytesBody() (*CacheBody, error) {
|
|
|
return nil, errors.New(err.Error())
|
|
|
}
|
|
|
|
|
|
- return &CacheBody{
|
|
|
+ return &BytesBody{
|
|
|
c: c,
|
|
|
bytesBody: bytesBody,
|
|
|
}, nil
|
|
|
@@ -155,29 +120,224 @@ func (c *Context) GetBytesBody() (*CacheBody, error) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// GetJsonBody 获取上下文中的JsonBody
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 上下文中的JsonBody
|
|
|
+// - 错误
|
|
|
+func (c *Context) GetJsonBody() (*JsonBody, error) {
|
|
|
+ body, exist := c.Get(bodyKey)
|
|
|
+ if !exist {
|
|
|
+ bytesBody, err := c.readOriginBody()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ jsonBodyMap := make(map[string]any)
|
|
|
+ err = json.Unmarshal(bytesBody, &jsonBodyMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ c.Set(bodyKey, jsonBodyMap)
|
|
|
+
|
|
|
+ return &JsonBody{
|
|
|
+ c: c,
|
|
|
+ jsonBodyMap: jsonBodyMap,
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ switch b := body.(type) {
|
|
|
+ case []byte:
|
|
|
+ jsonBodyMap := make(map[string]any)
|
|
|
+ err := json.Unmarshal(b, &jsonBodyMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ return &JsonBody{
|
|
|
+ c: c,
|
|
|
+ jsonBodyMap: jsonBodyMap,
|
|
|
+ }, nil
|
|
|
+ case map[string]any:
|
|
|
+ return &JsonBody{
|
|
|
+ c: c,
|
|
|
+ jsonBodyMap: body.(map[string]any),
|
|
|
+ }, nil
|
|
|
+ default:
|
|
|
+ return nil, errors.New("不支持的body类型")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// GetQueryParams 获取上下文中的查询参数
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 上下文中的查询参数
|
|
|
+// - 错误
|
|
|
+func (c *Context) GetQueryParams() *QueryPrams {
|
|
|
+ queryParams, exist := c.Get(queryParamsKey)
|
|
|
+ if !exist {
|
|
|
+ return &QueryPrams{
|
|
|
+ c: c,
|
|
|
+ queryParams: c.getAllQueryParams(),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return &QueryPrams{
|
|
|
+ c: c,
|
|
|
+ queryParams: queryParams.(map[string]string),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// GetPathParams 获取上下文中的路径参数
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 上下文中的路径参数
|
|
|
+// - 错误
|
|
|
+func (c *Context) GetPathParams() *PathPrams {
|
|
|
+ pathParams, exist := c.Get(pathParamsKey)
|
|
|
+ if !exist {
|
|
|
+ return &PathPrams{
|
|
|
+ c: c,
|
|
|
+ pathParams: c.getAllPathParams(),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return &PathPrams{
|
|
|
+ c: c,
|
|
|
+ pathParams: pathParams.(map[string]string),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+type Header struct {
|
|
|
+ c *Context
|
|
|
+ header map[string]string
|
|
|
+}
|
|
|
+
|
|
|
+// Set 设置Header
|
|
|
+// 参数:
|
|
|
+// - key: Header的键
|
|
|
+// - value: Header对应键的值
|
|
|
+// 返回值: 无
|
|
|
+func (header *Header) Set(key string, value string) {
|
|
|
+ header.header[key] = value
|
|
|
+ header.c.Set(headerKey, header.header)
|
|
|
+}
|
|
|
+
|
|
|
+// Get 获取Header对应键的值
|
|
|
+// 参数:
|
|
|
+// - key: Header的键
|
|
|
+// 返回值:
|
|
|
+// - Header对应键的值
|
|
|
+func (header *Header) Get(key string) string {
|
|
|
+ return header.header[key]
|
|
|
+}
|
|
|
+
|
|
|
+// Map 获取Header的map表示
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - Header的map表示
|
|
|
+func (header *Header) Map() map[string]string {
|
|
|
+ return header.header
|
|
|
+}
|
|
|
+
|
|
|
+type BytesBody struct {
|
|
|
+ c *Context
|
|
|
+ bytesBody []byte
|
|
|
+}
|
|
|
+
|
|
|
+// Set 设置字节body
|
|
|
+// 参数:
|
|
|
+// - body: 字节body的内容
|
|
|
+// 返回值: 无
|
|
|
+func (bytesBody *BytesBody) Set(body []byte) {
|
|
|
+ bytesBody.bytesBody = body
|
|
|
+ bytesBody.c.Set(bodyKey, bytesBody.bytesBody)
|
|
|
+}
|
|
|
+
|
|
|
+// Bytes 获取字节body的内容
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 字节body的内容
|
|
|
+func (bytesBody *BytesBody) Bytes() []byte {
|
|
|
+ return bytesBody.bytesBody
|
|
|
+}
|
|
|
+
|
|
|
+// Marshal 将input Marshal到字节body
|
|
|
+// 参数:
|
|
|
+// - input: 输入,一般为结构或map[string]any
|
|
|
+// 返回值:
|
|
|
+// - 错误
|
|
|
+func (bytesBody *BytesBody) Marshal(input any) error {
|
|
|
+ jsonBytesBody, err := json.Marshal(input)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ bytesBody.Set(jsonBytesBody)
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// Unmarshal 将字节body的内容Unmarshal到output
|
|
|
+// 参数:
|
|
|
+// - output: 输出,一般为结构指针或map[string]any指针
|
|
|
+// 返回值:
|
|
|
+// - 错误
|
|
|
+func (bytesBody *BytesBody) Unmarshal(output any) error {
|
|
|
+ err := json.Unmarshal(bytesBody.Bytes(), output)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
type JsonBody struct {
|
|
|
c *Context
|
|
|
jsonBodyMap map[string]any
|
|
|
}
|
|
|
|
|
|
+// Set 设置JsonBody键对应的值
|
|
|
+// 参数:
|
|
|
+// - key: JsonBody的键
|
|
|
+// - value: JsonBody对应键的值
|
|
|
+// 返回值: 无
|
|
|
func (jsonBody *JsonBody) Set(key string, value any) {
|
|
|
jsonBody.jsonBodyMap[key] = value
|
|
|
jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
|
|
|
}
|
|
|
|
|
|
+// Delete 删除JsonBody键对应的值
|
|
|
+// 参数:
|
|
|
+// - key: JsonBody的键
|
|
|
+// 返回值: 无
|
|
|
func (jsonBody *JsonBody) Delete(key string) {
|
|
|
delete(jsonBody.jsonBodyMap, key)
|
|
|
jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
|
|
|
}
|
|
|
|
|
|
+// Get 获取JsonBody对应键的值
|
|
|
+// 参数:
|
|
|
+// - key: JsonBody的键
|
|
|
+// 返回值:
|
|
|
+// - JsonBody对应键的值
|
|
|
func (jsonBody *JsonBody) Get(key string) any {
|
|
|
return jsonBody.jsonBodyMap[key]
|
|
|
}
|
|
|
|
|
|
+// Map 获取JsonBody的map表示
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - JsonBody的map表示
|
|
|
func (jsonBody *JsonBody) Map() map[string]any {
|
|
|
return jsonBody.jsonBodyMap
|
|
|
}
|
|
|
|
|
|
+// Bytes 获取JsonBody的内容
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - JsonBody的内容
|
|
|
func (jsonBody *JsonBody) Bytes() ([]byte, error) {
|
|
|
jsonBytes, err := json.Marshal(jsonBody.jsonBodyMap)
|
|
|
if err != nil {
|
|
|
@@ -187,6 +347,11 @@ func (jsonBody *JsonBody) Bytes() ([]byte, error) {
|
|
|
return jsonBytes, nil
|
|
|
}
|
|
|
|
|
|
+// Unmarshal 将JsonBody的内容Unmarshal到output
|
|
|
+// 参数:
|
|
|
+// - output: 输出,一般为结构指针或map[string]any指针
|
|
|
+// 返回值:
|
|
|
+// - 错误
|
|
|
func (jsonBody *JsonBody) Unmarshal(output any) error {
|
|
|
jsonBytes, err := jsonBody.Bytes()
|
|
|
if err != nil {
|
|
|
@@ -201,124 +366,88 @@ func (jsonBody *JsonBody) Unmarshal(output any) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (c *Context) GetJsonBody() (*JsonBody, error) {
|
|
|
- body, exist := c.Get(bodyKey)
|
|
|
- if !exist {
|
|
|
- bytesBody, err := c.readOriginBody()
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- jsonBodyMap := make(map[string]any)
|
|
|
- err = json.Unmarshal(bytesBody, &jsonBodyMap)
|
|
|
- if err != nil {
|
|
|
- return nil, errors.New(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- return &JsonBody{
|
|
|
- c: c,
|
|
|
- jsonBodyMap: jsonBodyMap,
|
|
|
- }, nil
|
|
|
- }
|
|
|
-
|
|
|
- switch b := body.(type) {
|
|
|
- case []byte:
|
|
|
- jsonBodyMap := make(map[string]any)
|
|
|
- err := json.Unmarshal(b, &jsonBodyMap)
|
|
|
- if err != nil {
|
|
|
- return nil, errors.New(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- return &JsonBody{
|
|
|
- c: c,
|
|
|
- jsonBodyMap: jsonBodyMap,
|
|
|
- }, nil
|
|
|
- case map[string]any:
|
|
|
- return &JsonBody{
|
|
|
- c: c,
|
|
|
- jsonBodyMap: body.(map[string]any),
|
|
|
- }, nil
|
|
|
- default:
|
|
|
- return nil, errors.New("不支持的body类型")
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
type QueryPrams struct {
|
|
|
c *Context
|
|
|
queryParams map[string]string
|
|
|
}
|
|
|
|
|
|
+// Set 设置查询参数
|
|
|
+// 参数:
|
|
|
+// - key: 查询参数的键
|
|
|
+// - value: 查询参数对应键的值
|
|
|
+// 返回值: 无
|
|
|
func (queryParams *QueryPrams) Set(key string, value string) {
|
|
|
queryParams.queryParams[key] = value
|
|
|
queryParams.c.Set(queryParamsKey, queryParams.queryParams)
|
|
|
}
|
|
|
|
|
|
+// Delete 删除JsonBody键对应的值
|
|
|
+// 参数:
|
|
|
+// - key: JsonBody的键
|
|
|
+// 返回值: 无
|
|
|
func (queryParams *QueryPrams) Delete(key string) {
|
|
|
delete(queryParams.queryParams, key)
|
|
|
queryParams.c.Set(queryParamsKey, queryParams.queryParams)
|
|
|
}
|
|
|
|
|
|
+// Get 获取查询参数对应键的值
|
|
|
+// 参数:
|
|
|
+// - key: 查询参数的键
|
|
|
+// 返回值:
|
|
|
+// - 查询参数对应键的值
|
|
|
func (queryParams *QueryPrams) Get(key string) string {
|
|
|
return queryParams.queryParams[key]
|
|
|
}
|
|
|
|
|
|
+// Map 获取查询参数的map表示
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 查询参数的map表示
|
|
|
func (queryParams *QueryPrams) Map() map[string]string {
|
|
|
return queryParams.queryParams
|
|
|
}
|
|
|
|
|
|
-func (c *Context) GetQueryParams() *QueryPrams {
|
|
|
- queryParams, exist := c.Get(queryParamsKey)
|
|
|
- if !exist {
|
|
|
- return &QueryPrams{
|
|
|
- c: c,
|
|
|
- queryParams: c.getAllQueryParams(),
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return &QueryPrams{
|
|
|
- c: c,
|
|
|
- queryParams: queryParams.(map[string]string),
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
type PathPrams struct {
|
|
|
c *Context
|
|
|
pathParams map[string]string
|
|
|
}
|
|
|
|
|
|
+// Set 设置路径参数
|
|
|
+// 参数:
|
|
|
+// - key: 路径参数的键
|
|
|
+// - value: 路径参数对应键的值
|
|
|
+// 返回值: 无
|
|
|
func (pathParams *PathPrams) Set(key string, value string) {
|
|
|
pathParams.pathParams[key] = value
|
|
|
pathParams.c.Set(pathParamsKey, pathParams.pathParams)
|
|
|
}
|
|
|
|
|
|
+// Delete 删除JsonBody键对应的值
|
|
|
+// 参数:
|
|
|
+// - key: JsonBody的键
|
|
|
+// 返回值: 无
|
|
|
func (pathParams *PathPrams) Delete(key string) {
|
|
|
delete(pathParams.pathParams, key)
|
|
|
pathParams.c.Set(pathParamsKey, pathParams.pathParams)
|
|
|
}
|
|
|
|
|
|
+// Get 获取路径参数对应键的值
|
|
|
+// 参数:
|
|
|
+// - key: 路径参数的键
|
|
|
+// 返回值:
|
|
|
+// - 路径参数对应键的值
|
|
|
func (pathParams *PathPrams) Get(key string) string {
|
|
|
return pathParams.pathParams[key]
|
|
|
}
|
|
|
|
|
|
+// Map 获取路径参数的map表示
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 路径参数的map表示
|
|
|
func (pathParams *PathPrams) Map() map[string]string {
|
|
|
return pathParams.pathParams
|
|
|
}
|
|
|
|
|
|
-func (c *Context) GetPathParams() *PathPrams {
|
|
|
- pathParams, exist := c.Get(pathParamsKey)
|
|
|
- if !exist {
|
|
|
- return &PathPrams{
|
|
|
- c: c,
|
|
|
- pathParams: c.getAllPathParams(),
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return &PathPrams{
|
|
|
- c: c,
|
|
|
- pathParams: pathParams.(map[string]string),
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
func (c *Context) getAllQueryParams() map[string]string {
|
|
|
queryParams := make(map[string]string)
|
|
|
|