|
4 | 4 |
|
5 | 5 | package binding |
6 | 6 |
|
7 | | -import "net/http" |
| 7 | +import ( |
| 8 | + "fmt" |
8 | 9 |
|
9 | | -// Content-Type MIME of the most common data formats. |
10 | | -const ( |
11 | | - MIMEJSON = "application/json" |
12 | | - MIMEHTML = "text/html" |
13 | | - MIMEXML = "application/xml" |
14 | | - MIMEXML2 = "text/xml" |
15 | | - MIMEPlain = "text/plain" |
16 | | - MIMEPOSTForm = "application/x-www-form-urlencoded" |
17 | | - MIMEMultipartPOSTForm = "multipart/form-data" |
18 | | - MIMEPROTOBUF = "application/x-protobuf" |
19 | | - MIMEMSGPACK = "application/x-msgpack" |
20 | | - MIMEMSGPACK2 = "application/msgpack" |
21 | | - MIMEYAML = "application/x-yaml" |
| 10 | + "github.com/gin-gonic/gin/binding/common" |
22 | 11 | ) |
23 | 12 |
|
24 | | -// Binding describes the interface which needs to be implemented for binding the |
25 | | -// data present in the request such as JSON request body, query parameters or |
26 | | -// the form POST. |
27 | | -type Binding interface { |
28 | | - Name() string |
29 | | - Bind(*http.Request, interface{}) error |
30 | | -} |
31 | | - |
32 | | -// BindingBody adds BindBody method to Binding. BindBody is similar with Bind, |
33 | | -// but it reads the body from supplied bytes instead of req.Body. |
34 | | -type BindingBody interface { |
35 | | - Binding |
36 | | - BindBody([]byte, interface{}) error |
37 | | -} |
38 | | - |
39 | | -// BindingUri adds BindUri method to Binding. BindUri is similar with Bind, |
40 | | -// but it read the Params. |
41 | | -type BindingUri interface { |
42 | | - Name() string |
43 | | - BindUri(map[string][]string, interface{}) error |
44 | | -} |
45 | | - |
46 | | -// StructValidator is the minimal interface which needs to be implemented in |
47 | | -// order for it to be used as the validator engine for ensuring the correctness |
48 | | -// of the request. Gin provides a default implementation for this using |
49 | | -// https://github.com/go-playground/validator/tree/v8.18.2. |
50 | | -type StructValidator interface { |
51 | | - // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right. |
52 | | - // If the received type is not a struct, any validation should be skipped and nil must be returned. |
53 | | - // If the received type is a struct or pointer to a struct, the validation should be performed. |
54 | | - // If the struct is not valid or the validation itself fails, a descriptive error should be returned. |
55 | | - // Otherwise nil must be returned. |
56 | | - ValidateStruct(interface{}) error |
57 | | - |
58 | | - // Engine returns the underlying validator engine which powers the |
59 | | - // StructValidator implementation. |
60 | | - Engine() interface{} |
61 | | -} |
62 | | - |
63 | | -// Validator is the default validator which implements the StructValidator |
64 | | -// interface. It uses https://github.com/go-playground/validator/tree/v8.18.2 |
65 | | -// under the hood. |
66 | | -var Validator StructValidator = &defaultValidator{} |
67 | | - |
68 | 13 | // These implement the Binding interface and can be used to bind the data |
69 | 14 | // present in the request to struct instances. |
70 | 15 | var ( |
71 | | - JSON = jsonBinding{} |
72 | | - XML = xmlBinding{} |
73 | 16 | Form = formBinding{} |
74 | 17 | Query = queryBinding{} |
75 | 18 | FormPost = formPostBinding{} |
76 | | - FormMultipart = formMultipartBinding{} |
77 | | - ProtoBuf = protobufBinding{} |
78 | | - MsgPack = msgpackBinding{} |
79 | | - YAML = yamlBinding{} |
80 | 19 | Uri = uriBinding{} |
| 20 | + FormMultipart = formMultipartBinding{} |
81 | 21 | ) |
82 | 22 |
|
83 | 23 | // Default returns the appropriate Binding instance based on the HTTP method |
84 | 24 | // and the content type. |
85 | | -func Default(method, contentType string) Binding { |
| 25 | +func Default(method, contentType string) common.Binding { |
86 | 26 | if method == "GET" { |
87 | 27 | return Form |
88 | 28 | } |
89 | | - |
90 | 29 | switch contentType { |
91 | | - case MIMEJSON: |
92 | | - return JSON |
93 | | - case MIMEXML, MIMEXML2: |
94 | | - return XML |
95 | | - case MIMEPROTOBUF: |
96 | | - return ProtoBuf |
97 | | - case MIMEMSGPACK, MIMEMSGPACK2: |
98 | | - return MsgPack |
99 | | - case MIMEYAML: |
100 | | - return YAML |
101 | | - case MIMEMultipartPOSTForm: |
| 30 | + case common.MIMEMultipartPOSTForm: |
102 | 31 | return FormMultipart |
103 | | - default: // case MIMEPOSTForm: |
104 | | - return Form |
| 32 | + default: |
| 33 | + b, ok := common.List[contentType] |
| 34 | + if !ok { |
| 35 | + return Form //Default to Form |
| 36 | + } |
| 37 | + return b |
105 | 38 | } |
106 | 39 | } |
107 | 40 |
|
108 | | -func validate(obj interface{}) error { |
109 | | - if Validator == nil { |
110 | | - return nil |
| 41 | +//YAML return the binding for yaml if loaded |
| 42 | +func YAML() common.BindingBody { |
| 43 | + return retBinding(common.MIMEYAML) |
| 44 | +} |
| 45 | + |
| 46 | +//JSON return the binding for json if loaded |
| 47 | +func JSON() common.BindingBody { |
| 48 | + return retBinding(common.MIMEJSON) |
| 49 | +} |
| 50 | + |
| 51 | +//XML return the binding for xml if loaded |
| 52 | +func XML() common.BindingBody { |
| 53 | + return retBinding(common.MIMEXML) |
| 54 | +} |
| 55 | + |
| 56 | +//ProtoBuf return the binding for ProtoBuf if loaded |
| 57 | +func ProtoBuf() common.BindingBody { |
| 58 | + return retBinding(common.MIMEPROTOBUF) |
| 59 | +} |
| 60 | + |
| 61 | +//MsgPack return the binding for MsgPack if loaded |
| 62 | +func MsgPack() common.BindingBody { |
| 63 | + return retBinding(common.MIMEMSGPACK) |
| 64 | +} |
| 65 | + |
| 66 | +//retBinding Search for a render and panic on not found |
| 67 | +func retBinding(contentType string) common.BindingBody { |
| 68 | + b, ok := common.List[contentType] |
| 69 | + if !ok { |
| 70 | + panic(fmt.Sprintf("Undefined binding %s", contentType)) |
111 | 71 | } |
112 | | - return Validator.ValidateStruct(obj) |
| 72 | + return b |
113 | 73 | } |
0 commit comments