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//go:build !nomsgpack
6// +build !nomsgpack
7
8package render
9
10import (
11	"net/http"
12
13	"github.com/ugorji/go/codec"
14)
15
16var (
17	_ Render = MsgPack{}
18)
19
20// MsgPack contains the given interface object.
21type MsgPack struct {
22	Data interface{}
23}
24
25var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
26
27// WriteContentType (MsgPack) writes MsgPack ContentType.
28func (r MsgPack) WriteContentType(w http.ResponseWriter) {
29	writeContentType(w, msgpackContentType)
30}
31
32// Render (MsgPack) encodes the given interface object and writes data with custom ContentType.
33func (r MsgPack) Render(w http.ResponseWriter) error {
34	return WriteMsgPack(w, r.Data)
35}
36
37// WriteMsgPack writes MsgPack ContentType and encodes the given interface object.
38func WriteMsgPack(w http.ResponseWriter, obj interface{}) error {
39	writeContentType(w, msgpackContentType)
40	var mh codec.MsgpackHandle
41	return codec.NewEncoder(w, &mh).Encode(obj)
42}
43