Skip to content
Myr

Merge branch 'new-binding-validator' of https://github.com/zhing/gin into zhing-new-binding-validator

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Conflicts: binding/binding.go binding/binding_test.go

authored by Javier Provecho Fernandez · committed by Javier Provecho Fernandez · parents 52fcc5d, 04917e8 · Browse files · 5 files+199 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+199 added−0 removed

  • other:+199 added−0 removed

5 files+199 added−0 removed

added:binding/example/test.pb.go+113 added
1// Code generated by protoc-gen-go.2// source: test.proto3// DO NOT EDIT!45/*6Package example is a generated protocol buffer package.78It is generated from these files:9 test.proto1011It has these top-level messages:12 Test13*/14package example1516import proto "github.com/golang/protobuf/proto"17import math "math"1819// Reference imports to suppress errors if they are not otherwise used.20var _ = proto.Marshal21var _ = math.Inf2223type FOO int322425const (26 FOO_X FOO = 1727)2829var FOO_name = map[int32]string{30 17: "X",31}32var FOO_value = map[string]int32{33 "X": 17,34}3536func (x FOO) Enum() *FOO {37 p := new(FOO)38 *p = x39 return p40}41func (x FOO) String() string {42 return proto.EnumName(FOO_name, int32(x))43}44func (x *FOO) UnmarshalJSON(data []byte) error {45 value, err := proto.UnmarshalJSONEnum(FOO_value, data, "FOO")46 if err != nil {47 return err48 }49 *x = FOO(value)50 return nil51}5253type Test struct {54 Label *string `protobuf:"bytes,1,req,name=label" json:"label,omitempty"`55 Type *int32 `protobuf:"varint,2,opt,name=type,def=77" json:"type,omitempty"`56 Reps []int64 `protobuf:"varint,3,rep,name=reps" json:"reps,omitempty"`57 Optionalgroup *Test_OptionalGroup `protobuf:"group,4,opt,name=OptionalGroup" json:"optionalgroup,omitempty"`58 XXX_unrecognized []byte `json:"-"`59}6061func (m *Test) Reset() { *m = Test{} }62func (m *Test) String() string { return proto.CompactTextString(m) }63func (*Test) ProtoMessage() {}6465const Default_Test_Type int32 = 776667func (m *Test) GetLabel() string {68 if m != nil && m.Label != nil {69 return *m.Label70 }71 return ""72}7374func (m *Test) GetType() int32 {75 if m != nil && m.Type != nil {76 return *m.Type77 }78 return Default_Test_Type79}8081func (m *Test) GetReps() []int64 {82 if m != nil {83 return m.Reps84 }85 return nil86}8788func (m *Test) GetOptionalgroup() *Test_OptionalGroup {89 if m != nil {90 return m.Optionalgroup91 }92 return nil93}9495type Test_OptionalGroup struct {96 RequiredField *string `protobuf:"bytes,5,req" json:"RequiredField,omitempty"`97 XXX_unrecognized []byte `json:"-"`98}99100func (m *Test_OptionalGroup) Reset() { *m = Test_OptionalGroup{} }101func (m *Test_OptionalGroup) String() string { return proto.CompactTextString(m) }102func (*Test_OptionalGroup) ProtoMessage() {}103104func (m *Test_OptionalGroup) GetRequiredField() string {105 if m != nil && m.RequiredField != nil {106 return *m.RequiredField107 }108 return ""109}110111func init() {112 proto.RegisterEnum("example.FOO", FOO_name, FOO_value)113}
added:binding/example/test.proto+12 added
1package example;23enum FOO {X=17;};45message Test {6 required string label = 1;7 optional int32 type = 2[default=77];8 repeated int64 reps = 3;9 optional group OptionalGroup = 4{10 required string RequiredField = 5;11 }12}
modified:binding/binding_test.go+35 added
8 unmodified lines
9 "mime/multipart"10 "net/http"11 "testing"1213 "github.com/gin-gonic/gin/binding/example"14 "github.com/golang/protobuf/proto"1516 "github.com/stretchr/testify/assert"17)1819type FooStruct struct {
19 unmodified lines
39 assert.Equal(t, Default("PUT", MIMEPOSTForm), Form)4041 assert.Equal(t, Default("POST", MIMEMultipartPOSTForm), Form)42 assert.Equal(t, Default("PUT", MIMEMultipartPOSTForm), Form)4344 assert.Equal(t, Default("POST", MIMEPROTOBUF), ProtoBuf)45 assert.Equal(t, Default("PUT", MIMEPROTOBUF), ProtoBuf)46}4748func TestBindingJSON(t *testing.T) {49 testBodyBinding(t,
58 unmodified lines
108 assert.Equal(t, obj.Foo, "bar")109 assert.Equal(t, obj.Bar, "foo")110}111112func TestBindingProtoBuf(t *testing.T) {113 test := &example.Test{114 Label: proto.String("yes"),115 }116 data, _ := proto.Marshal(test)117118 testProtoBodyBinding(t,119 ProtoBuf, "protobuf",120 "/", "/",121 string(data), string(data[1:]))122}123124func TestValidationFails(t *testing.T) {125 var obj FooStruct126 req := requestWithBody("POST", "/", `{"bar": "foo"}`)127 err := JSON.Bind(req, &obj)
45 unmodified lines
173 err = JSON.Bind(req, &obj)174 assert.Error(t, err)175}176177func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {178 assert.Equal(t, b.Name(), name)179180 obj := example.Test{}181 req := requestWithBody("POST", path, body)182 req.Header.Add("Content-Type", MIMEPROTOBUF)183 err := b.Bind(req, &obj)184 assert.NoError(t, err)185 assert.Equal(t, *obj.Label, "yes")186187 obj = example.Test{}188 req = requestWithBody("POST", badPath, badBody)189 req.Header.Add("Content-Type", MIMEPROTOBUF)190 err = ProtoBuf.Bind(req, &obj)191 assert.Error(t, err)192}193194func requestWithBody(method, path, body string) (req *http.Request) {195 req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))196 return197}
modified:binding/binding.go+4 added
12 unmodified lines
13 MIMEXML2 = "text/xml"14 MIMEPlain = "text/plain"15 MIMEPOSTForm = "application/x-www-form-urlencoded"16 MIMEMultipartPOSTForm = "multipart/form-data"17 MIMEPROTOBUF = "application/x-protobuf"18)1920type Binding interface {21 Name() string
16 unmodified lines
38 XML = xmlBinding{}39 Form = formBinding{}40 FormPost = formPostBinding{}41 FormMultipart = formMultipartBinding{}42 ProtoBuf = protobufBinding{}43)4445func Default(method, contentType string) Binding {46 if method == "GET" {
3 unmodified lines
50 case MIMEJSON:51 return JSON52 case MIMEXML, MIMEXML2:53 return XML54 case MIMEPROTOBUF:55 return ProtoBuf56 default: //case MIMEPOSTForm, MIMEMultipartPOSTForm:57 return Form58 }59 }
8 unmodified lines
added:binding/protobuf.go+35 added
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package binding67import (8 "github.com/golang/protobuf/proto"910 "io/ioutil"11 "net/http"12)1314type protobufBinding struct{}1516func (_ protobufBinding) Name() string {17 return "protobuf"18}1920func (_ protobufBinding) Bind(req *http.Request, obj interface{}) error {2122 buf, err := ioutil.ReadAll(req.Body)23 if err != nil {24 return err25 }2627 if err = proto.Unmarshal(buf, obj.(proto.Message)); err != nil {28 return err29 }3031 //Here it's same to return validate(obj), but util now we cann't add `binding:""` to the struct32 //which automatically generate by gen-proto33 return nil34 //return validate(obj)35}