Skip to content

Commit 22f577f

Browse files
feat: Upgrade to v2 module and update dependencies (#59)
- Update module and internal import paths to include `/v2` - Upgrade `go.mongodb.org/mongo-driver` dependency to v2 - Update the README.md and README-zh_CN.md - **creator**, **aggregator**, **deleter**: Adjust `Options` parameters for compatibility with mongo-driver v2 - **updater**: Adjust `Options` parameters and update `Upsert` method for compatibility with mongo-driver v2 - **finder**: Adjust `Options` parameters and update `Distinct` and `DistinctWithParse` methods for compatibility with mongo-driver v2 - **bson**: Refactor `dToM` function for compatibility with mongo-driver v2
1 parent 7c44763 commit 22f577f

File tree

105 files changed

+567
-683
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+567
-683
lines changed

README-zh_CN.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@
2727
- 支持插件化编程
2828

2929
# 安装
30-
```go
31-
go get github.com/chenmingyong0423/go-mongox
32-
```
30+
- 如果使用 `mongo-driver 1.x` 版本:
31+
```go
32+
go get github.com/chenmingyong0423/go-mongox
33+
```
34+
- 如果使用 `mongo-driver 2.x` 版本:
35+
```go
36+
go get github.com/chenmingyong0423/go-mongox/v2
37+
```
3338

3439
# 快速开始
3540
- `go mongox` 指南: [https://go-mongox.dev](https://go-mongox.dev)

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ English | [中文简体](./README-zh_CN.md)
2727
- Plugin programming support
2828

2929
# Install
30-
```go
31-
go get github.com/chenmingyong0423/go-mongox
32-
```
30+
- If you are using `mongo-driver 1.x` version:
31+
```go
32+
go get github.com/chenmingyong0423/go-mongox
33+
```
34+
- If you are using `mongo-driver 2.x` version:
35+
```go
36+
go get github.com/chenmingyong0423/go-mongox/v2
37+
```
3338

3439
# Getting Started
3540
- `go mongox` Guides: [https://go-mongox.dev](https://go-mongox.dev/en)

aggregator/aggregator.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ package aggregator
1717
import (
1818
"context"
1919

20-
"go.mongodb.org/mongo-driver/mongo"
21-
"go.mongodb.org/mongo-driver/mongo/options"
20+
"go.mongodb.org/mongo-driver/v2/mongo"
21+
"go.mongodb.org/mongo-driver/v2/mongo/options"
2222
)
2323

2424
//go:generate mockgen -source=aggregator.go -destination=../mock/aggregator.mock.go -package=mocks
2525
type iAggregator[T any] interface {
26-
Aggregate(ctx context.Context, opts ...*options.AggregateOptions) ([]*T, error)
27-
AggregateWithParse(ctx context.Context, result any, opts ...*options.AggregateOptions) error
26+
Aggregate(ctx context.Context, opts ...options.Lister[options.AggregateOptions]) ([]*T, error)
27+
AggregateWithParse(ctx context.Context, result any, opts ...options.Lister[options.AggregateOptions]) error
2828
}
2929

30+
var _ iAggregator[any] = (*Aggregator[any])(nil)
31+
3032
type Aggregator[T any] struct {
3133
collection *mongo.Collection
3234
pipeline any
@@ -43,7 +45,7 @@ func (a *Aggregator[T]) Pipeline(pipeline any) *Aggregator[T] {
4345
return a
4446
}
4547

46-
func (a *Aggregator[T]) Aggregate(ctx context.Context, opts ...*options.AggregateOptions) ([]*T, error) {
48+
func (a *Aggregator[T]) Aggregate(ctx context.Context, opts ...options.Lister[options.AggregateOptions]) ([]*T, error) {
4749
cursor, err := a.collection.Aggregate(ctx, a.pipeline, opts...)
4850
if err != nil {
4951
return nil, err
@@ -60,7 +62,7 @@ func (a *Aggregator[T]) Aggregate(ctx context.Context, opts ...*options.Aggregat
6062

6163
// AggregateWithParse is used to parse the result of the aggregation
6264
// result must be a pointer to a slice
63-
func (a *Aggregator[T]) AggregateWithParse(ctx context.Context, result any, opts ...*options.AggregateOptions) error {
65+
func (a *Aggregator[T]) AggregateWithParse(ctx context.Context, result any, opts ...options.Lister[options.AggregateOptions]) error {
6466
cursor, err := a.collection.Aggregate(ctx, a.pipeline, opts...)
6567
if err != nil {
6668
return err

aggregator/aggregator_e2e_test.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,20 @@ import (
2020
"context"
2121
"testing"
2222

23-
"go.mongodb.org/mongo-driver/bson/primitive"
24-
23+
"github.com/chenmingyong0423/go-mongox/v2/bsonx"
2524
"github.com/stretchr/testify/require"
2625

27-
"github.com/chenmingyong0423/go-mongox/bsonx"
28-
29-
"github.com/chenmingyong0423/go-mongox/builder/aggregation"
30-
"github.com/chenmingyong0423/go-mongox/builder/query"
26+
"github.com/chenmingyong0423/go-mongox/v2/builder/aggregation"
27+
"github.com/chenmingyong0423/go-mongox/v2/builder/query"
3128

32-
"go.mongodb.org/mongo-driver/mongo"
33-
"go.mongodb.org/mongo-driver/mongo/options"
34-
"go.mongodb.org/mongo-driver/mongo/readpref"
29+
"go.mongodb.org/mongo-driver/v2/bson"
30+
"go.mongodb.org/mongo-driver/v2/mongo"
31+
"go.mongodb.org/mongo-driver/v2/mongo/options"
32+
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
3533
)
3634

3735
func getCollection(t *testing.T) *mongo.Collection {
38-
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://localhost:27017").SetAuth(options.Credential{
36+
client, err := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017").SetAuth(options.Credential{
3937
Username: "test",
4038
Password: "test",
4139
AuthSource: "db-test",
@@ -64,7 +62,7 @@ func TestAggregator_e2e_Aggregation(t *testing.T) {
6462
after func(ctx context.Context, t *testing.T)
6563

6664
pipeline any
67-
aggregationOptions []*options.AggregateOptions
65+
aggregationOptions []options.Lister[options.AggregateOptions]
6866

6967
ctx context.Context
7068
want []*TestUser
@@ -190,7 +188,7 @@ func TestAggregator_e2e_Aggregation(t *testing.T) {
190188
require.Equal(t, int64(2), deleteResult.DeletedCount)
191189
},
192190
pipeline: aggregation.NewStageBuilder().Sort(bsonx.M("age", 1)).Build(),
193-
aggregationOptions: []*options.AggregateOptions{
191+
aggregationOptions: []options.Lister[options.AggregateOptions]{
194192
options.Aggregate().SetCollation(&options.Collation{Locale: "en", Strength: 2}),
195193
},
196194
want: []*TestUser{
@@ -214,7 +212,7 @@ func TestAggregator_e2e_Aggregation(t *testing.T) {
214212
if err == nil {
215213
require.Equal(t, len(tc.want), len(testUsers))
216214
for _, tu := range testUsers {
217-
var zero primitive.ObjectID
215+
var zero bson.ObjectID
218216
tu.ID = zero
219217
}
220218
require.ElementsMatch(t, tc.want, testUsers)
@@ -239,7 +237,7 @@ func TestAggregator_e2e_AggregateWithParse(t *testing.T) {
239237
before func(ctx context.Context, t *testing.T)
240238
after func(ctx context.Context, t *testing.T)
241239
pipeline any
242-
aggregationOptions *options.AggregateOptions
240+
aggregationOptions []options.Lister[options.AggregateOptions]
243241
ctx context.Context
244242
result any
245243
want []*User
@@ -301,9 +299,11 @@ func TestAggregator_e2e_AggregateWithParse(t *testing.T) {
301299
{Id: "1", Name: "cmy", Age: 24, IsProgrammer: true},
302300
{Id: "2", Name: "gopher", Age: 20, IsProgrammer: true},
303301
},
304-
aggregationOptions: options.Aggregate().SetCollation(&options.Collation{Locale: "en", Strength: 2}),
305-
ctx: context.Background(),
306-
wantErr: require.NoError,
302+
aggregationOptions: []options.Lister[options.AggregateOptions]{
303+
options.Aggregate().SetCollation(&options.Collation{Locale: "en", Strength: 2}),
304+
},
305+
ctx: context.Background(),
306+
wantErr: require.NoError,
307307
},
308308
{
309309
name: "got error from cursor",
@@ -320,18 +320,20 @@ func TestAggregator_e2e_AggregateWithParse(t *testing.T) {
320320
require.NoError(t, err)
321321
require.Equal(t, int64(2), deleteResult.DeletedCount)
322322
},
323-
pipeline: aggregation.NewStageBuilder().Set(bsonx.M("is_programmer", true)).Sort(bsonx.M("name", 1)).Build(),
324-
result: []string{},
325-
want: []*User{},
326-
aggregationOptions: options.Aggregate().SetCollation(&options.Collation{Locale: "en", Strength: 2}),
327-
ctx: context.Background(),
328-
wantErr: require.Error,
323+
pipeline: aggregation.NewStageBuilder().Set(bsonx.M("is_programmer", true)).Sort(bsonx.M("name", 1)).Build(),
324+
result: []string{},
325+
want: []*User{},
326+
aggregationOptions: []options.Lister[options.AggregateOptions]{
327+
options.Aggregate().SetCollation(&options.Collation{Locale: "en", Strength: 2}),
328+
},
329+
ctx: context.Background(),
330+
wantErr: require.Error,
329331
},
330332
}
331333
for _, tc := range testCases {
332334
t.Run(tc.name, func(t *testing.T) {
333335
tc.before(tc.ctx, t)
334-
err := aggregator.Pipeline(tc.pipeline).AggregateWithParse(tc.ctx, &tc.result, tc.aggregationOptions)
336+
err := aggregator.Pipeline(tc.pipeline).AggregateWithParse(tc.ctx, &tc.result, tc.aggregationOptions...)
335337
tc.after(tc.ctx, t)
336338
tc.wantErr(t, err)
337339
if err == nil {

aggregator/aggregator_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ import (
2020
"testing"
2121
"time"
2222

23-
"go.mongodb.org/mongo-driver/bson/primitive"
24-
25-
mocks "github.com/chenmingyong0423/go-mongox/mock"
23+
mocks "github.com/chenmingyong0423/go-mongox/v2/mock"
2624

2725
"github.com/stretchr/testify/assert"
28-
"go.mongodb.org/mongo-driver/mongo"
26+
"go.mongodb.org/mongo-driver/v2/bson"
27+
"go.mongodb.org/mongo-driver/v2/mongo"
2928
"go.uber.org/mock/gomock"
3029
)
3130

3231
type TestUser struct {
33-
ID primitive.ObjectID `bson:"_id,omitempty"`
34-
Name string `bson:"name"`
32+
ID bson.ObjectID `bson:"_id,omitempty"`
33+
Name string `bson:"name"`
3534
Age int64
3635
UnknownField string `bson:"-"`
3736
CreatedAt time.Time `bson:"created_at"`
@@ -56,8 +55,8 @@ type TestTempUser struct {
5655
}
5756

5857
type IllegalUser struct {
59-
ID primitive.ObjectID `bson:"_id,omitempty"`
60-
Name string `bson:"name"`
58+
ID bson.ObjectID `bson:"_id,omitempty"`
59+
Name string `bson:"name"`
6160
Age string
6261
}
6362

bsonx/bsonx.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package bsonx
1616

1717
import (
18-
"go.mongodb.org/mongo-driver/bson"
18+
"bytes"
19+
20+
"go.mongodb.org/mongo-driver/v2/bson"
1921
)
2022

2123
func M(key string, value any) bson.M {
@@ -79,7 +81,9 @@ func dToM(d bson.D) bson.M {
7981
return nil
8082
}
8183
var m bson.M
82-
err = bson.Unmarshal(marshal, &m)
84+
decoder := bson.NewDecoder(bson.NewDocumentReader(bytes.NewReader(marshal)))
85+
decoder.DefaultDocumentM()
86+
err = decoder.Decode(&m)
8387
if err != nil {
8488
return nil
8589
}

bsonx/bsonx_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/stretchr/testify/assert"
21-
"go.mongodb.org/mongo-driver/bson"
21+
"go.mongodb.org/mongo-driver/v2/bson"
2222
)
2323

2424
func TestD(t *testing.T) {

bsonx/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package bsonx
1616

17-
import "go.mongodb.org/mongo-driver/bson"
17+
import "go.mongodb.org/mongo-driver/v2/bson"
1818

1919
// DBuilder is a builder for bson.D
2020
type DBuilder struct {

bsonx/builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/stretchr/testify/assert"
21-
"go.mongodb.org/mongo-driver/bson"
21+
"go.mongodb.org/mongo-driver/v2/bson"
2222
)
2323

2424
func TestDBuilder(t *testing.T) {

builder/aggregation/accumulators_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package aggregation
1616

1717
import (
18-
"go.mongodb.org/mongo-driver/bson"
18+
"go.mongodb.org/mongo-driver/v2/bson"
1919
)
2020

2121
type accumulatorsBuilder struct {

0 commit comments

Comments
 (0)