msgpack.go 814 B

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