1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 10:11:29 +00:00

允许o.Raw(sql).QueryRows(&container) 传入的container包含结构的嵌套

This commit is contained in:
sunxinle 2017-05-12 18:11:42 +08:00
parent bceefc9075
commit 589f3755f0

View File

@ -493,19 +493,33 @@ func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) {
} }
} }
} else { } else {
for i := 0; i < ind.NumField(); i++ { // define recursive function
f := ind.Field(i) var recursiveSetField func(rv reflect.Value)
fe := ind.Type().Field(i) recursiveSetField = func(rv reflect.Value) {
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName)) for i := 0; i < rv.NumField(); i++ {
var col string f := rv.Field(i)
if col = tags["column"]; col == "" { fe := rv.Type().Field(i)
col = snakeString(fe.Name)
} // check if the field is a Struct
if v, ok := columnsMp[col]; ok { // recursive the Struct type
value := reflect.ValueOf(v).Elem().Interface() if fe.Type.Kind() == reflect.Struct {
o.setFieldValue(f, value) recursiveSetField(f)
}
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
var col string
if col = tags["column"]; col == "" {
col = snakeString(fe.Name)
}
if v, ok := columnsMp[col]; ok {
value := reflect.ValueOf(v).Elem().Interface()
o.setFieldValue(f, value)
}
} }
} }
// init call the recursive function
recursiveSetField(ind)
} }
if eTyps[0].Kind() == reflect.Ptr { if eTyps[0].Kind() == reflect.Ptr {