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
// +build !nomsgpack
6

7
package binding
8

9
import (
10
	"bytes"
11
	"io"
12
	"net/http"
13

14
	"github.com/ugorji/go/codec"
15
)
16

17
type msgpackBinding struct{}
18

19
func (msgpackBinding) Name() string {
20 6
	return "msgpack"
21
}
22

23
func (msgpackBinding) Bind(req *http.Request, obj interface{}) error {
24 6
	return decodeMsgPack(req.Body, obj)
25
}
26

27
func (msgpackBinding) BindBody(body []byte, obj interface{}) error {
28 6
	return decodeMsgPack(bytes.NewReader(body), obj)
29
}
30

31
func decodeMsgPack(r io.Reader, obj interface{}) error {
32 6
	cdc := new(codec.MsgpackHandle)
33 6
	if err := codec.NewDecoder(r, cdc).Decode(&obj); err != nil {
34 6
		return err
35
	}
36 6
	return validate(obj)
37
}

Read our documentation on viewing source code .

Loading