Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
ContentTypeXML
ContentTypeForm
ContentTypeEventStream
ContentMsgPack
)

func GetContentType(s string) ContentType {
Expand All @@ -39,6 +40,8 @@ func GetContentType(s string) ContentType {
return ContentTypeForm
case "text/event-stream":
return ContentTypeEventStream
case "application/msgpack":
return ContentMsgPack
default:
return ContentTypeUnknown
}
Expand Down
9 changes: 9 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"encoding/xml"
"errors"
"github.com/vmihailenco/msgpack/v5"
"io"
"net/http"

Expand All @@ -30,6 +31,8 @@ func DefaultDecoder(r *http.Request, v interface{}) error {
err = DecodeXML(r.Body, v)
case ContentTypeForm:
err = DecodeForm(r.Body, v)
case ContentMsgPack:
err = DecodeMsgPack(r.Body, v)
default:
err = errors.New("render: unable to automatically decode the request content type")
}
Expand All @@ -54,3 +57,9 @@ func DecodeForm(r io.Reader, v interface{}) error {
decoder := form.NewDecoder(r) //nolint:errcheck
return decoder.Decode(v)
}

// DecodeMsgPack decodes a given reader into an interface using the msgpack decoder.
func DecodeMsgPack(r io.Reader, v interface{}) error {
defer io.Copy(io.Discard, r) //nolint:errcheck
return msgpack.NewDecoder(r).Decode(v)
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/go-chi/render

go 1.16

require github.com/ajg/form v1.5.1
require (
github.com/ajg/form v1.5.1
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=