msgpack.go 792 B

12345678910111213141516171819202122232425262728293031323334353637
  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. // +build !nomsgpack
  5. package binding
  6. import (
  7. "bytes"
  8. "io"
  9. "net/http"
  10. "github.com/ugorji/go/codec"
  11. )
  12. type msgpackBinding struct{}
  13. func (msgpackBinding) Name() string {
  14. return "msgpack"
  15. }
  16. func (msgpackBinding) Bind(req *http.Request, obj interface{}) error {
  17. return decodeMsgPack(req.Body, obj)
  18. }
  19. func (msgpackBinding) BindBody(body []byte, obj interface{}) error {
  20. return decodeMsgPack(bytes.NewReader(body), obj)
  21. }
  22. func decodeMsgPack(r io.Reader, obj interface{}) error {
  23. cdc := new(codec.MsgpackHandle)
  24. if err := codec.NewDecoder(r, cdc).Decode(&obj); err != nil {
  25. return err
  26. }
  27. return validate(obj)
  28. }