gin-gonic / gin
1
// Copyright 2018 Gin Core Team.  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 render
6

7
import (
8
	"net/http"
9

10
	"github.com/golang/protobuf/proto"
11
)
12

13
// ProtoBuf contains the given interface object.
14
type ProtoBuf struct {
15
	Data interface{}
16
}
17

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

20
// Render (ProtoBuf) marshals the given interface object and writes data with custom ContentType.
21
func (r ProtoBuf) Render(w http.ResponseWriter) error {
22 151
	r.WriteContentType(w)
23

24 151
	bytes, err := proto.Marshal(r.Data.(proto.Message))
25 151
	if err != nil {
26 151
		return err
27
	}
28

29 151
	_, err = w.Write(bytes)
30 151
	return err
31
}
32

33
// WriteContentType (ProtoBuf) writes ProtoBuf ContentType.
34
func (r ProtoBuf) WriteContentType(w http.ResponseWriter) {
35 151
	writeContentType(w, protobufContentType)
36
}

Read our documentation on viewing source code .

Loading