-
Notifications
You must be signed in to change notification settings - Fork 39
/
hook.go
73 lines (65 loc) · 1.44 KB
/
hook.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
package gosql
import (
"context"
"errors"
"log"
"reflect"
"strings"
)
type Hook struct {
db *DB
Errs []error
ctx context.Context
}
func NewHook(ctx context.Context, db *DB) *Hook {
return &Hook{
db: db,
ctx: ctx,
}
}
func (h *Hook) callMethod(methodName string, reflectValue reflect.Value) {
// Only get address from non-pointer
if reflectValue.CanAddr() && reflectValue.Kind() != reflect.Ptr {
reflectValue = reflectValue.Addr()
}
if methodValue := reflectValue.MethodByName(methodName); methodValue.IsValid() {
switch method := methodValue.Interface().(type) {
case func():
method()
case func() error:
h.Err(method())
case func(db *DB):
method(h.db)
case func(db *DB) error:
h.Err(method(h.db))
case func(ctx context.Context):
method(h.ctx)
case func(ctx context.Context) error:
h.Err(method(h.ctx))
case func(ctx context.Context, db *DB):
method(h.ctx, h.db)
case func(ctx context.Context, db *DB) error:
h.Err(method(h.ctx, h.db))
default:
log.Panicf("unsupported function %v", methodName)
}
}
}
// Err add error
func (h *Hook) Err(err error) {
if err != nil {
h.Errs = append(h.Errs, err)
}
}
// HasError has errors
func (h *Hook) HasError() bool {
return len(h.Errs) > 0
}
// Error format happened errors
func (h *Hook) Error() error {
var errs = make([]string, 0)
for _, e := range h.Errs {
errs = append(errs, e.Error())
}
return errors.New(strings.Join(errs, "; "))
}