github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/render/msgpack.go (about)

     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  
     8  package render
     9  
    10  import (
    11  	"github.com/hellobchain/newcryptosm/http"
    12  
    13  	"github.com/ugorji/go/codec"
    14  )
    15  
    16  var (
    17  	_ Render = MsgPack{}
    18  )
    19  
    20  // MsgPack contains the given interface object.
    21  type MsgPack struct {
    22  	Data interface{}
    23  }
    24  
    25  var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
    26  
    27  // WriteContentType (MsgPack) writes MsgPack ContentType.
    28  func (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.
    33  func (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.
    38  func 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  }