Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not use protobuf on context but use it on render #1496

Merged
merged 1 commit into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
not use protobuf on context but use it on render
  • Loading branch information
thinkerou committed Aug 19, 2018
commit 2b3ee95af38e309a8795397dafd5e85c36b61921
11 changes: 5 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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.
Expand Down Expand Up @@ -784,6 +783,11 @@ func (c *Context) YAML(code int, obj interface{}) {
c.Render(code, render.YAML{Data: obj})
}

// ProtoBuf serializes the given struct as ProtoBuf into the response body.
func (c *Context) ProtoBuf(code int, obj interface{}) {
c.Render(code, render.ProtoBuf{Data: obj})
}

// String writes the given string into the response body.
func (c *Context) String(code int, format string, values ...interface{}) {
c.Render(code, render.String{Format: format, Data: values})
Expand Down Expand Up @@ -846,11 +850,6 @@ 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 *******/
/************************************/
Expand Down
Empty file modified render/json.go
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions render/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
)

type ProtoBuf struct {
Data proto.Message
Data interface{}
}

var protobufContentType = []string{"application/x-protobuf"}

func (r ProtoBuf) Render(w http.ResponseWriter) error {
r.WriteContentType(w)

bytes, err := proto.Marshal(r.Data)
bytes, err := proto.Marshal(r.Data.(proto.Message))
if err != nil {
return err
}
Expand Down
Empty file modified render/render.go
100755 → 100644
Empty file.
3 changes: 2 additions & 1 deletion render/render_test.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
"strings"
"testing"

testdata "github.com/gin-gonic/gin/testdata/protoexample"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/ugorji/go/codec"

testdata "github.com/gin-gonic/gin/testdata/protoexample"
)

// TODO unit tests
Expand Down