-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.go
51 lines (42 loc) · 1.11 KB
/
hooks.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
package sqlite
import (
"context"
"fmt"
)
type Hooks[T any] struct {
BeforePut []Hook[T]
AfterPut []Hook[T]
}
type Hook[T any] func(ctx context.Context, model *T) error
type HookBeforePut[T any] interface {
BeforePut(ctx context.Context, tx *Tx[T]) error
}
func runBeforePut[T any](ctx context.Context, tx *Tx[T], hooks Hooks[T], model *T) error {
if hook, ok := any(model).(HookBeforePut[T]); ok {
if err := hook.BeforePut(ctx, tx); err != nil {
return fmt.Errorf("before put hook: %w", err)
}
}
for _, hook := range hooks.BeforePut {
if err := hook(ctx, model); err != nil {
return fmt.Errorf("global before put hook: %w", err)
}
}
return nil
}
type HookAfterPut interface {
AfterPut(ctx context.Context) error
}
func runAfterPut[T any](ctx context.Context, hooks Hooks[T], model *T) error {
if hook, ok := any(model).(HookAfterPut); ok {
if err := hook.AfterPut(ctx); err != nil {
return fmt.Errorf("after put hook: %w", err)
}
}
for _, hook := range hooks.AfterPut {
if err := hook(ctx, model); err != nil {
return fmt.Errorf("global after put hook: %w", err)
}
}
return nil
}