Skip to content

Commit

Permalink
remove unnecessary reflection usage on context#UnmarshalBody
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Feb 23, 2018
1 parent cfd33bc commit 4b9e414
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -53,16 +52,16 @@ type (
Decode(data []byte) error
}

// Unmarshaler is the interface implemented by types that can unmarshal any raw data
// TIP INFO: Any v object which implements the BodyDecoder can be override the unmarshaler.
// Unmarshaler is the interface implemented by types that can unmarshal any raw data.
// TIP INFO: Any pointer to a value which implements the BodyDecoder can be override the unmarshaler.
Unmarshaler interface {
Unmarshal(data []byte, v interface{}) error
Unmarshal(data []byte, outPtr interface{}) error
}

// UnmarshalerFunc a shortcut for the Unmarshaler interface
//
// See 'Unmarshaler' and 'BodyDecoder' for more.
UnmarshalerFunc func(data []byte, v interface{}) error
UnmarshalerFunc func(data []byte, outPtr interface{}) error
)

// Unmarshal parses the X-encoded data and stores the result in the value pointed to by v.
Expand Down Expand Up @@ -596,16 +595,16 @@ type Context interface {
// should be called before reading the request body from the client.
SetMaxRequestBodySize(limitOverBytes int64)

// UnmarshalBody reads the request's body and binds it to a value or pointer of any type
// UnmarshalBody reads the request's body and binds it to a value or pointer of any type.
// Examples of usage: context.ReadJSON, context.ReadXML.
UnmarshalBody(v interface{}, unmarshaler Unmarshaler) error
// ReadJSON reads JSON from request's body and binds it to a value of any json-valid type.
ReadJSON(jsonObject interface{}) error
// ReadXML reads XML from request's body and binds it to a value of any xml-valid type.
ReadXML(xmlObject interface{}) error
UnmarshalBody(outPtr interface{}, unmarshaler Unmarshaler) error
// ReadJSON reads JSON from request's body and binds it to a pointer of a value of any json-valid type.
ReadJSON(jsonObjectPtr interface{}) error
// ReadXML reads XML from request's body and binds it to a pointer of a value of any xml-valid type.
ReadXML(xmlObjectPtr interface{}) error
// ReadForm binds the formObject with the form data
// it supports any kind of struct.
ReadForm(formObject interface{}) error
ReadForm(formObjectPtr interface{}) error

// +------------------------------------------------------------+
// | Body (raw) Writers |
Expand Down Expand Up @@ -2008,7 +2007,7 @@ func (ctx *context) SetMaxRequestBodySize(limitOverBytes int64) {

// UnmarshalBody reads the request's body and binds it to a value or pointer of any type
// Examples of usage: context.ReadJSON, context.ReadXML.
func (ctx *context) UnmarshalBody(v interface{}, unmarshaler Unmarshaler) error {
func (ctx *context) UnmarshalBody(outPtr interface{}, unmarshaler Unmarshaler) error {
if ctx.request.Body == nil {
return errors.New("unmarshal: empty body")
}
Expand All @@ -2028,18 +2027,19 @@ func (ctx *context) UnmarshalBody(v interface{}, unmarshaler Unmarshaler) error
// in this case the v should be a pointer also,
// but this is up to the user's custom Decode implementation*
//
// See 'BodyDecoder' for more
if decoder, isDecoder := v.(BodyDecoder); isDecoder {
// See 'BodyDecoder' for more.
if decoder, isDecoder := outPtr.(BodyDecoder); isDecoder {
return decoder.Decode(rawData)
}

// check if v is already a pointer, if yes then pass as it's
if reflect.TypeOf(v).Kind() == reflect.Ptr {
return unmarshaler.Unmarshal(rawData, v)
}
// finally, if the v doesn't contains a self-body decoder and it's not a pointer
// use the custom unmarshaler to bind the body
return unmarshaler.Unmarshal(rawData, &v)
// // check if v is already a pointer, if yes then pass as it's
// if reflect.TypeOf(v).Kind() == reflect.Ptr {
// return unmarshaler.Unmarshal(rawData, v)
// } <- no need for that, ReadJSON is documented enough to receive a pointer,
// we don't need to reduce the performance here by using the reflect.TypeOf method.

// f the v doesn't contains a self-body decoder use the custom unmarshaler to bind the body.
return unmarshaler.Unmarshal(rawData, outPtr)
}

func (ctx *context) shouldOptimize() bool {
Expand Down

0 comments on commit 4b9e414

Please sign in to comment.