Skip to content

Commit 54eda7f

Browse files
committed
binding/render: modular deps
1 parent ffcbe77 commit 54eda7f

36 files changed

+715
-394
lines changed

binding/binding.go

Lines changed: 43 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,110 +4,70 @@
44

55
package binding
66

7-
import "net/http"
7+
import (
8+
"fmt"
89

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"
2211
)
2312

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-
6813
// These implement the Binding interface and can be used to bind the data
6914
// present in the request to struct instances.
7015
var (
71-
JSON = jsonBinding{}
72-
XML = xmlBinding{}
7316
Form = formBinding{}
7417
Query = queryBinding{}
7518
FormPost = formPostBinding{}
76-
FormMultipart = formMultipartBinding{}
77-
ProtoBuf = protobufBinding{}
78-
MsgPack = msgpackBinding{}
79-
YAML = yamlBinding{}
8019
Uri = uriBinding{}
20+
FormMultipart = formMultipartBinding{}
8121
)
8222

8323
// Default returns the appropriate Binding instance based on the HTTP method
8424
// and the content type.
85-
func Default(method, contentType string) Binding {
25+
func Default(method, contentType string) common.Binding {
8626
if method == "GET" {
8727
return Form
8828
}
89-
9029
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:
10231
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
10538
}
10639
}
10740

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))
11171
}
112-
return Validator.ValidateStruct(obj)
72+
return b
11373
}

binding/binding_body_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"testing"
77

8+
"github.com/gin-gonic/gin/binding/common"
89
"github.com/gin-gonic/gin/testdata/protoexample"
910
"github.com/golang/protobuf/proto"
1011
"github.com/stretchr/testify/assert"
@@ -14,31 +15,31 @@ import (
1415
func TestBindingBody(t *testing.T) {
1516
for _, tt := range []struct {
1617
name string
17-
binding BindingBody
18+
binding common.BindingBody
1819
body string
1920
want string
2021
}{
2122
{
2223
name: "JSON binding",
23-
binding: JSON,
24+
binding: JSON(),
2425
body: `{"foo":"FOO"}`,
2526
},
2627
{
2728
name: "XML binding",
28-
binding: XML,
29+
binding: XML(),
2930
body: `<?xml version="1.0" encoding="UTF-8"?>
3031
<root>
3132
<foo>FOO</foo>
3233
</root>`,
3334
},
3435
{
3536
name: "MsgPack binding",
36-
binding: MsgPack,
37+
binding: MsgPack(),
3738
body: msgPackBody(t),
3839
},
3940
{
4041
name: "YAML binding",
41-
binding: YAML,
42+
binding: YAML(),
4243
body: `foo: FOO`,
4344
},
4445
} {
@@ -67,6 +68,6 @@ func TestBindingBodyProto(t *testing.T) {
6768
req := requestWithBody("POST", "/", string(data))
6869
form := protoexample.Test{}
6970
body, _ := ioutil.ReadAll(req.Body)
70-
assert.NoError(t, ProtoBuf.BindBody(body, &form))
71+
assert.NoError(t, ProtoBuf().BindBody(body, &form))
7172
assert.Equal(t, test, form)
7273
}

0 commit comments

Comments
 (0)