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.
|
|
|
|
|
2019-04-17 17:10:21 +03:00
|
|
|
package msgpack
|
2017-02-23 17:08:37 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-04-17 17:10:21 +03:00
|
|
|
"github.com/gin-gonic/gin/render/common"
|
2017-02-23 17:08:37 +03:00
|
|
|
"github.com/ugorji/go/codec"
|
|
|
|
)
|
|
|
|
|
2019-04-17 17:10:21 +03:00
|
|
|
func init() {
|
|
|
|
common.List["MsgPack"] = NewMsgPack
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-04-17 17:10:21 +03:00
|
|
|
common.WriteContentType(w, msgpackContentType)
|
2017-02-23 17:08:37 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-04-17 17:10:21 +03:00
|
|
|
common.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
|
|
|
}
|
2019-04-17 17:10:21 +03:00
|
|
|
|
|
|
|
//NewMsgPack build a new MsgPack render
|
|
|
|
func NewMsgPack(obj interface{}, _ map[string]string) common.Render {
|
|
|
|
return MsgPack{Data: obj}
|
|
|
|
}
|