Skip to content

Commit 427c287

Browse files
Merge pull request #4 from chenmingyong0423/feature/enhancement
Optimize Memory Usage and Performance
2 parents 099472c + 0b8b279 commit 427c287

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

creator/creator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525

2626
//go:generate mockgen -source=creator.go -destination=../mock/creator.mock.go -package=mocks
2727
type iCreator[T any] interface {
28-
InsertOne(ctx context.Context, docs T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
29-
InsertMany(ctx context.Context, docs []T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
28+
InsertOne(ctx context.Context, docs *T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
29+
InsertMany(ctx context.Context, docs []*T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
3030
}
3131

3232
type Creator[T any] struct {
@@ -39,10 +39,10 @@ func NewCreator[T any](collection *mongo.Collection) *Creator[T] {
3939
}
4040
}
4141

42-
func (c *Creator[T]) InsertOne(ctx context.Context, doc T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
42+
func (c *Creator[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
4343
return c.collection.InsertOne(ctx, doc, opts...)
4444
}
4545

46-
func (c *Creator[T]) InsertMany(ctx context.Context, docs []T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) {
46+
func (c *Creator[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) {
4747
return c.collection.InsertMany(ctx, utils.ToAnySlice(docs...), opts...)
4848
}

creator/creator_e2e_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ func TestCreator_e2e_One(t *testing.T) {
5252
after func(ctx context.Context, t *testing.T)
5353
opts []*options.InsertOneOptions
5454
ctx context.Context
55-
t types.TestUser
55+
t *types.TestUser
5656

5757
wantId string
5858
wantError assert.ErrorAssertionFunc
5959
}{
6060
{
6161
name: "duplicate",
6262
before: func(ctx context.Context, t *testing.T) {
63-
oneResult, err := collection.InsertOne(ctx, types.TestUser{
63+
oneResult, err := collection.InsertOne(ctx, &types.TestUser{
6464
Id: "123",
6565
Name: "cmy",
6666
Age: 24,
@@ -77,7 +77,7 @@ func TestCreator_e2e_One(t *testing.T) {
7777
opts: []*options.InsertOneOptions{
7878
options.InsertOne().SetComment("test"),
7979
},
80-
t: types.TestUser{
80+
t: &types.TestUser{
8181
Id: "123",
8282
Name: "cmy",
8383
Age: 24,
@@ -99,7 +99,7 @@ func TestCreator_e2e_One(t *testing.T) {
9999
opts: []*options.InsertOneOptions{
100100
options.InsertOne().SetComment("test"),
101101
},
102-
t: types.TestUser{
102+
t: &types.TestUser{
103103
Id: "123",
104104
Name: "cmy",
105105
Age: 24,
@@ -138,7 +138,7 @@ func TestCreator_e2e_Many(t *testing.T) {
138138
after func(ctx context.Context, t *testing.T)
139139

140140
ctx context.Context
141-
t []types.TestUser
141+
t []*types.TestUser
142142
opts []*options.InsertManyOptions
143143

144144
wantIds []string
@@ -147,7 +147,7 @@ func TestCreator_e2e_Many(t *testing.T) {
147147
{
148148
name: "duplicate",
149149
before: func(ctx context.Context, t *testing.T) {
150-
oneResult, err := collection.InsertOne(ctx, types.TestUser{
150+
oneResult, err := collection.InsertOne(ctx, &types.TestUser{
151151
Id: "123",
152152
Name: "cmy",
153153
Age: 24,
@@ -164,7 +164,7 @@ func TestCreator_e2e_Many(t *testing.T) {
164164
options.InsertMany().SetComment("test"),
165165
},
166166
ctx: context.Background(),
167-
t: []types.TestUser{
167+
t: []*types.TestUser{
168168
{
169169
Id: "123",
170170
Name: "cmy",
@@ -188,7 +188,7 @@ func TestCreator_e2e_Many(t *testing.T) {
188188
options.InsertMany().SetComment("test"),
189189
},
190190
ctx: context.Background(),
191-
t: []types.TestUser{
191+
t: []*types.TestUser{
192192
{
193193
Id: "123",
194194
Name: "cmy",

creator/creator_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestCreator_One(t *testing.T) {
4040
name string
4141
mock func(ctx context.Context, ctl *gomock.Controller) iCreator[types.TestUser]
4242
ctx context.Context
43-
doc types.TestUser
43+
doc *types.TestUser
4444

4545
wantId string
4646
wantErr error
@@ -49,31 +49,31 @@ func TestCreator_One(t *testing.T) {
4949
name: "duplicate",
5050
mock: func(ctx context.Context, ctl *gomock.Controller) iCreator[types.TestUser] {
5151
mockCollection := mocks.NewMockiCreator[types.TestUser](ctl)
52-
mockCollection.EXPECT().InsertOne(ctx, types.TestUser{
52+
mockCollection.EXPECT().InsertOne(ctx, &types.TestUser{
5353
Id: "123",
5454
Name: "cmy",
5555
Age: 18,
5656
}).Return(nil, errors.New("duplicate key")).Times(1)
5757
return mockCollection
5858
},
5959
ctx: context.Background(),
60-
doc: types.TestUser{Id: "123", Name: "cmy", Age: 18},
60+
doc: &types.TestUser{Id: "123", Name: "cmy", Age: 18},
6161
wantId: "",
6262
wantErr: errors.New("duplicate key"),
6363
},
6464
{
6565
name: "success",
6666
mock: func(ctx context.Context, ctl *gomock.Controller) iCreator[types.TestUser] {
6767
mockCollection := mocks.NewMockiCreator[types.TestUser](ctl)
68-
mockCollection.EXPECT().InsertOne(ctx, types.TestUser{
68+
mockCollection.EXPECT().InsertOne(ctx, &types.TestUser{
6969
Id: "123",
7070
Name: "cmy",
7171
Age: 18,
7272
}).Return(&mongo.InsertOneResult{InsertedID: "123"}, nil).Times(1)
7373
return mockCollection
7474
},
7575
ctx: context.Background(),
76-
doc: types.TestUser{
76+
doc: &types.TestUser{
7777
Id: "123",
7878
Name: "cmy",
7979
Age: 18,
@@ -101,7 +101,7 @@ func TestCreator_Many(t *testing.T) {
101101
name string
102102
mock func(ctx context.Context, ctl *gomock.Controller) iCreator[types.TestUser]
103103
ctx context.Context
104-
doc []types.TestUser
104+
doc []*types.TestUser
105105

106106
wantIds []string
107107
wantErr error
@@ -110,7 +110,7 @@ func TestCreator_Many(t *testing.T) {
110110
name: "duplicate",
111111
mock: func(ctx context.Context, ctl *gomock.Controller) iCreator[types.TestUser] {
112112
mockCollection := mocks.NewMockiCreator[types.TestUser](ctl)
113-
mockCollection.EXPECT().InsertMany(ctx, []types.TestUser{
113+
mockCollection.EXPECT().InsertMany(ctx, []*types.TestUser{
114114
{
115115
Id: "123",
116116
Name: "cmy",
@@ -124,7 +124,7 @@ func TestCreator_Many(t *testing.T) {
124124
return mockCollection
125125
},
126126
ctx: context.Background(),
127-
doc: []types.TestUser{
127+
doc: []*types.TestUser{
128128
{Id: "123", Name: "cmy", Age: 18},
129129
{Id: "123", Name: "cmy", Age: 18},
130130
},
@@ -135,7 +135,7 @@ func TestCreator_Many(t *testing.T) {
135135
name: "success",
136136
mock: func(ctx context.Context, ctl *gomock.Controller) iCreator[types.TestUser] {
137137
mockCollection := mocks.NewMockiCreator[types.TestUser](ctl)
138-
mockCollection.EXPECT().InsertMany(ctx, []types.TestUser{
138+
mockCollection.EXPECT().InsertMany(ctx, []*types.TestUser{
139139
{
140140
Id: "123",
141141
Name: "cmy",
@@ -149,7 +149,7 @@ func TestCreator_Many(t *testing.T) {
149149
return mockCollection
150150
},
151151
ctx: context.Background(),
152-
doc: []types.TestUser{
152+
doc: []*types.TestUser{
153153
{Id: "123", Name: "cmy", Age: 18},
154154
{Id: "456", Name: "cmy", Age: 18},
155155
},

mock/creator.mock.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)