v1 schema struct mapping

This commit is contained in:
Jay
2018-04-20 14:09:27 +08:00
parent a4488be12b
commit 04b2cb38b1
2 changed files with 71 additions and 10 deletions
+13 -3
View File
@@ -24,8 +24,12 @@ func ScanToStruct(rows *sql.Rows, ss interface{}) (err error) {
func ScanToStructAll(rows *sql.Rows, ss interface{}) error {
sliceVal := reflect.Indirect(reflect.ValueOf(ss))
if sliceVal.Kind() != reflect.Slice && sliceVal.Kind() != reflect.Map {
return errors.New("need a pointer to a slice or a map")
if sliceVal.Kind() != reflect.Slice {
return errors.New("need a pointer to a slice ")
}
vt := sliceVal.Type().Elem()
if vt.Kind() != reflect.Struct {
return errors.New("need struct slice")
}
data, err := ResultToMap(rows)
@@ -34,7 +38,13 @@ func ScanToStructAll(rows *sql.Rows, ss interface{}) error {
}
for i := range data {
tmp := reflect.New(vt)
iface := tmp.Interface()
err = MapToStruct(data[i], iface)
if err != nil {
return err
}
sliceVal.Set(reflect.Append(sliceVal, reflect.ValueOf(iface).Elem()))
}
return nil