-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodec.go
66 lines (60 loc) · 1.48 KB
/
codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package gin_tpl
// @author: xs
// @date: 2022/3/4
// @Description: 数据解析
package gin_tpl
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"net/http"
)
// DecodeRequestFunc is decode request func.
type DecodeRequestFunc func(*gin.Context, interface{}) error
type EncodeResponseFunc func(*gin.Context, interface{}, error)
// DefaultRequestDecoder decodes the request body to object.
func DefaultRequestDecoder(c *gin.Context, v interface{}) error {
switch c.Request.Method {
case http.MethodPost, http.MethodPut:
bin := getBindingBody(c)
if err := c.ShouldBindBodyWith(v, bin); err != nil {
return err
}
case http.MethodGet, http.MethodDelete:
if err := c.ShouldBindQuery(v); err != nil {
return err
}
}
return nil
}
// getBindingBody 获取绑定类型
func getBindingBody(c *gin.Context) binding.BindingBody {
b := binding.Default(c.Request.Method, c.ContentType())
var bin binding.BindingBody
switch b.Name() {
case "json":
bin = binding.JSON
case "xml":
bin = binding.XML
case "yaml":
bin = binding.YAML
case "protobuf":
bin = binding.ProtoBuf
case "msgpack":
bin = binding.MsgPack
default:
bin = binding.JSON
}
return bin
}
// DefaultResponseEncoder encodes the object to the HTTP response.
func DefaultResponseEncoder(c *gin.Context, obj interface{}, err error) {
// 默认输出逻辑
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"msg": err.Error(),
})
return
} else {
c.JSON(http.StatusOK, obj)
}
}