Skip to content

Commit 206e1d3

Browse files
Feat: Refactor model hook and integrate ModelHook across operations. #54
feat: - hook: adjust the implementation of the model hook to handle the default parameters and check if the corresponding interface is implemented based on either the default parameters or the `modelHook` parameter. - operation: add the ModelHook field to the OpContext struct. - finder: - add the ModelHook method to set the modelHook value. - add the ModelHook field to the OpContext struct. - when creating an instance of the operation.OpContext struct and the OpContext struct, pass the ModelHook object. - creator: - add the ModelHook method to set the modelHook value. - add the ModelHook field to the OpContext struct. - when creating an instance of the operation.OpContext struct and the OpContext struct, pass the ModelHook object. - deleter: - add the ModelHook method to set the modelHook value. - add the ModelHook field to the OpContext struct. - when creating an instance of the operation.OpContext struct and the OpContext struct, pass the ModelHook object. - updater: - add the ModelHook method to set the modelHook value. - add the ModelHook field to the OpContext struct. - when creating an instance of the operation.OpContext struct and the OpContext struct, pass the ModelHook object.
1 parent b7e200a commit 206e1d3

17 files changed

+246
-46
lines changed

callback_test.go

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func TestPluginInit_EnableModelHook(t *testing.T) {
334334
testCases := []struct {
335335
name string
336336
ctx context.Context
337-
ocOption func(tm *testModelHookStruct) operation.OpContextOption
337+
ocOption func(tm *testModelHookStruct) []operation.OpContextOption
338338
opType operation.OpType
339339

340340
wantErr error
@@ -343,61 +343,141 @@ func TestPluginInit_EnableModelHook(t *testing.T) {
343343
{
344344
name: "beforeInsert",
345345
ctx: context.Background(),
346-
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
347-
return operation.WithDoc(tm)
346+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
347+
return []operation.OpContextOption{
348+
operation.WithDoc(tm),
349+
}
348350
},
349351
opType: operation.OpTypeBeforeInsert,
350352
wantErr: nil,
351353
want: 1,
352354
},
355+
{
356+
name: "beforeInsert with model hook",
357+
ctx: context.Background(),
358+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
359+
*tm = 1
360+
return []operation.OpContextOption{
361+
operation.WithDoc(new(testModelHookStruct)),
362+
operation.WithModelHook(tm),
363+
}
364+
},
365+
opType: operation.OpTypeBeforeInsert,
366+
wantErr: nil,
367+
want: 2,
368+
},
353369
{
354370
name: "afterInsert",
355371
ctx: context.Background(),
356-
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
357-
return operation.WithDoc(tm)
372+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
373+
return []operation.OpContextOption{
374+
operation.WithDoc(tm),
375+
}
358376
},
359377
opType: operation.OpTypeAfterInsert,
360378
wantErr: nil,
361379
want: 1,
362380
},
381+
{
382+
name: "afterInsert with model hook",
383+
ctx: context.Background(),
384+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
385+
*tm = 1
386+
return []operation.OpContextOption{
387+
operation.WithDoc(new(testModelHookStruct)),
388+
operation.WithModelHook(tm),
389+
}
390+
},
391+
opType: operation.OpTypeAfterInsert,
392+
wantErr: nil,
393+
want: 2,
394+
},
363395
{
364396
name: "beforeUpsert",
365397
ctx: context.Background(),
366-
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
367-
return operation.WithReplacement(tm)
398+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
399+
return []operation.OpContextOption{
400+
operation.WithUpdates(tm),
401+
}
368402
},
369403
opType: operation.OpTypeBeforeUpsert,
370404
wantErr: nil,
371405
want: 1,
372406
},
407+
{
408+
name: "beforeUpsert with model hook",
409+
ctx: context.Background(),
410+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
411+
*tm = 1
412+
return []operation.OpContextOption{
413+
operation.WithUpdates(new(testModelHookStruct)),
414+
operation.WithModelHook(tm),
415+
}
416+
},
417+
opType: operation.OpTypeBeforeUpsert,
418+
wantErr: nil,
419+
want: 2,
420+
},
373421
{
374422
name: "afterUpsert",
375423
ctx: context.Background(),
376-
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
377-
return operation.WithReplacement(tm)
424+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
425+
return []operation.OpContextOption{
426+
operation.WithUpdates(tm),
427+
}
378428
},
379429
opType: operation.OpTypeAfterUpsert,
380430
wantErr: nil,
381431
want: 1,
382432
},
433+
{
434+
name: "afterUpsert with model hook",
435+
ctx: context.Background(),
436+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
437+
*tm = 1
438+
return []operation.OpContextOption{
439+
operation.WithUpdates(new(testModelHookStruct)),
440+
operation.WithModelHook(tm),
441+
}
442+
},
443+
opType: operation.OpTypeAfterUpsert,
444+
wantErr: nil,
445+
want: 2,
446+
},
383447
{
384448
name: "afterFind",
385449
ctx: context.Background(),
386-
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
387-
return operation.WithDoc(tm)
450+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
451+
return []operation.OpContextOption{
452+
operation.WithDoc(tm),
453+
}
388454
},
389455
opType: operation.OpTypeAfterFind,
390456
wantErr: nil,
391457
want: 1,
392458
},
459+
{
460+
name: "afterFind with model hook",
461+
ctx: context.Background(),
462+
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
463+
*tm = 1
464+
return []operation.OpContextOption{
465+
operation.WithDoc(new(testModelHookStruct)),
466+
operation.WithModelHook(tm),
467+
}
468+
},
469+
opType: operation.OpTypeAfterFind,
470+
wantErr: nil,
471+
want: 2,
472+
},
393473
}
394474

395475
for _, tc := range testCases {
396476
t.Run(tc.name, func(t *testing.T) {
397477
tm := new(testModelHookStruct)
398478
err := callback.GetCallback().Execute(
399479
tc.ctx,
400-
operation.NewOpContext(nil, tc.ocOption(tm)),
480+
operation.NewOpContext(nil, tc.ocOption(tm)...),
401481
tc.opType,
402482
)
403483
require.Nil(t, err)
@@ -407,7 +487,7 @@ func TestPluginInit_EnableModelHook(t *testing.T) {
407487
InitPlugin(cfg)
408488
err = callback.GetCallback().Execute(
409489
tc.ctx,
410-
operation.NewOpContext(nil, tc.ocOption(tm)),
490+
operation.NewOpContext(nil, tc.ocOption(tm)...),
411491
tc.opType,
412492
)
413493
require.Equal(t, tc.wantErr, err)

creator/creator.go

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

3535
type Creator[T any] struct {
3636
collection *mongo.Collection
37+
modelHook any
3738
beforeHooks []hookFn[T]
3839
afterHooks []hookFn[T]
3940
}
@@ -44,6 +45,11 @@ func NewCreator[T any](collection *mongo.Collection) *Creator[T] {
4445
}
4546
}
4647

48+
func (c *Creator[T]) ModelHook(modelHook any) *Creator[T] {
49+
c.modelHook = modelHook
50+
return c
51+
}
52+
4753
// RegisterBeforeHooks is used to set the after hooks of the insert operation
4854
// If you register the hook for InsertOne, the opContext.Docs will be nil
4955
// If you register the hook for InsertMany, the opContext.Doc will be nil
@@ -86,8 +92,8 @@ func (c *Creator[T]) postActionHandler(ctx context.Context, globalOpContext *ope
8692
}
8793

8894
func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
89-
opContext := operation.NewOpContext(c.collection, operation.WithDoc(doc), operation.WithMongoOptions(opts))
90-
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDoc(doc), WithMongoOptions[T](opts)), operation.OpTypeBeforeInsert)
95+
opContext := operation.NewOpContext(c.collection, operation.WithDoc(doc), operation.WithMongoOptions(opts), operation.WithModelHook(c.modelHook))
96+
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDoc(doc), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeBeforeInsert)
9197
if err != nil {
9298
return nil, err
9399
}
@@ -97,7 +103,7 @@ func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.Ins
97103
return nil, err
98104
}
99105

100-
err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDoc(doc), WithMongoOptions[T](opts)), operation.OpTypeAfterInsert)
106+
err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDoc(doc), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeAfterInsert)
101107
if err != nil {
102108
return nil, err
103109
}
@@ -106,8 +112,8 @@ func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.Ins
106112
}
107113

108114
func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) {
109-
opContext := operation.NewOpContext(c.collection, operation.WithDoc(docs), operation.WithMongoOptions(opts))
110-
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts)), operation.OpTypeBeforeInsert)
115+
opContext := operation.NewOpContext(c.collection, operation.WithDoc(docs), operation.WithMongoOptions(opts), operation.WithModelHook(c.modelHook))
116+
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeBeforeInsert)
111117
if err != nil {
112118
return nil, err
113119
}
@@ -117,7 +123,7 @@ func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options
117123
return nil, err
118124
}
119125

120-
err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts)), operation.OpTypeAfterInsert)
126+
err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeAfterInsert)
121127
if err != nil {
122128
return nil, err
123129
}

creator/opt_op_context_gen.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ func WithMongoOptions[T any](mongoOptions any) OpContextOption[T] {
3737
opContext.MongoOptions = mongoOptions
3838
}
3939
}
40+
41+
func WithModelHook[T any](modelHook any) OpContextOption[T] {
42+
return func(opContext *OpContext[T]) {
43+
opContext.ModelHook = modelHook
44+
}
45+
}

creator/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type OpContext[T any] struct {
2626
Doc *T
2727
Docs []*T
2828
MongoOptions any
29+
ModelHook any
2930
}
3031

3132
type (

deleter/deleter.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func NewDeleter[T any](collection *mongo.Collection) *Deleter[T] {
3737
type Deleter[T any] struct {
3838
collection *mongo.Collection
3939
filter any
40+
modelHook any
4041
beforeHooks []beforeHookFn
4142
afterHooks []afterHookFn
4243
}
@@ -85,9 +86,14 @@ func (d *Deleter[T]) Filter(filter any) *Deleter[T] {
8586
return d
8687
}
8788

89+
func (d *Deleter[T]) ModelHook(modelHook any) *Deleter[T] {
90+
d.modelHook = modelHook
91+
return d
92+
}
93+
8894
func (d *Deleter[T]) DeleteOne(ctx context.Context, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
89-
globalPoContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter), operation.WithMongoOptions(opts))
90-
err := d.preActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts)), operation.OpTypeBeforeDelete)
95+
globalPoContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter), operation.WithMongoOptions(opts), operation.WithModelHook(d.modelHook))
96+
err := d.preActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts), WithModelHook(d.modelHook)), operation.OpTypeBeforeDelete)
9197
if err != nil {
9298
return nil, err
9399
}
@@ -97,7 +103,7 @@ func (d *Deleter[T]) DeleteOne(ctx context.Context, opts ...*options.DeleteOptio
97103
return nil, err
98104
}
99105

100-
err = d.postActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts)), operation.OpTypeAfterDelete)
106+
err = d.postActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts), WithModelHook(d.modelHook)), operation.OpTypeAfterDelete)
101107
if err != nil {
102108
return nil, err
103109
}
@@ -106,8 +112,8 @@ func (d *Deleter[T]) DeleteOne(ctx context.Context, opts ...*options.DeleteOptio
106112
}
107113

108114
func (d *Deleter[T]) DeleteMany(ctx context.Context, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
109-
globalPoContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter), operation.WithMongoOptions(opts))
110-
err := d.preActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts)), operation.OpTypeBeforeDelete)
115+
globalPoContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter), operation.WithMongoOptions(opts), operation.WithModelHook(d.modelHook))
116+
err := d.preActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts), WithModelHook(d.modelHook)), operation.OpTypeBeforeDelete)
111117
if err != nil {
112118
return nil, err
113119
}
@@ -117,7 +123,7 @@ func (d *Deleter[T]) DeleteMany(ctx context.Context, opts ...*options.DeleteOpti
117123
return nil, err
118124
}
119125

120-
err = d.postActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts)), operation.OpTypeAfterDelete)
126+
err = d.postActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts), WithModelHook(d.modelHook)), operation.OpTypeAfterDelete)
121127
if err != nil {
122128
return nil, err
123129
}

deleter/opt_op_context_gen.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ func WithMongoOptions(mongoOptions any) OpContextOption {
2626
opContext.MongoOptions = mongoOptions
2727
}
2828
}
29+
30+
func WithModelHook(modelHook any) OpContextOption {
31+
return func(opContext *OpContext) {
32+
opContext.ModelHook = modelHook
33+
}
34+
}

deleter/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type OpContext struct {
2525
Col *mongo.Collection `opt:"-"`
2626
Filter any `opt:"-"`
2727
MongoOptions any
28+
ModelHook any
2829
}
2930

3031
type (

0 commit comments

Comments
 (0)