123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package binding
- import "net/http"
- const (
- MIMEJSON = "application/json"
- MIMEHTML = "text/html"
- MIMEXML = "application/xml"
- MIMEXML2 = "text/xml"
- MIMEPlain = "text/plain"
- MIMEPOSTForm = "application/x-www-form-urlencoded"
- MIMEMultipartPOSTForm = "multipart/form-data"
- MIMEPROTOBUF = "application/x-protobuf"
- MIMEMSGPACK = "application/x-msgpack"
- MIMEMSGPACK2 = "application/msgpack"
- MIMEYAML = "application/x-yaml"
- MIMETOML = "application/toml"
- )
- type Binding interface {
- Name() string
- Bind(*http.Request, any) error
- }
- type BindingBody interface {
- Binding
- BindBody([]byte, any) error
- }
- type BindingUri interface {
- Name() string
- BindUri(map[string][]string, any) error
- }
- type StructValidator interface {
-
-
-
-
-
-
- ValidateStruct(any) error
-
-
- Engine() any
- }
- var Validator StructValidator = &defaultValidator{}
- var (
- JSON = jsonBinding{}
- XML = xmlBinding{}
- Form = formBinding{}
- Query = queryBinding{}
- FormPost = formPostBinding{}
- FormMultipart = formMultipartBinding{}
- ProtoBuf = protobufBinding{}
- MsgPack = msgpackBinding{}
- YAML = yamlBinding{}
- Uri = uriBinding{}
- Header = headerBinding{}
- TOML = tomlBinding{}
- )
- func Default(method, contentType string) Binding {
- if method == http.MethodGet {
- return Form
- }
- switch contentType {
- case MIMEJSON:
- return JSON
- case MIMEXML, MIMEXML2:
- return XML
- case MIMEPROTOBUF:
- return ProtoBuf
- case MIMEMSGPACK, MIMEMSGPACK2:
- return MsgPack
- case MIMEYAML:
- return YAML
- case MIMETOML:
- return TOML
- case MIMEMultipartPOSTForm:
- return FormMultipart
- default:
- return Form
- }
- }
- func validate(obj any) error {
- if Validator == nil {
- return nil
- }
- return Validator.ValidateStruct(obj)
- }
|