gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

msgpack.go (768B)


      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 
      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 }