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
Provenance
- Commit
e5339a3- Tree
23cae4b- Author
Javier Provecho Fernandez <javiertitan@gmail.com>- Committer
Javier Provecho Fernandez <javiertitan@gmail.com>- Parent 1
52fcc5d- Parent 2
04917e8
5 files changed+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!4+5+/*6+Package example is a generated protocol buffer package.7+8+It is generated from these files:9+ test.proto10+11+It has these top-level messages:12+ Test13+*/14+package example15+16+import proto "github.com/golang/protobuf/proto"17+import math "math"18+19+// Reference imports to suppress errors if they are not otherwise used.20+var _ = proto.Marshal21+var _ = math.Inf22+23+type FOO int3224+25+const (26+ FOO_X FOO = 1727+)28+29+var FOO_name = map[int32]string{30+ 17: "X",31+}32+var FOO_value = map[string]int32{33+ "X": 17,34+}35+36+func (x FOO) Enum() *FOO {37+ p := new(FOO)38+ *p = x39+ return p40+}41+func (x FOO) String() string {42+ return proto.EnumName(FOO_name, int32(x))43+}44+func (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+}52+53+type 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+}60+61+func (m *Test) Reset() { *m = Test{} }62+func (m *Test) String() string { return proto.CompactTextString(m) }63+func (*Test) ProtoMessage() {}64+65+const Default_Test_Type int32 = 7766+67+func (m *Test) GetLabel() string {68+ if m != nil && m.Label != nil {69+ return *m.Label70+ }71+ return ""72+}73+74+func (m *Test) GetType() int32 {75+ if m != nil && m.Type != nil {76+ return *m.Type77+ }78+ return Default_Test_Type79+}80+81+func (m *Test) GetReps() []int64 {82+ if m != nil {83+ return m.Reps84+ }85+ return nil86+}87+88+func (m *Test) GetOptionalgroup() *Test_OptionalGroup {89+ if m != nil {90+ return m.Optionalgroup91+ }92+ return nil93+}94+95+type Test_OptionalGroup struct {96+ RequiredField *string `protobuf:"bytes,5,req" json:"RequiredField,omitempty"`97+ XXX_unrecognized []byte `json:"-"`98+}99+100+func (m *Test_OptionalGroup) Reset() { *m = Test_OptionalGroup{} }101+func (m *Test_OptionalGroup) String() string { return proto.CompactTextString(m) }102+func (*Test_OptionalGroup) ProtoMessage() {}103+104+func (m *Test_OptionalGroup) GetRequiredField() string {105+ if m != nil && m.RequiredField != nil {106+ return *m.RequiredField107+ }108+ return ""109+}110+111+func init() {112+ proto.RegisterEnum("example.FOO", FOO_name, FOO_value)113+}added:binding/example/test.proto+12 added
1+package example;2+3+enum FOO {X=17;};4+5+message 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 lines9 "mime/multipart"10 "net/http"11 "testing"12 13+ "github.com/gin-gonic/gin/binding/example"14+ "github.com/golang/protobuf/proto"15+16 "github.com/stretchr/testify/assert"17 )18 19 type FooStruct struct {19 unmodified lines39 assert.Equal(t, Default("PUT", MIMEPOSTForm), Form)40 41 assert.Equal(t, Default("POST", MIMEMultipartPOSTForm), Form)42 assert.Equal(t, Default("PUT", MIMEMultipartPOSTForm), Form)43+44+ assert.Equal(t, Default("POST", MIMEPROTOBUF), ProtoBuf)45+ assert.Equal(t, Default("PUT", MIMEPROTOBUF), ProtoBuf)46 }47 48 func TestBindingJSON(t *testing.T) {49 testBodyBinding(t,58 unmodified lines108 assert.Equal(t, obj.Foo, "bar")109 assert.Equal(t, obj.Bar, "foo")110 }111 112+func TestBindingProtoBuf(t *testing.T) {113+ test := &example.Test{114+ Label: proto.String("yes"),115+ }116+ data, _ := proto.Marshal(test)117+118+ testProtoBodyBinding(t,119+ ProtoBuf, "protobuf",120+ "/", "/",121+ string(data), string(data[1:]))122+}123+124 func TestValidationFails(t *testing.T) {125 var obj FooStruct126 req := requestWithBody("POST", "/", `{"bar": "foo"}`)127 err := JSON.Bind(req, &obj)45 unmodified lines173 err = JSON.Bind(req, &obj)174 assert.Error(t, err)175 }176 177+func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {178+ assert.Equal(t, b.Name(), name)179+180+ 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")186+187+ 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+}193+194 func 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 lines13 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 )19 20 type Binding interface {21 Name() string16 unmodified lines38 XML = xmlBinding{}39 Form = formBinding{}40 FormPost = formPostBinding{}41 FormMultipart = formMultipartBinding{}42+ ProtoBuf = protobufBinding{}43 )44 45 func Default(method, contentType string) Binding {46 if method == "GET" {3 unmodified lines50 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 linesadded: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.4+5+package binding6+7+import (8+ "github.com/golang/protobuf/proto"9+10+ "io/ioutil"11+ "net/http"12+)13+14+type protobufBinding struct{}15+16+func (_ protobufBinding) Name() string {17+ return "protobuf"18+}19+20+func (_ protobufBinding) Bind(req *http.Request, obj interface{}) error {21+22+ buf, err := ioutil.ReadAll(req.Body)23+ if err != nil {24+ return err25+ }26+27+ if err = proto.Unmarshal(buf, obj.(proto.Message)); err != nil {28+ return err29+ }30+31+ //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+}