OSDN Git Service

feat(warder): add warder backbone (#181)
[bytom/vapor.git] / vendor / github.com / gin-gonic / gin / binding / protobuf.go
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 package binding
6
7 import (
8         "io/ioutil"
9         "net/http"
10
11         "github.com/golang/protobuf/proto"
12 )
13
14 type protobufBinding struct{}
15
16 func (protobufBinding) Name() string {
17         return "protobuf"
18 }
19
20 func (b protobufBinding) Bind(req *http.Request, obj interface{}) error {
21         buf, err := ioutil.ReadAll(req.Body)
22         if err != nil {
23                 return err
24         }
25         return b.BindBody(buf, obj)
26 }
27
28 func (protobufBinding) BindBody(body []byte, obj interface{}) error {
29         if err := proto.Unmarshal(body, obj.(proto.Message)); err != nil {
30                 return err
31         }
32         // Here it's same to return validate(obj), but util now we cann't add
33         // `binding:""` to the struct which automatically generate by gen-proto
34         return nil
35         // return validate(obj)
36 }