1
0
mirror of https://github.com/beego/bee.git synced 2024-11-21 23:50:54 +00:00

when field list is empty, no need to reflect

This commit is contained in:
ZhengYang 2014-08-06 10:26:54 +08:00
parent b057b16a80
commit 6a7428b3aa

View File

@ -678,22 +678,29 @@ func GetAll{{modelName}}(query map[string]string, fields []string, sortby []stri
var l []{{modelName}} var l []{{modelName}}
qs = qs.OrderBy(sortFields...) qs = qs.OrderBy(sortFields...)
if _, err := qs.Limit(limit, offset).All(&l, fields...); err == nil { if _, err := qs.Limit(limit, offset).All(&l, fields...); err == nil {
// trim unused fields if len(fields) == 0 {
fieldMap := make(map[string]interface{}) for _, v := range l {
for _, v := range fields { ml = append(ml, v)
fieldMap[v] = true }
} } else {
for _, v := range l { // trim unused fields
s := reflect.Indirect(reflect.ValueOf(v)) fieldMap := make(map[string]interface{})
typeOfS := s.Type() for _, v := range fields {
for i := 0; i < s.NumField(); i++ { fieldMap[v] = true
f := s.Field(i) }
if _, ok := fieldMap[typeOfS.Field(i).Name]; ok { for _, v := range l {
fieldMap[typeOfS.Field(i).Name] = f.Interface() m := make(map[string]interface{})
s := reflect.Indirect(reflect.ValueOf(v))
typeOfS := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
if _, ok := fieldMap[typeOfS.Field(i).Name]; ok {
m[typeOfS.Field(i).Name] = f.Interface()
}
} }
ml = append(ml, m)
} }
} }
ml = append(ml, fieldMap)
return ml, nil return ml, nil
} }
return nil, err return nil, err