forked from chrisyxlee/pgxpoolmock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
row.go
67 lines (54 loc) · 1.39 KB
/
row.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
package pgxpoolmock
import (
"fmt"
"reflect"
)
// Row implements the pgx.Row interface and can be passed into pgxpoolmock.PgxPoolMock.QueryRow
// as the expected returned row.
//
// Usage: pgxMock.EXPECT().QueryRow(gomock.Any(), HasString("GetEntityByID")).Return(NewRow(1, "foo")).
type Row struct {
values []interface{}
err error
}
func NewRow(values ...any) *Row {
return &Row{
values: values,
err: nil,
}
}
func (r *Row) WithError(err error) *Row {
r.err = err
return r
}
func (r *Row) Scan(dest ...any) error {
if len(r.values) != len(dest) {
return fmt.Errorf(
"%w\ngot %d\n%+v\nwant %d\n%+v",
ErrScanExpectedToHaveSameNumberOfArgs,
len(dest),
dest,
len(r.values),
r.values,
)
}
for i := range dest {
value := r.values[i]
valueType := reflect.TypeOf(value).String()
valueRV := reflect.ValueOf(value)
if dest[i] == nil {
return fmt.Errorf("%w %d, want type %s", ErrUnexpectedNilVal, i, valueType)
}
dstRV := reflect.ValueOf(dest[i])
if dstRV.Kind() != reflect.Ptr {
return fmt.Errorf("%w: got %s, want %s", ErrToBeCalledWithPointers, reflect.TypeOf(dest[i]).String(), valueType)
}
innerDstRV := reflect.Indirect(dstRV)
dstType := innerDstRV.Type().String()
if dstType != valueType {
return fmt.Errorf("%w %d: got type %s, want type %s", ErrUnexpectedArg, i, dstType, valueType)
}
innerDstRV.Set(valueRV)
}
return r.err
}