Skip to content

Commit 156bccb

Browse files
authored
feat: Add the full Implementation of methods in interfaces (#91)
1 parent 1e937a2 commit 156bccb

20 files changed

+910
-328
lines changed

creator/creator.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ import (
3535
type ICreator[T any] interface {
3636
InsertOne(ctx context.Context, docs *T, opts ...options.Lister[options.InsertOneOptions]) (*mongo.InsertOneResult, error)
3737
InsertMany(ctx context.Context, docs []*T, opts ...options.Lister[options.InsertManyOptions]) (*mongo.InsertManyResult, error)
38+
ModelHook(modelHook any) ICreator[T]
39+
RegisterAfterHooks(hooks ...HookFn[T]) ICreator[T]
40+
RegisterBeforeHooks(hooks ...HookFn[T]) ICreator[T]
41+
GetCollection() *mongo.Collection
3842
}
3943

4044
var _ ICreator[any] = (*Creator[any])(nil)
@@ -44,45 +48,45 @@ type Creator[T any] struct {
4448

4549
modelHook any
4650

47-
dbCallbacks *callback.Callback
48-
beforeHooks []hookFn[T]
49-
afterHooks []hookFn[T]
51+
DBCallbacks *callback.Callback
52+
BeforeHooks []HookFn[T]
53+
AfterHooks []HookFn[T]
5054

5155
fields []*field.Filed
5256
}
5357

5458
func NewCreator[T any](collection *mongo.Collection, dbCallbacks *callback.Callback, fields []*field.Filed) *Creator[T] {
5559
return &Creator[T]{
5660
collection: collection,
57-
dbCallbacks: dbCallbacks,
61+
DBCallbacks: dbCallbacks,
5862
fields: fields,
5963
}
6064
}
6165

62-
func (c *Creator[T]) ModelHook(modelHook any) *Creator[T] {
66+
func (c *Creator[T]) ModelHook(modelHook any) ICreator[T] {
6367
c.modelHook = modelHook
6468
return c
6569
}
6670

6771
// RegisterBeforeHooks is used to set the after hooks of the insert operation
6872
// If you register the hook for InsertOne, the opContext.Docs will be nil
6973
// If you register the hook for InsertMany, the opContext.Doc will be nil
70-
func (c *Creator[T]) RegisterBeforeHooks(hooks ...hookFn[T]) *Creator[T] {
71-
c.beforeHooks = append(c.beforeHooks, hooks...)
74+
func (c *Creator[T]) RegisterBeforeHooks(hooks ...HookFn[T]) ICreator[T] {
75+
c.BeforeHooks = append(c.BeforeHooks, hooks...)
7276
return c
7377
}
7478

75-
func (c *Creator[T]) RegisterAfterHooks(hooks ...hookFn[T]) *Creator[T] {
76-
c.afterHooks = append(c.afterHooks, hooks...)
79+
func (c *Creator[T]) RegisterAfterHooks(hooks ...HookFn[T]) ICreator[T] {
80+
c.AfterHooks = append(c.AfterHooks, hooks...)
7781
return c
7882
}
7983

8084
func (c *Creator[T]) preActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *OpContext[T], opType operation.OpType) error {
81-
err := c.dbCallbacks.Execute(ctx, globalOpContext, opType)
85+
err := c.DBCallbacks.Execute(ctx, globalOpContext, opType)
8286
if err != nil {
8387
return err
8488
}
85-
for _, beforeHook := range c.beforeHooks {
89+
for _, beforeHook := range c.BeforeHooks {
8690
err = beforeHook(ctx, opContext)
8791
if err != nil {
8892
return err
@@ -92,11 +96,11 @@ func (c *Creator[T]) preActionHandler(ctx context.Context, globalOpContext *oper
9296
}
9397

9498
func (c *Creator[T]) postActionHandler(ctx context.Context, globalOpContext *operation.OpContext, opContext *OpContext[T], opType operation.OpType) error {
95-
err := c.dbCallbacks.Execute(ctx, globalOpContext, opType)
99+
err := c.DBCallbacks.Execute(ctx, globalOpContext, opType)
96100
if err != nil {
97101
return err
98102
}
99-
for _, afterHook := range c.afterHooks {
103+
for _, afterHook := range c.AfterHooks {
100104
err = afterHook(ctx, opContext)
101105
if err != nil {
102106
return err
@@ -157,3 +161,6 @@ func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...options.
157161
}
158162
return result, nil
159163
}
164+
func (c *Creator[T]) GetCollection() *mongo.Collection {
165+
return c.collection
166+
}

creator/creator_e2e_test.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414

1515
//go:build e2e
1616

17-
package creator
17+
package creator_test
1818

1919
import (
2020
"context"
2121
"errors"
2222
"testing"
2323
"time"
2424

25+
xcreator "github.com/chenmingyong0423/go-mongox/v2/creator"
2526
"github.com/chenmingyong0423/go-mongox/v2/field"
2627

2728
"github.com/chenmingyong0423/go-mongox/v2/callback"
@@ -70,7 +71,7 @@ func newCollection(t *testing.T) *mongo.Collection {
7071

7172
func TestCreator_e2e_One(t *testing.T) {
7273
collection := newCollection(t)
73-
creator := NewCreator[User](collection, callback.InitializeCallbacks(), field.ParseFields(User{}))
74+
creator := xcreator.NewCreator[User](collection, callback.InitializeCallbacks(), field.ParseFields(User{}))
7475

7576
type globalHook struct {
7677
opType operation.OpType
@@ -86,8 +87,8 @@ func TestCreator_e2e_One(t *testing.T) {
8687
ctx context.Context
8788
doc *User
8889
globalHook []globalHook
89-
beforeHook []hookFn[User]
90-
afterHook []hookFn[User]
90+
beforeHook []xcreator.HookFn[User]
91+
afterHook []xcreator.HookFn[User]
9192

9293
wantError assert.ErrorAssertionFunc
9394
}{
@@ -226,8 +227,8 @@ func TestCreator_e2e_One(t *testing.T) {
226227
options.InsertOne().SetComment("test"),
227228
},
228229
doc: nil,
229-
beforeHook: []hookFn[User]{
230-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
230+
beforeHook: []xcreator.HookFn[User]{
231+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
231232
return errors.New("before hook error")
232233
},
233234
},
@@ -251,8 +252,8 @@ func TestCreator_e2e_One(t *testing.T) {
251252
Name: "Mingyong Chen",
252253
Age: 18,
253254
},
254-
afterHook: []hookFn[User]{
255-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
255+
afterHook: []xcreator.HookFn[User]{
256+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
256257
return errors.New("after hook error")
257258
},
258259
},
@@ -276,16 +277,16 @@ func TestCreator_e2e_One(t *testing.T) {
276277
Name: "Mingyong Chen",
277278
Age: 18,
278279
},
279-
beforeHook: []hookFn[User]{
280-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
280+
beforeHook: []xcreator.HookFn[User]{
281+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
281282
if opContext.Doc == nil {
282283
return errors.New("before hook error")
283284
}
284285
return nil
285286
},
286287
},
287-
afterHook: []hookFn[User]{
288-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
288+
afterHook: []xcreator.HookFn[User]{
289+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
289290
if opContext == nil {
290291
return errors.New("after hook error")
291292
}
@@ -322,8 +323,8 @@ func TestCreator_e2e_One(t *testing.T) {
322323
Name: "Mingyong Chen",
323324
Age: 18,
324325
},
325-
afterHook: []hookFn[User]{
326-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
326+
afterHook: []xcreator.HookFn[User]{
327+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
327328
user := opContext.Doc
328329
if user == nil {
329330
return errors.New("user is nil")
@@ -372,7 +373,7 @@ func TestCreator_e2e_One(t *testing.T) {
372373
t.Run(tc.name, func(t *testing.T) {
373374
tc.before(tc.ctx, t)
374375
for _, hook := range tc.globalHook {
375-
creator.dbCallbacks.Register(hook.opType, hook.name, hook.fn)
376+
creator.DBCallbacks.Register(hook.opType, hook.name, hook.fn)
376377
}
377378
insertOneResult, err := creator.RegisterBeforeHooks(tc.beforeHook...).
378379
RegisterAfterHooks(tc.afterHook...).InsertOne(tc.ctx, tc.doc, tc.opts...)
@@ -384,17 +385,17 @@ func TestCreator_e2e_One(t *testing.T) {
384385
require.NotNil(t, insertOneResult.InsertedID)
385386
}
386387
for _, hook := range tc.globalHook {
387-
creator.dbCallbacks.Remove(hook.opType, hook.name)
388+
creator.DBCallbacks.Remove(hook.opType, hook.name)
388389
}
389-
creator.beforeHooks = nil
390-
creator.afterHooks = nil
390+
creator.BeforeHooks = nil
391+
creator.AfterHooks = nil
391392
})
392393
}
393394
}
394395

395396
func TestCreator_e2e_Many(t *testing.T) {
396397
collection := newCollection(t)
397-
creator := NewCreator[User](collection, callback.InitializeCallbacks(), field.ParseFields(User{}))
398+
creator := xcreator.NewCreator[User](collection, callback.InitializeCallbacks(), field.ParseFields(User{}))
398399

399400
type globalHook struct {
400401
opType operation.OpType
@@ -411,8 +412,8 @@ func TestCreator_e2e_Many(t *testing.T) {
411412
docs []*User
412413
opts []options.Lister[options.InsertManyOptions]
413414
globalHook []globalHook
414-
beforeHook []hookFn[User]
415-
afterHook []hookFn[User]
415+
beforeHook []xcreator.HookFn[User]
416+
afterHook []xcreator.HookFn[User]
416417

417418
wantIdsLength int
418419
wantError assert.ErrorAssertionFunc
@@ -566,8 +567,8 @@ func TestCreator_e2e_Many(t *testing.T) {
566567
options.InsertMany().SetComment("test"),
567568
},
568569
docs: nil,
569-
beforeHook: []hookFn[User]{
570-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
570+
beforeHook: []xcreator.HookFn[User]{
571+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
571572
return errors.New("before hook error")
572573
},
573574
},
@@ -597,8 +598,8 @@ func TestCreator_e2e_Many(t *testing.T) {
597598
Age: 19,
598599
},
599600
},
600-
afterHook: []hookFn[User]{
601-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
601+
afterHook: []xcreator.HookFn[User]{
602+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
602603
return errors.New("after hook error")
603604
},
604605
},
@@ -628,16 +629,16 @@ func TestCreator_e2e_Many(t *testing.T) {
628629
Age: 19,
629630
},
630631
},
631-
beforeHook: []hookFn[User]{
632-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
632+
beforeHook: []xcreator.HookFn[User]{
633+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
633634
if len(opContext.Docs) != 2 {
634635
return errors.New("before hook error")
635636
}
636637
return nil
637638
},
638639
},
639-
afterHook: []hookFn[User]{
640-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
640+
afterHook: []xcreator.HookFn[User]{
641+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
641642
if opContext == nil {
642643
return errors.New("after hook error")
643644
}
@@ -683,8 +684,8 @@ func TestCreator_e2e_Many(t *testing.T) {
683684
Age: 18,
684685
},
685686
},
686-
afterHook: []hookFn[User]{
687-
func(ctx context.Context, opContext *OpContext[User], opts ...any) error {
687+
afterHook: []xcreator.HookFn[User]{
688+
func(ctx context.Context, opContext *xcreator.OpContext[User], opts ...any) error {
688689
users := opContext.Docs
689690
if users == nil {
690691
return errors.New("users is nil")
@@ -736,7 +737,7 @@ func TestCreator_e2e_Many(t *testing.T) {
736737
t.Run(tc.name, func(t *testing.T) {
737738
tc.before(tc.ctx, t)
738739
for _, hook := range tc.globalHook {
739-
creator.dbCallbacks.Register(hook.opType, hook.name, hook.fn)
740+
creator.DBCallbacks.Register(hook.opType, hook.name, hook.fn)
740741
}
741742
insertManyResult, err := creator.RegisterBeforeHooks(tc.beforeHook...).
742743
RegisterAfterHooks(tc.afterHook...).InsertMany(tc.ctx, tc.docs, tc.opts...)
@@ -749,10 +750,10 @@ func TestCreator_e2e_Many(t *testing.T) {
749750
require.Len(t, insertManyResult.InsertedIDs, tc.wantIdsLength)
750751
}
751752
for _, hook := range tc.globalHook {
752-
creator.dbCallbacks.Remove(hook.opType, hook.name)
753+
creator.DBCallbacks.Remove(hook.opType, hook.name)
753754
}
754-
creator.beforeHooks = nil
755-
creator.afterHooks = nil
755+
creator.BeforeHooks = nil
756+
creator.AfterHooks = nil
756757
})
757758
}
758759
}

creator/creator_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package creator
15+
package creator_test
1616

1717
import (
1818
"context"
1919
"errors"
2020
"testing"
2121
"time"
2222

23+
creator "github.com/chenmingyong0423/go-mongox/v2/creator"
2324
mocks "github.com/chenmingyong0423/go-mongox/v2/mock"
2425
"github.com/stretchr/testify/assert"
2526
"github.com/stretchr/testify/require"
@@ -49,24 +50,24 @@ func (tu *TestUser) DefaultUpdatedAt() {
4950

5051
func TestNewCreator(t *testing.T) {
5152
mongoCollection := &mongo.Collection{}
52-
creator := NewCreator[any](mongoCollection, nil, nil)
53+
creator := creator.NewCreator[any](mongoCollection, nil, nil)
5354

5455
assert.NotNil(t, creator)
55-
assert.Equal(t, mongoCollection, creator.collection)
56+
assert.Equal(t, mongoCollection, creator.GetCollection())
5657
}
5758

5859
func TestCreator_One(t *testing.T) {
5960
testCases := []struct {
6061
name string
61-
mock func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) ICreator[TestUser]
62+
mock func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) creator.ICreator[TestUser]
6263
ctx context.Context
6364
doc *TestUser
6465

6566
wantErr error
6667
}{
6768
{
6869
name: "nil doc",
69-
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) ICreator[TestUser] {
70+
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) creator.ICreator[TestUser] {
7071
mockCollection := mocks.NewMockICreator[TestUser](ctl)
7172
mockCollection.EXPECT().InsertOne(ctx, doc).Return(nil, errors.New("nil filter")).Times(1)
7273
return mockCollection
@@ -77,7 +78,7 @@ func TestCreator_One(t *testing.T) {
7778
},
7879
{
7980
name: "success",
80-
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) ICreator[TestUser] {
81+
mock: func(ctx context.Context, ctl *gomock.Controller, doc *TestUser) creator.ICreator[TestUser] {
8182
mockCollection := mocks.NewMockICreator[TestUser](ctl)
8283
mockCollection.EXPECT().InsertOne(ctx, doc).Return(&mongo.InsertOneResult{InsertedID: "?"}, nil).Times(1)
8384
return mockCollection
@@ -107,7 +108,7 @@ func TestCreator_One(t *testing.T) {
107108
func TestCreator_Many(t *testing.T) {
108109
testCases := []struct {
109110
name string
110-
mock func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) ICreator[TestUser]
111+
mock func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) creator.ICreator[TestUser]
111112
ctx context.Context
112113
docs []*TestUser
113114

@@ -116,7 +117,7 @@ func TestCreator_Many(t *testing.T) {
116117
}{
117118
{
118119
name: "nil docs",
119-
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) ICreator[TestUser] {
120+
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) creator.ICreator[TestUser] {
120121
mockCollection := mocks.NewMockICreator[TestUser](ctl)
121122
mockCollection.EXPECT().InsertMany(ctx, docs).Return(nil, errors.New("nil docs")).Times(1)
122123
return mockCollection
@@ -130,7 +131,7 @@ func TestCreator_Many(t *testing.T) {
130131
},
131132
{
132133
name: "success",
133-
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) ICreator[TestUser] {
134+
mock: func(ctx context.Context, ctl *gomock.Controller, docs []*TestUser) creator.ICreator[TestUser] {
134135
mockCollection := mocks.NewMockICreator[TestUser](ctl)
135136
mockCollection.EXPECT().InsertMany(ctx, docs).Return(&mongo.InsertManyResult{InsertedIDs: make([]interface{}, 2)}, nil).Times(1)
136137
return mockCollection

creator/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type (
4343

4444
Result any
4545
}
46-
hookFn[T any] func(ctx context.Context, opContext *OpContext[T], opts ...any) error
46+
HookFn[T any] func(ctx context.Context, opContext *OpContext[T], opts ...any) error
4747
)
4848

4949
type OpContextOption[T any] func(*OpContext[T])

0 commit comments

Comments
 (0)