-
Notifications
You must be signed in to change notification settings - Fork 42
/
binder.go
323 lines (255 loc) · 6.28 KB
/
binder.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package bob
import (
"database/sql/driver"
"errors"
"fmt"
"reflect"
"time"
"github.com/stephenafamo/bob/internal/mappings"
)
//nolint:gochecknoglobals
var (
ErrBadArgType = errors.New("bind type of multiple named args must be a struct, pointer to struct or map with ~string keys")
ErrTooManyNamedArgs = errors.New("too many named args for single arg binder")
driverValuerIntf = reflect.TypeFor[driver.Valuer]()
timeType = reflect.TypeFor[time.Time]()
)
type MissingArgError struct{ Name string }
func (e MissingArgError) Error() string {
return fmt.Sprintf("missing arg %s", e.Name)
}
type binder[T any] interface {
// list returns the names of the args that the binder expects
list() []string
// Return the args to be run in the query
// this should also include any non-named args in the original query
toArgs(T) []any
}
func bindArgs[Arg any](args []any, named Arg) ([]any, error) {
binder, err := makeBinder[Arg](args)
if err != nil {
return nil, err
}
return binder.toArgs(named), nil
}
func makeBinder[Arg any](args []any) (binder[Arg], error) {
namedArgs := countNamedArgs(args)
switch namedArgs {
case 0: // no named args
return emptyBinder[Arg](args), nil
case 1: // only one named arg
return makeSingleArgBinder[Arg](args)
default:
return makeMultiArgBinder[Arg](args)
}
}
func canUseAsSingleValue(typ reflect.Type) bool {
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
switch typ.Kind() {
case reflect.Bool, reflect.String,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return true
case reflect.Slice:
return typ.Elem().Kind() == reflect.Uint8
}
if typ == timeType {
return true
}
if typ.Implements(driverValuerIntf) {
return true
}
return false
}
func makeSingleArgBinder[Arg any](args []any) (binder[Arg], error) {
typ := reflect.TypeFor[Arg]()
if !canUseAsSingleValue(typ) {
return makeMultiArgBinder[Arg](args)
}
givenArg := make([]any, len(args))
copy(givenArg, args)
b := singleBinder[Arg]{givenArg: givenArg}
for pos, arg := range args {
if name, ok := arg.(namedArg); ok {
b.argIndexs = append(b.argIndexs, pos)
b.name = string(name)
}
}
return b, nil
}
func makeMultiArgBinder[Arg any](args []any) (binder[Arg], error) {
typ := reflect.TypeFor[Arg]()
switch typ.Kind() {
case reflect.Map:
if typ.Key().Kind() != reflect.String {
return nil, ErrBadArgType
}
return makeMapBinder[Arg](args), nil
case reflect.Struct:
return makeStructBinder[Arg](args)
case reflect.Ptr:
if typ.Elem().Kind() == reflect.Struct {
return makeStructBinder[Arg](args)
}
}
return nil, ErrBadArgType
}
type emptyBinder[Arg any] []any
func (b emptyBinder[Arg]) list() []string {
return nil
}
func (b emptyBinder[Arg]) toArgs(arg Arg) []any {
return b
}
func makeStructBinder[Arg any](args []any) (binder[Arg], error) {
typ := reflect.TypeFor[Arg]()
isStruct := typ.Kind() == reflect.Struct
if typ.Kind() == reflect.Ptr {
isStruct = typ.Elem().Kind() == reflect.Struct
}
if !isStruct {
return structBinder[Arg]{}, errors.New("bind type must be a struct")
}
givenArg := make([]any, len(args))
argPositions := make([]string, len(args))
for pos, arg := range args {
if name, ok := arg.(namedArg); ok {
argPositions[pos] = string(name)
continue
}
givenArg[pos] = arg
}
fieldNames := mappings.GetMappings(typ).All
fieldPositions := make([]int, len(argPositions))
// check if all positions have matching fields
ArgLoop:
for argIndex, name := range argPositions {
if name == "" {
continue
}
for fieldIndex, field := range fieldNames {
if field == name {
fieldPositions[argIndex] = fieldIndex
continue ArgLoop
}
}
return structBinder[Arg]{}, MissingArgError{Name: name}
}
return structBinder[Arg]{
args: argPositions,
fields: fieldPositions,
givenArg: givenArg,
}, nil
}
type structBinder[Arg any] struct {
args []string
fields []int
givenArg []any
}
func (b structBinder[Arg]) list() []string {
names := make([]string, len(b.args))
for _, name := range b.args {
if name == "" {
continue
}
names = append(names, name)
}
return names
}
func (b structBinder[Arg]) toArgs(arg Arg) []any {
isNil := false
val := reflect.ValueOf(arg)
if val.Kind() == reflect.Pointer {
isNil = val.IsNil()
val = val.Elem()
}
values := make([]any, len(b.args))
for index, argName := range b.args {
if argName == "" {
values[index] = b.givenArg[index]
continue
}
if isNil {
continue
}
values[index] = val.Field(b.fields[index]).Interface()
}
return values
}
func makeMapBinder[Arg any](args []any) binder[Arg] {
givenArg := make([]any, len(args))
argPositions := make([]string, len(args))
for pos, arg := range args {
if name, ok := arg.(namedArg); ok {
argPositions[pos] = string(name)
continue
}
givenArg[pos] = arg
}
return mapBinder[Arg]{
args: argPositions,
givenArg: givenArg,
}
}
type mapBinder[Arg any] struct {
args []string
givenArg []any
}
func (b mapBinder[Arg]) list() []string {
names := make([]string, len(b.args))
for _, name := range b.args {
if name == "" {
continue
}
names = append(names, name)
}
return names
}
func (b mapBinder[Arg]) toArgs(args Arg) []any {
values := make([]any, len(b.args))
for index, argName := range b.args {
if argName == "" {
values[index] = b.givenArg[index]
continue
}
val := reflect.ValueOf(args).MapIndex(reflect.ValueOf(argName))
if !val.IsValid() {
continue
}
values[index] = val.Interface()
}
return values
}
type singleBinder[Arg any] struct {
givenArg []any
argIndexs []int
name string
}
func (b singleBinder[Arg]) list() []string {
return []string{b.name}
}
func (b singleBinder[Arg]) toArgs(arg Arg) []any {
values := make([]any, len(b.givenArg))
copy(values, b.givenArg)
for _, i := range b.argIndexs {
values[i] = arg
}
return values
}
func countNamedArgs(args []any) int {
names := map[string]struct{}{}
for _, arg := range args {
if name, ok := arg.(namedArg); ok {
names[string(name)] = struct{}{}
continue
}
if name, ok := arg.(named); ok && len(name.names) == 1 {
names[name.names[0]] = struct{}{}
continue
}
}
return len(names)
}