-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.go
56 lines (49 loc) · 1.18 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
package binding
import (
"github.com/gofiber/fiber/v3"
"github.com/hopeio/cherry/utils/net/http/binding"
stringsi "github.com/hopeio/cherry/utils/strings"
"net/http"
)
type Binding interface {
Name() string
Bind(fiber.Ctx, interface{}) error
}
type BindingBody interface {
Binding
BindBody([]byte, interface{}) error
}
var (
JSON = jsonBinding{}
XML = xmlBinding{}
Form = formBinding{}
Query = queryBinding{}
FormPost = formPostBinding{}
FormMultipart = formMultipartBinding{}
Uri = uriBinding{}
ProtoBuf = protobufBinding{}
MsgPack = msgpackBinding{}
YAML = yamlBinding{}
Header = headerBinding{}
)
func Default(method string, contentType []byte) Binding {
if method == http.MethodGet {
return Query
}
switch stringsi.BytesToString(contentType) {
case binding.MIMEJSON:
return JSON
case binding.MIMEXML, binding.MIMEXML2:
return XML
case binding.MIMEPROTOBUF:
return ProtoBuf
case binding.MIMEMSGPACK, binding.MIMEMSGPACK2:
return MsgPack
case binding.MIMEYAML:
return YAML
case binding.MIMEMultipartPOSTForm:
return FormMultipart
default: // case MIMEPOSTForm:
return Form
}
}