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