gin-gonic / gin
1
// Copyright 2017 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
// +build !nomsgpack
6

7
package render
8

9
import (
10
	"net/http"
11

12
	"github.com/ugorji/go/codec"
13
)
14

15
var (
16
	_ Render = MsgPack{}
17
)
18

19
// MsgPack contains the given interface object.
20
type MsgPack struct {
21
	Data interface{}
22
}
23

24
var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
25

26
// WriteContentType (MsgPack) writes MsgPack ContentType.
27
func (r MsgPack) WriteContentType(w http.ResponseWriter) {
28 94
	writeContentType(w, msgpackContentType)
29
}
30

31
// Render (MsgPack) encodes the given interface object and writes data with custom ContentType.
32
func (r MsgPack) Render(w http.ResponseWriter) error {
33 94
	return WriteMsgPack(w, r.Data)
34
}
35

36
// WriteMsgPack writes MsgPack ContentType and encodes the given interface object.
37
func WriteMsgPack(w http.ResponseWriter, obj interface{}) error {
38 94
	writeContentType(w, msgpackContentType)
39 94
	var mh codec.MsgpackHandle
40 94
	return codec.NewEncoder(w, &mh).Encode(obj)
41
}

Read our documentation on viewing source code .

Loading