This commit is contained in:
+4
-4
@@ -2,16 +2,16 @@ package wmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson/bsontype"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"reflect"
|
||||
|
||||
ct "git.blackforestbytes.com/BlackForestBytes/goext/cursortoken"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"reflect"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
type EntityID interface {
|
||||
MarshalBSONValue() (bsontype.Type, []byte, error)
|
||||
MarshalBSONValue() (byte, []byte, error)
|
||||
String() string
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -2,8 +2,9 @@ package wmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) decodeSingle(ctx context.Context, dec Decodable) (TData, error) {
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package wmo
|
||||
|
||||
import "go.mongodb.org/mongo-driver/mongo"
|
||||
import "go.mongodb.org/mongo-driver/v2/mongo"
|
||||
|
||||
func W[TData any](collection *mongo.Collection) *Coll[TData] {
|
||||
c := Coll[TData]{coll: collection}
|
||||
|
||||
@@ -2,13 +2,14 @@ package wmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) ([]TData, error) {
|
||||
func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, opts ...options.Lister[options.AggregateOptions]) ([]TData, error) {
|
||||
|
||||
for _, ppl := range c.extraModPipeline {
|
||||
pipeline = langext.ArrConcat(pipeline, ppl(ctx))
|
||||
@@ -29,7 +30,7 @@ func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, op
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) AggregateOneOpt(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) (*TData, error) {
|
||||
func (c *Coll[TData]) AggregateOneOpt(ctx context.Context, pipeline mongo.Pipeline, opts ...options.Lister[options.AggregateOptions]) (*TData, error) {
|
||||
|
||||
for _, ppl := range c.extraModPipeline {
|
||||
pipeline = langext.ArrConcat(pipeline, ppl(ctx))
|
||||
@@ -53,7 +54,7 @@ func (c *Coll[TData]) AggregateOneOpt(ctx context.Context, pipeline mongo.Pipeli
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) AggregateOne(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) (TData, error) {
|
||||
func (c *Coll[TData]) AggregateOne(ctx context.Context, pipeline mongo.Pipeline, opts ...options.Lister[options.AggregateOptions]) (TData, error) {
|
||||
|
||||
for _, ppl := range c.extraModPipeline {
|
||||
pipeline = langext.ArrConcat(pipeline, ppl(ctx))
|
||||
|
||||
+3
-2
@@ -2,9 +2,10 @@ package wmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) DeleteOneByID(ctx context.Context, id EntityID) error {
|
||||
|
||||
+16
-14
@@ -2,12 +2,13 @@ package wmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"iter"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"iter"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) createFindQuery(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (*mongo.Cursor, error) {
|
||||
@@ -51,7 +52,7 @@ func (c *Coll[TData]) createFindQuery(ctx context.Context, filter bson.M, opts .
|
||||
}
|
||||
}
|
||||
|
||||
convOpts := make([]*options.AggregateOptions, 0, len(opts))
|
||||
convOpts := make([]*options.AggregateOptionsBuilder, 0, len(opts))
|
||||
for _, v := range opts {
|
||||
vConv, err := convertFindOpt(v)
|
||||
if err != nil {
|
||||
@@ -60,7 +61,14 @@ func (c *Coll[TData]) createFindQuery(ctx context.Context, filter bson.M, opts .
|
||||
convOpts = append(convOpts, vConv)
|
||||
}
|
||||
|
||||
cursor, err := c.coll.Aggregate(ctx, pipeline, convOpts...)
|
||||
convOptsLister := make([]options.Lister[options.AggregateOptions], 0, len(convOpts))
|
||||
for _, v := range convOpts {
|
||||
if v != nil {
|
||||
convOptsLister = append(convOptsLister, v)
|
||||
}
|
||||
}
|
||||
|
||||
cursor, err := c.coll.Aggregate(ctx, pipeline, convOptsLister...)
|
||||
if err != nil {
|
||||
return nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build()
|
||||
}
|
||||
@@ -137,7 +145,7 @@ func (c *Coll[TData]) FindIterate(ctx context.Context, filter bson.M, opts ...*o
|
||||
}
|
||||
|
||||
// converts FindOptions to AggregateOptions
|
||||
func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) {
|
||||
func convertFindOpt(v *options.FindOptions) (*options.AggregateOptionsBuilder, error) {
|
||||
if v == nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -157,7 +165,7 @@ func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) {
|
||||
r.SetCollation(v.Collation)
|
||||
}
|
||||
if v.Comment != nil {
|
||||
r.SetComment(*v.Comment)
|
||||
r.SetComment(v.Comment)
|
||||
}
|
||||
if v.CursorType != nil {
|
||||
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'CursorType' (cannot convert to AggregateOptions)").Build()
|
||||
@@ -171,9 +179,6 @@ func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) {
|
||||
if v.MaxAwaitTime != nil {
|
||||
r.SetMaxAwaitTime(*v.MaxAwaitTime)
|
||||
}
|
||||
if v.MaxTime != nil {
|
||||
r.SetMaxTime(*v.MaxTime)
|
||||
}
|
||||
if v.Min != nil {
|
||||
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'Min' (cannot convert to AggregateOptions)").Build()
|
||||
}
|
||||
@@ -189,9 +194,6 @@ func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) {
|
||||
if v.ShowRecordID != nil {
|
||||
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'ShowRecordID' (cannot convert to AggregateOptions)").Build()
|
||||
}
|
||||
if v.Snapshot != nil {
|
||||
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'Snapshot' (cannot convert to AggregateOptions)").Build()
|
||||
}
|
||||
if v.Let != nil {
|
||||
r.SetLet(v.Let)
|
||||
}
|
||||
|
||||
+3
-2
@@ -3,10 +3,11 @@ package wmo
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) FindOne(ctx context.Context, filter bson.M) (TData, error) {
|
||||
|
||||
+3
-2
@@ -2,10 +2,11 @@ package wmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, error) {
|
||||
|
||||
+4
-3
@@ -2,12 +2,13 @@ package wmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"iter"
|
||||
|
||||
ct "git.blackforestbytes.com/BlackForestBytes/goext/cursortoken"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"iter"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CursorToken) ([]TData, ct.CursorToken, error) {
|
||||
|
||||
@@ -2,12 +2,13 @@ package wmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"iter"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
pag "git.blackforestbytes.com/BlackForestBytes/goext/pagination"
|
||||
"iter"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.MongoFilter, page int, limit *int) ([]TData, pag.Pagination, error) {
|
||||
|
||||
+4
-3
@@ -2,10 +2,11 @@ package wmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) FindOneAndUpdate(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (TData, error) {
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/rfctime"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/timeext"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
)
|
||||
|
||||
func TestReflectionGetFieldType(t *testing.T) {
|
||||
@@ -235,12 +235,11 @@ func TestReflectionGetFieldValueAsTokenString(t *testing.T) {
|
||||
func TestReflectionWithInterface(t *testing.T) {
|
||||
|
||||
type TestData struct {
|
||||
ID primitive.ObjectID `bson:"_id"`
|
||||
CDate time.Time `bson:"cdate"`
|
||||
ID bson.ObjectID `bson:"_id"`
|
||||
CDate time.Time `bson:"cdate"`
|
||||
}
|
||||
|
||||
type TestInterface interface {
|
||||
}
|
||||
type TestInterface any
|
||||
|
||||
coll1 := W[TestInterface](&mongo.Collection{})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user