Skip to content

Commit b7ef098

Browse files
feat(updater): add partial hook
1 parent 6d83fa6 commit b7ef098

File tree

6 files changed

+869
-157
lines changed

6 files changed

+869
-157
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Generated by optioner -type AfterOpContext; 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 updater
6+
7+
import (
8+
"go.mongodb.org/mongo-driver/mongo"
9+
)
10+
11+
type AfterOpContextOption func(*AfterOpContext)
12+
13+
func NewAfterOpContext(col *mongo.Collection, condContext *CondContext, opts ...AfterOpContextOption) *AfterOpContext {
14+
afterOpContext := &AfterOpContext{
15+
Col: col,
16+
CondContext: condContext,
17+
}
18+
19+
for _, opt := range opts {
20+
opt(afterOpContext)
21+
}
22+
23+
return afterOpContext
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Generated by optioner -type BeforeOpContext; 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 updater
6+
7+
import (
8+
"go.mongodb.org/mongo-driver/mongo"
9+
)
10+
11+
type BeforeOpContextOption func(*BeforeOpContext)
12+
13+
func NewBeforeOpContext(col *mongo.Collection, condContext *CondContext, opts ...BeforeOpContextOption) *BeforeOpContext {
14+
beforeOpContext := &BeforeOpContext{
15+
Col: col,
16+
CondContext: condContext,
17+
}
18+
19+
for _, opt := range opts {
20+
opt(beforeOpContext)
21+
}
22+
23+
return beforeOpContext
24+
}

updater/opt_cond_context_gen.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Generated by optioner -type CondContext; 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 updater
6+
7+
type CondContextOption func(*CondContext)
8+
9+
func NewCondContext(filter any, opts ...CondContextOption) *CondContext {
10+
condContext := &CondContext{
11+
Filter: filter,
12+
}
13+
14+
for _, opt := range opts {
15+
opt(condContext)
16+
}
17+
18+
return condContext
19+
}
20+
21+
func WithUpdates(updates any) CondContextOption {
22+
return func(condContext *CondContext) {
23+
condContext.Updates = updates
24+
}
25+
}
26+
27+
func WithReplacement(replacement any) CondContextOption {
28+
return func(condContext *CondContext) {
29+
condContext.Replacement = replacement
30+
}
31+
}

updater/types.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2024 chenmingyong0423
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package updater
16+
17+
import (
18+
"context"
19+
20+
"go.mongodb.org/mongo-driver/mongo"
21+
)
22+
23+
//go:generate optioner -type CondContext
24+
type CondContext struct {
25+
Filter any `opt:"-"`
26+
Updates any
27+
Replacement any
28+
}
29+
30+
//go:generate optioner -type BeforeOpContext
31+
type BeforeOpContext struct {
32+
Col *mongo.Collection `opt:"-"`
33+
*CondContext `opt:"-"`
34+
}
35+
36+
//go:generate optioner -type AfterOpContext
37+
type AfterOpContext struct {
38+
Col *mongo.Collection `opt:"-"`
39+
*CondContext `opt:"-"`
40+
}
41+
42+
type (
43+
beforeHookFn func(ctx context.Context, opContext *BeforeOpContext, opts ...any) error
44+
afterHookFn func(ctx context.Context, opContext *AfterOpContext, opts ...any) error
45+
)

updater/updater.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type Updater[T any] struct {
4242
filter any
4343
updates any
4444
replacement any
45+
beforeHooks []beforeHookFn
46+
afterHooks []afterHookFn
4547
}
4648

4749
// Filter is used to set the filter of the query
@@ -66,9 +68,47 @@ func (u *Updater[T]) UpdatesWithOperator(operator string, value any) *Updater[T]
6668
return u
6769
}
6870

71+
func (u *Updater[T]) RegisterBeforeHooks(hooks ...beforeHookFn) *Updater[T] {
72+
u.beforeHooks = append(u.beforeHooks, hooks...)
73+
return u
74+
}
75+
76+
func (u *Updater[T]) RegisterAfterHooks(hooks ...afterHookFn) *Updater[T] {
77+
u.afterHooks = append(u.afterHooks, hooks...)
78+
return u
79+
}
80+
81+
func (u *Updater[T]) preActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *BeforeOpContext, opType operation.OpType) error {
82+
err := callback.GetCallback().Execute(ctx, globalOpContext, opType)
83+
if err != nil {
84+
return err
85+
}
86+
for _, beforeHook := range u.beforeHooks {
87+
err = beforeHook(ctx, opContext)
88+
if err != nil {
89+
return err
90+
}
91+
}
92+
return nil
93+
}
94+
95+
func (u *Updater[T]) postActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *AfterOpContext, opType operation.OpType) error {
96+
err := callback.GetCallback().Execute(ctx, globalOpContext, opType)
97+
if err != nil {
98+
return err
99+
}
100+
for _, afterHook := range u.afterHooks {
101+
err = afterHook(ctx, opContext)
102+
if err != nil {
103+
return err
104+
}
105+
}
106+
return nil
107+
}
108+
69109
func (u *Updater[T]) UpdateOne(ctx context.Context, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
70-
opContext := operation.NewOpContext(u.collection, operation.WithFilter(u.filter), operation.WithUpdate(u.updates))
71-
err := callback.GetCallback().Execute(ctx, opContext, operation.OpTypeBeforeUpdate)
110+
globalOpContext := operation.NewOpContext(u.collection, operation.WithFilter(u.filter), operation.WithUpdate(u.updates))
111+
err := u.preActionHandler(ctx, globalOpContext, NewBeforeOpContext(u.collection, NewCondContext(u.filter, WithUpdates(u.updates))), operation.OpTypeBeforeUpdate)
72112
if err != nil {
73113
return nil, err
74114
}
@@ -78,16 +118,16 @@ func (u *Updater[T]) UpdateOne(ctx context.Context, opts ...*options.UpdateOptio
78118
return nil, err
79119
}
80120

81-
err = callback.GetCallback().Execute(ctx, opContext, operation.OpTypeAfterUpdate)
121+
err = u.postActionHandler(ctx, globalOpContext, NewAfterOpContext(u.collection, NewCondContext(u.filter, WithUpdates(u.updates))), operation.OpTypeAfterUpdate)
82122
if err != nil {
83123
return nil, err
84124
}
85125
return result, nil
86126
}
87127

88128
func (u *Updater[T]) UpdateMany(ctx context.Context, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
89-
opContext := operation.NewOpContext(u.collection, operation.WithFilter(u.filter), operation.WithUpdate(u.updates))
90-
err := callback.GetCallback().Execute(ctx, opContext, operation.OpTypeBeforeUpdate)
129+
globalOpContext := operation.NewOpContext(u.collection, operation.WithFilter(u.filter), operation.WithUpdate(u.updates))
130+
err := u.preActionHandler(ctx, globalOpContext, NewBeforeOpContext(u.collection, NewCondContext(u.filter, WithUpdates(u.updates))), operation.OpTypeBeforeUpdate)
91131
if err != nil {
92132
return nil, err
93133
}
@@ -97,7 +137,7 @@ func (u *Updater[T]) UpdateMany(ctx context.Context, opts ...*options.UpdateOpti
97137
return nil, err
98138
}
99139

100-
err = callback.GetCallback().Execute(ctx, opContext, operation.OpTypeAfterUpdate)
140+
err = u.postActionHandler(ctx, globalOpContext, NewAfterOpContext(u.collection, NewCondContext(u.filter, WithUpdates(u.updates))), operation.OpTypeAfterUpdate)
101141
if err != nil {
102142
return nil, err
103143
}
@@ -111,9 +151,9 @@ func (u *Updater[T]) Upsert(ctx context.Context, opts ...*options.ReplaceOptions
111151
opts[0].SetUpsert(true)
112152
}
113153

114-
opContext := operation.NewOpContext(u.collection, operation.WithFilter(u.filter), operation.WithReplacement(u.replacement))
154+
globalOpContext := operation.NewOpContext(u.collection, operation.WithFilter(u.filter), operation.WithReplacement(u.replacement))
115155

116-
err := callback.GetCallback().Execute(ctx, opContext, operation.OpTypeBeforeUpsert)
156+
err := u.preActionHandler(ctx, globalOpContext, NewBeforeOpContext(u.collection, NewCondContext(u.filter, WithReplacement(u.replacement))), operation.OpTypeBeforeUpsert)
117157
if err != nil {
118158
return nil, err
119159
}
@@ -123,7 +163,7 @@ func (u *Updater[T]) Upsert(ctx context.Context, opts ...*options.ReplaceOptions
123163
return nil, err
124164
}
125165

126-
err = callback.GetCallback().Execute(ctx, opContext, operation.OpTypeAfterUpsert)
166+
err = u.postActionHandler(ctx, globalOpContext, NewAfterOpContext(u.collection, NewCondContext(u.filter, WithReplacement(u.replacement))), operation.OpTypeAfterUpsert)
127167
if err != nil {
128168
return nil, err
129169
}

0 commit comments

Comments
 (0)