msgpack.go 1015 B

1234567891011121314151617181920212223242526272829303132333435
  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. package render
  5. import (
  6. "net/http"
  7. "github.com/ugorji/go/codec"
  8. )
  9. // MsgPack contains the given interface object.
  10. type MsgPack struct {
  11. Data interface{}
  12. }
  13. var msgpackContentType = []string{"application/msgpack; charset=utf-8"}
  14. // WriteContentType (MsgPack) writes MsgPack ContentType.
  15. func (r MsgPack) WriteContentType(w http.ResponseWriter) {
  16. writeContentType(w, msgpackContentType)
  17. }
  18. // Render (MsgPack) encodes the given interface object and writes data with custom ContentType.
  19. func (r MsgPack) Render(w http.ResponseWriter) error {
  20. return WriteMsgPack(w, r.Data)
  21. }
  22. // WriteMsgPack writes MsgPack ContentType and encodes the given interface object.
  23. func WriteMsgPack(w http.ResponseWriter, obj interface{}) error {
  24. writeContentType(w, msgpackContentType)
  25. var mh codec.MsgpackHandle
  26. return codec.NewEncoder(w, &mh).Encode(obj)
  27. }