improve code

This commit is contained in:
Lunny Xiao 2021-06-29 10:04:19 +08:00
parent d3593cd8de
commit 31fef0a637
1 changed files with 23 additions and 1 deletions

View File

@ -419,12 +419,34 @@ func (session *Session) rows2Beans(rows *core.Rows, types []*sql.ColumnType, fie
return nil return nil
} }
func bean2Map(parser *tags.Parser, bean interface{}) (map[string]reflect.Value, error) {
table, err := parser.ParseWithCache(reflect.ValueOf(bean))
if err != nil {
return nil, err
}
v := reflect.ValueOf(bean)
v.FieldByIndex(index []int)
}
func getValues(bean interface{}, fields ...string) ([]reflect.Value, error) {
values := bean2Map(bean)
var res = make([]reflect.Value, 0, len(fields))
for _, field := range fields {
v, ok := values[strings.ToUpper(field)]
if !ok {
return nil, fmt.Errorf("cannot find column %s on bean %#v", field, bean)
}
res = append(res, v)
}
return res, nil
}
func (session *Session) row2Slice(rows *core.Rows, types []*sql.ColumnType, fields []string, bean interface{}, table *schemas.Table) ([]interface{}, error) { func (session *Session) row2Slice(rows *core.Rows, types []*sql.ColumnType, fields []string, bean interface{}, table *schemas.Table) ([]interface{}, error) {
for _, closure := range session.beforeClosures { for _, closure := range session.beforeClosures {
closure(bean) closure(bean)
} }
values, err := getValues(bean, fields) values, err := getValues(bean, fields...)
if err != nil { if err != nil {
return nil, err return nil, err
} }