-
Notifications
You must be signed in to change notification settings - Fork 3
/
values.go
189 lines (156 loc) · 4.23 KB
/
values.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package scan
import (
"fmt"
"reflect"
)
// Value retrieves a value from [Values] with the specified name
// if [Values.IsRecording] returns true, it will ALWAYS return the zero value
// of that type
// When not recording, it will panic if the requested type does not match
// what was recorded
func Value[T any](v *Values, name string) T {
if v.recording {
var x T
v.record(name, reflect.TypeOf(x))
}
return v.get(name).(T)
}
// ReflectedValue returns the named value as an [reflect.Value]
// When not recording, it will panic if the requested type does not match
// what was recorded
func ReflectedValue(v *Values, name string, typ reflect.Type) reflect.Value {
if v.recording {
v.record(name, typ)
}
return v.getRef(name)
}
// ValueCallback passes a function that will be called for the column
// this is meant to be used when you need to control the exact value that the column
// is mapped into.
func ValueCallback(v *Values, name string, f func() reflect.Value) reflect.Value {
if v.recording {
v.recordCallback(name, f)
}
return v.getRef(name)
}
func newValues(r Rows, allowUnknown bool) (*Values, error) {
cols, err := r.Columns()
if err != nil {
return nil, err
}
// convert columns to a map
colMap := make(map[string]int, len(cols))
for k, v := range cols {
colMap[v] = k
}
return &Values{
columns: colMap,
types: make(map[string]reflect.Type, len(cols)),
pointerGetter: make(map[string]func() reflect.Value),
}, nil
}
// Values holds the values of a row
// use Value() to retrieve a value by column name
// Column names must be unique, so
// if multiple columns have the same name, only the last one remains
type Values struct {
columns map[string]int
recording bool
types map[string]reflect.Type
pointerGetter map[string]func() reflect.Value
scanned []any
allowUnknown bool
}
// IsRecording returns wether the values are currently in recording mode
// When recording, calls to Get() will record the expected type
func (v *Values) IsRecording() bool {
return v.recording
}
func (v *Values) startRecording() {
v.recording = true
}
func (v *Values) findMissingColumns() []string {
cols := make([]string, len(v.columns))
for name, index := range v.columns {
if _, ok := v.types[name]; !ok {
cols[index] = name // to preserve order
}
}
// Remvoe empty strings
filtered := make([]string, 0)
for _, v := range cols {
if v != "" {
filtered = append(filtered, v)
}
}
return filtered
}
func (v *Values) stopRecording() error {
if !v.allowUnknown && len(v.types) != len(v.columns) {
missing := v.findMissingColumns()
err := fmt.Errorf("No destination for columns %v", missing)
return createError(err, append([]string{"no destination"}, missing...)...)
}
v.recording = false
return nil
}
// To get a copy of the columns to pass to mapper generators
// since modifing the map can have unintended side effects.
// Ideally, a generator should only call this once
func (v *Values) columnsCopy() map[string]int {
m := make(map[string]int, len(v.columns))
for k, v := range v.columns {
m[k] = v
}
return m
}
func (v *Values) getRef(name string) reflect.Value {
index, ok := v.columns[name]
if !ok || v.recording {
if p, ok := v.pointerGetter[name]; ok {
return p()
}
x := reflect.New(v.types[name]).Elem()
return x
}
return reflect.Indirect(
reflect.ValueOf(v.scanned[index]),
)
}
func (v *Values) get(name string) any {
return v.getRef(name).Interface()
}
func (v *Values) record(name string, t reflect.Type) {
v.types[name] = t
}
func (v *Values) recordCallback(name string, f func() reflect.Value) {
v.types[name] = f().Type()
v.pointerGetter[name] = f
}
func (v *Values) scanRow(r Row) error {
targets := make([]any, len(v.columns))
for name, i := range v.columns {
if p, ok := v.pointerGetter[name]; ok {
targets[i] = p().Interface()
continue
}
t := v.types[name]
if t == nil {
var fallback interface{}
targets[i] = &fallback
continue
}
// pointers[i] = reflect.New(t).Interface()
if t.Kind() == reflect.Pointer {
targets[i] = reflect.New(t).Elem().Interface()
} else {
targets[i] = reflect.New(t).Interface()
}
}
err := r.Scan(targets...)
if err != nil {
return err
}
v.scanned = targets
return nil
}