Skip to content

Commit

Permalink
Feat: Refactor model hook and integrate ModelHook across operations. #54
Browse files Browse the repository at this point in the history


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.
  • Loading branch information
chenmingyong0423 authored Sep 10, 2024
1 parent b7e200a commit 206e1d3
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 46 deletions.
106 changes: 93 additions & 13 deletions callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func TestPluginInit_EnableModelHook(t *testing.T) {
testCases := []struct {
name string
ctx context.Context
ocOption func(tm *testModelHookStruct) operation.OpContextOption
ocOption func(tm *testModelHookStruct) []operation.OpContextOption
opType operation.OpType

wantErr error
Expand All @@ -343,61 +343,141 @@ func TestPluginInit_EnableModelHook(t *testing.T) {
{
name: "beforeInsert",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
return operation.WithDoc(tm)
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
return []operation.OpContextOption{
operation.WithDoc(tm),
}
},
opType: operation.OpTypeBeforeInsert,
wantErr: nil,
want: 1,
},
{
name: "beforeInsert with model hook",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
*tm = 1
return []operation.OpContextOption{
operation.WithDoc(new(testModelHookStruct)),
operation.WithModelHook(tm),
}
},
opType: operation.OpTypeBeforeInsert,
wantErr: nil,
want: 2,
},
{
name: "afterInsert",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
return operation.WithDoc(tm)
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
return []operation.OpContextOption{
operation.WithDoc(tm),
}
},
opType: operation.OpTypeAfterInsert,
wantErr: nil,
want: 1,
},
{
name: "afterInsert with model hook",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
*tm = 1
return []operation.OpContextOption{
operation.WithDoc(new(testModelHookStruct)),
operation.WithModelHook(tm),
}
},
opType: operation.OpTypeAfterInsert,
wantErr: nil,
want: 2,
},
{
name: "beforeUpsert",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
return operation.WithReplacement(tm)
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
return []operation.OpContextOption{
operation.WithUpdates(tm),
}
},
opType: operation.OpTypeBeforeUpsert,
wantErr: nil,
want: 1,
},
{
name: "beforeUpsert with model hook",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
*tm = 1
return []operation.OpContextOption{
operation.WithUpdates(new(testModelHookStruct)),
operation.WithModelHook(tm),
}
},
opType: operation.OpTypeBeforeUpsert,
wantErr: nil,
want: 2,
},
{
name: "afterUpsert",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
return operation.WithReplacement(tm)
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
return []operation.OpContextOption{
operation.WithUpdates(tm),
}
},
opType: operation.OpTypeAfterUpsert,
wantErr: nil,
want: 1,
},
{
name: "afterUpsert with model hook",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
*tm = 1
return []operation.OpContextOption{
operation.WithUpdates(new(testModelHookStruct)),
operation.WithModelHook(tm),
}
},
opType: operation.OpTypeAfterUpsert,
wantErr: nil,
want: 2,
},
{
name: "afterFind",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) operation.OpContextOption {
return operation.WithDoc(tm)
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
return []operation.OpContextOption{
operation.WithDoc(tm),
}
},
opType: operation.OpTypeAfterFind,
wantErr: nil,
want: 1,
},
{
name: "afterFind with model hook",
ctx: context.Background(),
ocOption: func(tm *testModelHookStruct) []operation.OpContextOption {
*tm = 1
return []operation.OpContextOption{
operation.WithDoc(new(testModelHookStruct)),
operation.WithModelHook(tm),
}
},
opType: operation.OpTypeAfterFind,
wantErr: nil,
want: 2,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tm := new(testModelHookStruct)
err := callback.GetCallback().Execute(
tc.ctx,
operation.NewOpContext(nil, tc.ocOption(tm)),
operation.NewOpContext(nil, tc.ocOption(tm)...),
tc.opType,
)
require.Nil(t, err)
Expand All @@ -407,7 +487,7 @@ func TestPluginInit_EnableModelHook(t *testing.T) {
InitPlugin(cfg)
err = callback.GetCallback().Execute(
tc.ctx,
operation.NewOpContext(nil, tc.ocOption(tm)),
operation.NewOpContext(nil, tc.ocOption(tm)...),
tc.opType,
)
require.Equal(t, tc.wantErr, err)
Expand Down
18 changes: 12 additions & 6 deletions creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type iCreator[T any] interface {

type Creator[T any] struct {
collection *mongo.Collection
modelHook any
beforeHooks []hookFn[T]
afterHooks []hookFn[T]
}
Expand All @@ -44,6 +45,11 @@ func NewCreator[T any](collection *mongo.Collection) *Creator[T] {
}
}

func (c *Creator[T]) ModelHook(modelHook any) *Creator[T] {
c.modelHook = modelHook
return c
}

// RegisterBeforeHooks is used to set the after hooks of the insert operation
// If you register the hook for InsertOne, the opContext.Docs will be nil
// If you register the hook for InsertMany, the opContext.Doc will be nil
Expand Down Expand Up @@ -86,8 +92,8 @@ func (c *Creator[T]) postActionHandler(ctx context.Context, globalOpContext *ope
}

func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
opContext := operation.NewOpContext(c.collection, operation.WithDoc(doc), operation.WithMongoOptions(opts))
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDoc(doc), WithMongoOptions[T](opts)), operation.OpTypeBeforeInsert)
opContext := operation.NewOpContext(c.collection, operation.WithDoc(doc), operation.WithMongoOptions(opts), operation.WithModelHook(c.modelHook))
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDoc(doc), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeBeforeInsert)
if err != nil {
return nil, err
}
Expand All @@ -97,7 +103,7 @@ func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.Ins
return nil, err
}

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

func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) {
opContext := operation.NewOpContext(c.collection, operation.WithDoc(docs), operation.WithMongoOptions(opts))
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts)), operation.OpTypeBeforeInsert)
opContext := operation.NewOpContext(c.collection, operation.WithDoc(docs), operation.WithMongoOptions(opts), operation.WithModelHook(c.modelHook))
err := c.preActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeBeforeInsert)
if err != nil {
return nil, err
}
Expand All @@ -117,7 +123,7 @@ func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options
return nil, err
}

err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts)), operation.OpTypeAfterInsert)
err = c.postActionHandler(ctx, opContext, NewOpContext(c.collection, WithDocs(docs), WithMongoOptions[T](opts), WithModelHook[T](c.modelHook)), operation.OpTypeAfterInsert)
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions creator/opt_op_context_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ func WithMongoOptions[T any](mongoOptions any) OpContextOption[T] {
opContext.MongoOptions = mongoOptions
}
}

func WithModelHook[T any](modelHook any) OpContextOption[T] {
return func(opContext *OpContext[T]) {
opContext.ModelHook = modelHook
}
}
1 change: 1 addition & 0 deletions creator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type OpContext[T any] struct {
Doc *T
Docs []*T
MongoOptions any
ModelHook any
}

type (
Expand Down
18 changes: 12 additions & 6 deletions deleter/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func NewDeleter[T any](collection *mongo.Collection) *Deleter[T] {
type Deleter[T any] struct {
collection *mongo.Collection
filter any
modelHook any
beforeHooks []beforeHookFn
afterHooks []afterHookFn
}
Expand Down Expand Up @@ -85,9 +86,14 @@ func (d *Deleter[T]) Filter(filter any) *Deleter[T] {
return d
}

func (d *Deleter[T]) ModelHook(modelHook any) *Deleter[T] {
d.modelHook = modelHook
return d
}

func (d *Deleter[T]) DeleteOne(ctx context.Context, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
globalPoContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter), operation.WithMongoOptions(opts))
err := d.preActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts)), operation.OpTypeBeforeDelete)
globalPoContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter), operation.WithMongoOptions(opts), operation.WithModelHook(d.modelHook))
err := d.preActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts), WithModelHook(d.modelHook)), operation.OpTypeBeforeDelete)
if err != nil {
return nil, err
}
Expand All @@ -97,7 +103,7 @@ func (d *Deleter[T]) DeleteOne(ctx context.Context, opts ...*options.DeleteOptio
return nil, err
}

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

func (d *Deleter[T]) DeleteMany(ctx context.Context, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
globalPoContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter), operation.WithMongoOptions(opts))
err := d.preActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts)), operation.OpTypeBeforeDelete)
globalPoContext := operation.NewOpContext(d.collection, operation.WithFilter(d.filter), operation.WithMongoOptions(opts), operation.WithModelHook(d.modelHook))
err := d.preActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts), WithModelHook(d.modelHook)), operation.OpTypeBeforeDelete)
if err != nil {
return nil, err
}
Expand All @@ -117,7 +123,7 @@ func (d *Deleter[T]) DeleteMany(ctx context.Context, opts ...*options.DeleteOpti
return nil, err
}

err = d.postActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts)), operation.OpTypeAfterDelete)
err = d.postActionHandler(ctx, globalPoContext, NewOpContext(d.collection, d.filter, WithMongoOptions(opts), WithModelHook(d.modelHook)), operation.OpTypeAfterDelete)
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions deleter/opt_op_context_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ func WithMongoOptions(mongoOptions any) OpContextOption {
opContext.MongoOptions = mongoOptions
}
}

func WithModelHook(modelHook any) OpContextOption {
return func(opContext *OpContext) {
opContext.ModelHook = modelHook
}
}
1 change: 1 addition & 0 deletions deleter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type OpContext struct {
Col *mongo.Collection `opt:"-"`
Filter any `opt:"-"`
MongoOptions any
ModelHook any
}

type (
Expand Down
Loading

0 comments on commit 206e1d3

Please sign in to comment.