2017-02-23 14:08:37 +00: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 01:09:44 +00:00
|
|
|
//go:build !nomsgpack
|
2020-01-07 03:31:10 +00:00
|
|
|
// +build !nomsgpack
|
|
|
|
|
2017-02-23 14:08:37 +00:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/ugorji/go/codec"
|
|
|
|
)
|
|
|
|
|
2021-06-25 04:14:06 +00:00
|
|
|
// Check interface implemented here to support go build tag nomsgpack.
|
|
|
|
// See: https://github.com/gin-gonic/gin/pull/1852/
|
2020-01-07 03:31:10 +00:00
|
|
|
var (
|
|
|
|
_ Render = MsgPack{}
|
|
|
|
)
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// MsgPack contains the given interface object.
|
2017-02-23 14:08:37 +00:00
|
|
|
type MsgPack struct {
|
2022-03-21 01:43:17 +00:00
|
|
|
Data any
|
2017-02-23 14:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// WriteContentType (MsgPack) writes MsgPack ContentType.
|
2017-02-23 14:08:37 +00:00
|
|
|
func (r MsgPack) WriteContentType(w http.ResponseWriter) {
|
|
|
|
writeContentType(w, msgpackContentType)
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// Render (MsgPack) encodes the given interface object and writes data with custom ContentType.
|
2017-02-23 14:08:37 +00:00
|
|
|
func (r MsgPack) Render(w http.ResponseWriter) error {
|
|
|
|
return WriteMsgPack(w, r.Data)
|
|
|
|
}
|
|
|
|
|
2018-09-15 02:23:32 +00:00
|
|
|
// WriteMsgPack writes MsgPack ContentType and encodes the given interface object.
|
2022-03-21 01:43:17 +00:00
|
|
|
func WriteMsgPack(w http.ResponseWriter, obj any) error {
|
2017-02-23 14:08:37 +00:00
|
|
|
writeContentType(w, msgpackContentType)
|
2018-08-19 14:14:02 +00:00
|
|
|
var mh codec.MsgpackHandle
|
|
|
|
return codec.NewEncoder(w, &mh).Encode(obj)
|
2017-02-23 14:08:37 +00:00
|
|
|
}
|