remove unnecessary reflection usage on context#UnmarshalBody

Former-commit-id: a7dc78027c5647b3b21098808f360a536d8b9438
This commit is contained in:
Gerasimos Maropoulos 2018-02-23 02:15:38 +02:00
parent c0e0886cb9
commit 86bb29ed14

View File

@ -15,7 +15,6 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
@ -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.
@ -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 |
@ -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")
}
@ -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 {