1 Star 2 Fork 0

api-go/iris

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
unmarshaler.go 847 Bytes
一键复制 编辑 原始数据 按行查看 历史
kataras 提交于 2017-11-02 05:54 . Update to version 8.5.5 | Read HISTORY.md
package encoding
import (
"errors"
"io"
"io/ioutil"
"reflect"
)
// UnmarshalerFunc is the Unmarshaler compatible type.
//
// See 'unmarshalBody' for more.
type UnmarshalerFunc func(data []byte, v interface{}) error
// UnmarshalBody reads the request's body and binds it to a value or pointer of any type.
func UnmarshalBody(body io.Reader, v interface{}, unmarshaler UnmarshalerFunc) error {
if body == nil {
return errors.New("unmarshal: empty body")
}
rawData, err := ioutil.ReadAll(body)
if err != nil {
return err
}
// check if v is already a pointer, if yes then pass as it's
if reflect.TypeOf(v).Kind() == reflect.Ptr {
return unmarshaler(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(rawData, &v)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/netscript/iris.git
git@gitee.com:netscript/iris.git
netscript
iris
iris
v8.5.9

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385