2017-02-23 17:08:37 +03:00
|
|
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-03-27 04:09:44 +03:00
|
|
|
//go:build !nomsgpack
|
2020-01-07 06:31:10 +03:00
|
|
|
// +build !nomsgpack
|
|
|
|
|
2017-02-23 17:08:37 +03:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/ugorji/go/codec"
|
|
|
|
)
|
|
|
|
|
2020-01-07 06:31:10 +03:00
|
|
|
var (
|
|
|
|
_ Render = MsgPack{}
|
|
|
|
)
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// MsgPack contains the given interface object.
|
2017-02-23 17:08:37 +03:00
|
|
|
type MsgPack struct {
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteContentType (MsgPack) writes MsgPack ContentType.
|
2017-02-23 17:08:37 +03:00
|
|
|
func (r MsgPack) WriteContentType(w http.ResponseWriter) {
|
|
|
|
writeContentType(w, msgpackContentType)
|
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// Render (MsgPack) encodes the given interface object and writes data with custom ContentType.
|
2017-02-23 17:08:37 +03:00
|
|
|
func (r MsgPack) Render(w http.ResponseWriter) error {
|
|
|
|
return WriteMsgPack(w, r.Data)
|
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteMsgPack writes MsgPack ContentType and encodes the given interface object.
|
2017-02-23 17:08:37 +03:00
|
|
|
func WriteMsgPack(w http.ResponseWriter, obj interface{}) error {
|
|
|
|
writeContentType(w, msgpackContentType)
|
2018-08-19 17:14:02 +03:00
|
|
|
var mh codec.MsgpackHandle
|
|
|
|
return codec.NewEncoder(w, &mh).Encode(obj)
|
2017-02-23 17:08:37 +03:00
|
|
|
}
|