示例#1
0
// Decode tranform request data to structure
func (rd *Decoder) Decode(obj interface{}, req *http.Request) error {
	m := make(map[string]interface{})
	//form
	if err := req.ParseForm(); err != nil {
		return err
	}
	if err := req.ParseMultipartForm(0); err != nil {
		return err
	}
	for key, customType := range rd.structType.GetSubTypes() {
		val, ok := req.PostForm[key]
		if ok {
			data, err := customType.FromString(val[0])
			if err != nil {
				return err
			}
			m[key] = data
			continue
		}
		fha, ok := req.MultipartForm.File[key]
		if !ok || len(fha) < 1 {
			return fmt.Errorf("can not find %v key of request form", key)
		}
		fh := fha[0]
		data, err := customType.FromMultipart(fh)
		if err != nil {
			return err
		}
		m[key] = data
		continue
	}
	return varutil.LoadStruct(obj, m, "schema", true)
}
示例#2
0
文件: main.go 项目: goatcms/goat-core
// StructScan scan current row
func (r *Rows) StructScan(dest interface{}) error {
	if err := varutil.LoadStruct(dest, r.data[r.index], "schema", true); err != nil {
		return err
	}
	r.index++
	return nil
}