...
1
2
3
4
5
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 return "msgpack"
21 }
22
23 func (msgpackBinding) Bind(req *http.Request, obj any) error {
24 return decodeMsgPack(req.Body, obj)
25 }
26
27 func (msgpackBinding) BindBody(body []byte, obj any) error {
28 return decodeMsgPack(bytes.NewReader(body), obj)
29 }
30
31 func decodeMsgPack(r io.Reader, obj any) error {
32 cdc := new(codec.MsgpackHandle)
33 if err := codec.NewDecoder(r, cdc).Decode(&obj); err != nil {
34 return err
35 }
36 return validate(obj)
37 }
38
View as plain text