mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
remove unnecessary reflection usage on context#UnmarshalBody
Former-commit-id: a7dc78027c5647b3b21098808f360a536d8b9438
This commit is contained in:
parent
c0e0886cb9
commit
86bb29ed14
|
@ -15,7 +15,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -53,16 +52,16 @@ type (
|
||||||
Decode(data []byte) error
|
Decode(data []byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshaler is the interface implemented by types that can unmarshal any raw data
|
// 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.
|
// TIP INFO: Any pointer to a value which implements the BodyDecoder can be override the unmarshaler.
|
||||||
Unmarshaler interface {
|
Unmarshaler interface {
|
||||||
Unmarshal(data []byte, v interface{}) error
|
Unmarshal(data []byte, outPtr interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalerFunc a shortcut for the Unmarshaler interface
|
// UnmarshalerFunc a shortcut for the Unmarshaler interface
|
||||||
//
|
//
|
||||||
// See 'Unmarshaler' and 'BodyDecoder' for more.
|
// 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.
|
// 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.
|
// should be called before reading the request body from the client.
|
||||||
SetMaxRequestBodySize(limitOverBytes int64)
|
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.
|
// Examples of usage: context.ReadJSON, context.ReadXML.
|
||||||
UnmarshalBody(v interface{}, unmarshaler Unmarshaler) error
|
UnmarshalBody(outPtr interface{}, unmarshaler Unmarshaler) error
|
||||||
// ReadJSON reads JSON from request's body and binds it to a value of any json-valid type.
|
// ReadJSON reads JSON from request's body and binds it to a pointer of a value of any json-valid type.
|
||||||
ReadJSON(jsonObject interface{}) error
|
ReadJSON(jsonObjectPtr interface{}) error
|
||||||
// ReadXML reads XML from request's body and binds it to a value of any xml-valid type.
|
// ReadXML reads XML from request's body and binds it to a pointer of a value of any xml-valid type.
|
||||||
ReadXML(xmlObject interface{}) error
|
ReadXML(xmlObjectPtr interface{}) error
|
||||||
// ReadForm binds the formObject with the form data
|
// ReadForm binds the formObject with the form data
|
||||||
// it supports any kind of struct.
|
// it supports any kind of struct.
|
||||||
ReadForm(formObject interface{}) error
|
ReadForm(formObjectPtr interface{}) error
|
||||||
|
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
// | Body (raw) Writers |
|
// | 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
|
// UnmarshalBody reads the request's body and binds it to a value or pointer of any type
|
||||||
// Examples of usage: context.ReadJSON, context.ReadXML.
|
// 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 {
|
if ctx.request.Body == nil {
|
||||||
return errors.New("unmarshal: empty body")
|
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,
|
// in this case the v should be a pointer also,
|
||||||
// but this is up to the user's custom Decode implementation*
|
// but this is up to the user's custom Decode implementation*
|
||||||
//
|
//
|
||||||
// See 'BodyDecoder' for more
|
// See 'BodyDecoder' for more.
|
||||||
if decoder, isDecoder := v.(BodyDecoder); isDecoder {
|
if decoder, isDecoder := outPtr.(BodyDecoder); isDecoder {
|
||||||
return decoder.Decode(rawData)
|
return decoder.Decode(rawData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if v is already a pointer, if yes then pass as it's
|
// // check if v is already a pointer, if yes then pass as it's
|
||||||
if reflect.TypeOf(v).Kind() == reflect.Ptr {
|
// if reflect.TypeOf(v).Kind() == reflect.Ptr {
|
||||||
return unmarshaler.Unmarshal(rawData, v)
|
// return unmarshaler.Unmarshal(rawData, v)
|
||||||
}
|
// } <- no need for that, ReadJSON is documented enough to receive a pointer,
|
||||||
// finally, if the v doesn't contains a self-body decoder and it's not a pointer
|
// we don't need to reduce the performance here by using the reflect.TypeOf method.
|
||||||
// use the custom unmarshaler to bind the body
|
|
||||||
return unmarshaler.Unmarshal(rawData, &v)
|
// 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 {
|
func (ctx *context) shouldOptimize() bool {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user