-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.go
45 lines (37 loc) · 951 Bytes
/
scan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package col
import (
"reflect"
)
func (index Index) Pointers(dest interface{}) []interface{} {
v := reflect.ValueOf(dest)
if v.Kind() != reflect.Ptr {
panic("destination struct is not a pointer")
}
v = v.Elem()
ret := make([]interface{}, 0, len(index))
for _, fieldIndex := range index {
fieldValue := v.FieldByIndex(fieldIndex)
ret = append(ret, fieldValue.Addr().Interface())
}
return ret
}
type Row interface {
Scan(dest ...interface{}) error
}
// Usage :
// type Tweet struct {
// Username string `col:"tweets.user_name"`
// Text string `col:"tweets.text"`
// }
//
// scanner := col.NewIndex(Tweet{}, "tweets.user_name", "tweets.text")
// rows, _ := squirrel.Select("tweets.user_name", "tweets.text").From("tweets").Query()
//
// for rows.Next() {
// tweet := Tweet{}
//
// scanner.Scan(rows, &tweet)
// }
func (index Index) Scan(row Row, dest interface{}) error {
return row.Scan(index.Pointers(dest)...)
}