Add support for Protobuf format response and unit test (#1479)

`Gin` now have the `protobufBinding` function to check the request format, but didn't have a protobuf response function like `c.YAML()`.
In our company [ByteDance](http://bytedance.com/), the largest internet company using golang in China, we use `gin` to transfer __Protobuf__  instead of __Json__, we have to write some internal library to make some wrappers to achieve that, and the code is not elegant. So we really want such a feature.
This commit is contained in:
aljun
2018-08-19 10:45:56 +08:00
committed by Bo-Yi Wu
parent f856aa85cd
commit efdd3c8b81
6 changed files with 113 additions and 2 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/gin-contrib/sse"
"github.com/gin-gonic/gin/binding"
"github.com/gin-gonic/gin/render"
"github.com/golang/protobuf/proto"
)
// Content-Type MIME of the most common data formats.
@ -845,6 +846,11 @@ func (c *Context) Stream(step func(w io.Writer) bool) {
}
}
// ProtoBuf serializes the given struct as ProtoBuf into the response body.
func (c *Context) ProtoBuf(code int, obj proto.Message) {
c.Render(code, render.ProtoBuf{Data: obj})
}
/************************************/
/******** CONTENT NEGOTIATION *******/
/************************************/