-
Notifications
You must be signed in to change notification settings - Fork 6
/
binding.go
executable file
·106 lines (105 loc) · 2.83 KB
/
binding.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package httpx
//var (
// BindingProtoBuf = protobufBinding{}
// BindingJSON = jsonBinding{}
// BindingXML = binding.XML
// BindingForm = binding.Form
// BindingQuery = binding.Query
// BindingFormPost = binding.FormPost
// BindingFormMultipart = binding.FormMultipart
// BindingMsgPack = binding.MsgPack
// BindingYAML = binding.YAML
// BindingUri = binding.Uri
//)
//
//func Bind(c *gin.Context, obj interface{}) error {
// return c.ShouldBindWith(obj, GetBinding(c))
//}
//
//func GetBinding(c *gin.Context) binding.Binding {
// method, contentType := c.Request.Method, c.ContentType()
// if method == "GET" {
// return BindingForm
// }
//
// switch contentType {
// case binding.MIMEJSON:
// return BindingJSON
// case binding.MIMEXML, binding.MIMEXML2:
// return BindingXML
// case binding.MIMEPROTOBUF:
// return BindingProtoBuf
// case binding.MIMEMSGPACK, binding.MIMEMSGPACK2:
// return BindingMsgPack
// case binding.MIMEYAML:
// return BindingYAML
// default: // case MIMEPOSTForm, MIMEMultipartPOSTForm:
// return BindingForm
// }
//}
//
//type protobufBinding struct{}
//
//func (protobufBinding) Name() string {
// return "protobuf"
//}
//
//func (b protobufBinding) Bind(req *http.Request, obj interface{}) error {
// buf, err := io.ReadAll(req.Body)
// if err != nil {
// return err
// }
// return b.BindBody(buf, obj)
//}
//
//func (protobufBinding) BindBody(body []byte, obj interface{}) error {
// if err := proto.Unmarshal(body, obj.(proto.Message)); err != nil {
// return err
// }
// // Here it's same to return validate(obj), but util now we can't add
// // `binding:""` to the struct which automatically generate by gen-proto
// return nil
// // return validate(obj)
//}
//
//// EnableDecoderUseNumber is used to call the UseNumber method on the JSON
//// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
//// interface{} as a Number instead of as a float64.
//var EnableDecoderUseNumber = false
//
//var json = jsoniter.ConfigCompatibleWithStandardLibrary
//
//type jsonBinding struct{}
//
//func (jsonBinding) Name() string {
// return "json"
//}
//
//func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
// if req == nil || req.Body == nil {
// return fmt.Errorf("invalid request")
// }
// return decodeJSON(req.Body, obj)
//}
//
//func (jsonBinding) BindBody(body []byte, obj interface{}) error {
// return decodeJSON(bytes.NewReader(body), obj)
//}
//
//func decodeJSON(r io.Reader, obj interface{}) error {
// decoder := json.NewDecoder(r)
// if EnableDecoderUseNumber {
// decoder.UseNumber()
// }
// if err := decoder.Decode(obj); err != nil {
// return err
// }
// return validate(obj)
//}
//
//func validate(obj interface{}) error {
// if binding.Validator == nil {
// return nil
// }
// return binding.Validator.ValidateStruct(obj)
//}