Skip to content

Commit d1424a0

Browse files
Merge pull request #14 from chenmingyong0423/feature/hook
hook enhancement
2 parents c974dfa + c9babc1 commit d1424a0

File tree

12 files changed

+151
-129
lines changed

12 files changed

+151
-129
lines changed

callback/callback.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ func initializeCallbacks() *Callback {
4343
},
4444
},
4545
},
46-
afterInsert: make([]callbackHandler, 0),
47-
beforeUpdate: []callbackHandler{
46+
afterInsert: []callbackHandler{
4847
{
49-
name: "mongox:default_field",
48+
name: "mongox:model",
5049
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
51-
return field.Execute(ctx, opCtx, operation.OpTypeBeforeUpdate, opts...)
50+
return model.Execute(ctx, opCtx, operation.OpTypeAfterInsert, opts...)
5251
},
5352
},
5453
},
54+
beforeUpdate: make([]callbackHandler, 0),
5555
afterUpdate: make([]callbackHandler, 0),
5656
beforeDelete: make([]callbackHandler, 0),
5757
afterDelete: make([]callbackHandler, 0),
@@ -69,8 +69,15 @@ func initializeCallbacks() *Callback {
6969
},
7070
},
7171
},
72-
afterUpsert: make([]callbackHandler, 0),
73-
beforeFind: make([]callbackHandler, 0),
72+
afterUpsert: []callbackHandler{
73+
{
74+
name: "mongox:model",
75+
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
76+
return model.Execute(ctx, opCtx, operation.OpTypeBeforeUpsert, opts...)
77+
},
78+
},
79+
},
80+
beforeFind: make([]callbackHandler, 0),
7481
afterFind: []callbackHandler{
7582
{
7683
name: "mongox:model",

creator/creator.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ type iCreator[T any] interface {
3434

3535
type Creator[T any] struct {
3636
collection *mongo.Collection
37-
beforeHooks []beforeHookFn[T]
38-
afterHooks []afterHookFn
37+
beforeHooks []hookFn[T]
38+
afterHooks []hookFn[T]
3939
}
4040

4141
func NewCreator[T any](collection *mongo.Collection) *Creator[T] {
@@ -47,17 +47,17 @@ func NewCreator[T any](collection *mongo.Collection) *Creator[T] {
4747
// RegisterBeforeHooks is used to set the after hooks of the insert operation
4848
// If you register the hook for InsertOne, the opContext.Docs will be nil
4949
// If you register the hook for InsertMany, the opContext.Doc will be nil
50-
func (c *Creator[T]) RegisterBeforeHooks(hooks ...beforeHookFn[T]) *Creator[T] {
50+
func (c *Creator[T]) RegisterBeforeHooks(hooks ...hookFn[T]) *Creator[T] {
5151
c.beforeHooks = append(c.beforeHooks, hooks...)
5252
return c
5353
}
5454

55-
func (c *Creator[T]) RegisterAfterHooks(hooks ...afterHookFn) *Creator[T] {
55+
func (c *Creator[T]) RegisterAfterHooks(hooks ...hookFn[T]) *Creator[T] {
5656
c.afterHooks = append(c.afterHooks, hooks...)
5757
return c
5858
}
5959

60-
func (c *Creator[T]) preActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *BeforeOpContext[T], opType operation.OpType) error {
60+
func (c *Creator[T]) preActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *OpContext[T], opType operation.OpType) error {
6161
err := callback.GetCallback().Execute(ctx, globalOpContext, opType)
6262
if err != nil {
6363
return err
@@ -71,7 +71,7 @@ func (c *Creator[T]) preActionHandler(ctx context.Context, globalOpContext *oper
7171
return nil
7272
}
7373

74-
func (c *Creator[T]) postActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *AfterOpContext, opType operation.OpType) error {
74+
func (c *Creator[T]) postActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *OpContext[T], opType operation.OpType) error {
7575
err := callback.GetCallback().Execute(ctx, globalOpContext, opType)
7676
if err != nil {
7777
return err
@@ -87,7 +87,7 @@ func (c *Creator[T]) postActionHandler(ctx context.Context, globalOpContext *ope
8787

8888
func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
8989
opContext := operation.NewOpContext(c.collection, operation.WithDoc(doc))
90-
err := c.preActionHandler(ctx, opContext, NewBeforeOpContext(c.collection, WithDoc(doc)), operation.OpTypeBeforeInsert)
90+
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDoc(doc)), operation.OpTypeBeforeInsert)
9191
if err != nil {
9292
return nil, err
9393
}
@@ -97,7 +97,7 @@ func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.Ins
9797
return nil, err
9898
}
9999

100-
err = c.postActionHandler(ctx, opContext, NewAfterOpContext(c.collection), operation.OpTypeAfterInsert)
100+
err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDoc(doc)), operation.OpTypeAfterInsert)
101101
if err != nil {
102102
return nil, err
103103
}
@@ -107,7 +107,7 @@ func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.Ins
107107

108108
func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) {
109109
opContext := operation.NewOpContext(c.collection, operation.WithDoc(docs))
110-
err := c.preActionHandler(ctx, opContext, NewBeforeOpContext(c.collection, WithDocs(docs)), operation.OpTypeBeforeInsert)
110+
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs)), operation.OpTypeBeforeInsert)
111111
if err != nil {
112112
return nil, err
113113
}
@@ -117,7 +117,7 @@ func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options
117117
return nil, err
118118
}
119119

120-
err = c.postActionHandler(ctx, opContext, NewAfterOpContext(c.collection), operation.OpTypeAfterInsert)
120+
err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs)), operation.OpTypeAfterInsert)
121121
if err != nil {
122122
return nil, err
123123
}

creator/creator_e2e_test.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ type User struct {
4646
UpdatedAt time.Time `bson:"updated_at"`
4747
}
4848

49+
func (u *User) DefaultId() {
50+
if u.ID.IsZero() {
51+
u.ID = primitive.NewObjectID()
52+
}
53+
}
54+
4955
func (u *User) DefaultCreatedAt() {
5056
if u.CreatedAt.IsZero() {
5157
u.CreatedAt = time.Now().Local()
@@ -87,8 +93,8 @@ func TestCreator_e2e_One(t *testing.T) {
8793
ctx context.Context
8894
doc *User
8995
globalHook []globalHook
90-
beforeHook []beforeHookFn[User]
91-
afterHook []afterHookFn
96+
beforeHook []hookFn[User]
97+
afterHook []hookFn[User]
9298

9399
wantError assert.ErrorAssertionFunc
94100
}{
@@ -227,8 +233,8 @@ func TestCreator_e2e_One(t *testing.T) {
227233
options.InsertOne().SetComment("test"),
228234
},
229235
doc: nil,
230-
beforeHook: []beforeHookFn[User]{
231-
func(ctx context.Context, opContext *BeforeOpContext[User], opts ...any) error {
236+
beforeHook: []hookFn[User]{
237+
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
232238
return errors.New("before hook error")
233239
},
234240
},
@@ -252,8 +258,8 @@ func TestCreator_e2e_One(t *testing.T) {
252258
Name: "Mingyong Chen",
253259
Age: 18,
254260
},
255-
afterHook: []afterHookFn{
256-
func(ctx context.Context, opContext *AfterOpContext, opts ...any) error {
261+
afterHook: []hookFn[User]{
262+
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
257263
return errors.New("after hook error")
258264
},
259265
},
@@ -277,16 +283,16 @@ func TestCreator_e2e_One(t *testing.T) {
277283
Name: "Mingyong Chen",
278284
Age: 18,
279285
},
280-
beforeHook: []beforeHookFn[User]{
281-
func(ctx context.Context, opContext *BeforeOpContext[User], opts ...any) error {
286+
beforeHook: []hookFn[User]{
287+
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
282288
if opContext.Doc == nil {
283289
return errors.New("before hook error")
284290
}
285291
return nil
286292
},
287293
},
288-
afterHook: []afterHookFn{
289-
func(ctx context.Context, opContext *AfterOpContext, opts ...any) error {
294+
afterHook: []hookFn[User]{
295+
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
290296
if opContext == nil {
291297
return errors.New("after hook error")
292298
}
@@ -340,8 +346,8 @@ func TestCreator_e2e_Many(t *testing.T) {
340346
docs []*User
341347
opts []*options.InsertManyOptions
342348
globalHook []globalHook
343-
beforeHook []beforeHookFn[User]
344-
afterHook []afterHookFn
349+
beforeHook []hookFn[User]
350+
afterHook []hookFn[User]
345351

346352
wantIdsLength int
347353
wantError assert.ErrorAssertionFunc
@@ -495,8 +501,8 @@ func TestCreator_e2e_Many(t *testing.T) {
495501
options.InsertMany().SetComment("test"),
496502
},
497503
docs: nil,
498-
beforeHook: []beforeHookFn[User]{
499-
func(ctx context.Context, opContext *BeforeOpContext[User], opts ...any) error {
504+
beforeHook: []hookFn[User]{
505+
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
500506
return errors.New("before hook error")
501507
},
502508
},
@@ -526,8 +532,8 @@ func TestCreator_e2e_Many(t *testing.T) {
526532
Age: 19,
527533
},
528534
},
529-
afterHook: []afterHookFn{
530-
func(ctx context.Context, opContext *AfterOpContext, opts ...any) error {
535+
afterHook: []hookFn[User]{
536+
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
531537
return errors.New("after hook error")
532538
},
533539
},
@@ -557,16 +563,16 @@ func TestCreator_e2e_Many(t *testing.T) {
557563
Age: 19,
558564
},
559565
},
560-
beforeHook: []beforeHookFn[User]{
561-
func(ctx context.Context, opContext *BeforeOpContext[User], opts ...any) error {
566+
beforeHook: []hookFn[User]{
567+
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
562568
if len(opContext.Docs) != 2 {
563569
return errors.New("before hook error")
564570
}
565571
return nil
566572
},
567573
},
568-
afterHook: []afterHookFn{
569-
func(ctx context.Context, opContext *AfterOpContext, opts ...any) error {
574+
afterHook: []hookFn[User]{
575+
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
570576
if opContext == nil {
571577
return errors.New("after hook error")
572578
}

creator/opt_after_op_context_gen.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

creator/opt_before_op_context_gen.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

creator/opt_op_context_gen.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Generated by optioner -type OpContext; DO NOT EDIT
2+
// If you have any questions, please create issues and submit contributions at:
3+
// https://github.com/chenmingyong0423/go-optioner
4+
5+
package creator
6+
7+
import (
8+
"go.mongodb.org/mongo-driver/mongo"
9+
)
10+
11+
type OpContextOption[T any] func(*OpContext[T])
12+
13+
func NewOpContext[T any](col *mongo.Collection, opts ...OpContextOption[T]) *OpContext[T] {
14+
opContext := &OpContext[T]{
15+
Col: col,
16+
}
17+
18+
for _, opt := range opts {
19+
opt(opContext)
20+
}
21+
22+
return opContext
23+
}
24+
25+
func WithDoc[T any](doc *T) OpContextOption[T] {
26+
return func(opContext *OpContext[T]) {
27+
opContext.Doc = doc
28+
}
29+
}
30+
31+
func WithDocs[T any](docs []*T) OpContextOption[T] {
32+
return func(opContext *OpContext[T]) {
33+
opContext.Docs = docs
34+
}
35+
}

creator/types.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@ import (
2020
"go.mongodb.org/mongo-driver/mongo"
2121
)
2222

23-
//go:generate optioner -type BeforeOpContext
24-
type BeforeOpContext[T any] struct {
23+
//go:generate optioner -type OpContext
24+
type OpContext[T any] struct {
2525
Col *mongo.Collection `opt:"-"`
2626
Doc *T
2727
Docs []*T
2828
}
2929

30-
//go:generate optioner -type AfterOpContext
31-
type AfterOpContext struct {
32-
Col *mongo.Collection `opt:"-"`
33-
}
34-
3530
type (
36-
beforeHookFn[T any] func(ctx context.Context, opContext *BeforeOpContext[T], opts ...any) error
37-
afterHookFn func(ctx context.Context, opContext *AfterOpContext, opts ...any) error
31+
hookFn[T any] func(ctx context.Context, opContext *OpContext[T], opts ...any) error
3832
)

hook/field/strategy.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,23 @@ var strategies = map[operation.OpType]func(doc any) error{
2626
}
2727

2828
func BeforeInsert(doc any) error {
29-
if tsh, ok := doc.(hook.TimeStamperHook); ok {
29+
if tsh, ok := doc.(hook.DefaultModelHook); ok {
30+
tsh.DefaultId()
3031
tsh.DefaultCreatedAt()
3132
}
3233
return nil
3334
}
3435

3536
func BeforeUpdate(doc any) error {
36-
if tsh, ok := doc.(hook.TimeStamperHook); ok {
37+
if tsh, ok := doc.(hook.DefaultModelHook); ok {
3738
tsh.DefaultUpdatedAt()
3839
}
3940
return nil
4041
}
4142

4243
func BeforeUpsert(doc any) error {
43-
if tsh, ok := doc.(hook.TimeStamperHook); ok {
44+
if tsh, ok := doc.(hook.DefaultModelHook); ok {
45+
tsh.DefaultId()
4446
tsh.DefaultCreatedAt()
4547
tsh.DefaultUpdatedAt()
4648
}

0 commit comments

Comments
 (0)