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