From 0c37dd5576c67a104365311a340af3beaf6109de Mon Sep 17 00:00:00 2001 From: viktor Date: Tue, 21 Apr 2026 12:11:38 +0200 Subject: [PATCH] updated mongo driver dependencies to v2 --- cursortoken/filter.go | 2 +- cursortoken/tokenKeySort.go | 14 ++++----- exerr/builder.go | 4 +-- exerr/constructor.go | 4 +-- exerr/dataCategory.go | 53 ++++++---------------------------- exerr/dataSeverity.go | 53 ++++++---------------------------- exerr/dataType.go | 53 ++++++---------------------------- exerr/data_test.go | 29 +++++++++---------- exerr/exerr.go | 2 +- exerr/meta.go | 19 ++++++------- go.mod | 4 +-- mongoext/pipeline.go | 4 +-- mongoext/projections.go | 2 +- mongoext/registry.go | 44 ++++------------------------ pagination/filter.go | 4 +-- reflectext/primStrSer.go | 12 ++++---- rfctime/date.go | 57 +++++++++---------------------------- rfctime/rfc3339.go | 53 ++++++---------------------------- rfctime/rfc3339Nano.go | 53 ++++++---------------------------- rfctime/seconds.go | 53 ++++++---------------------------- rfctime/unix.go | 53 ++++++---------------------------- rfctime/unixMilli.go | 53 ++++++---------------------------- rfctime/unixNano.go | 53 ++++++---------------------------- wmo/collection.go | 5 ++-- wmo/decoding.go | 2 +- wmo/mongo.go | 2 +- wmo/queryAggregate.go | 10 +++---- wmo/queryDelete.go | 4 +-- wmo/queryFind.go | 31 ++++++-------------- wmo/queryFindOne.go | 4 +-- wmo/queryInsert.go | 4 +-- wmo/queryList.go | 4 +-- wmo/queryPaginate.go | 4 +-- wmo/queryUpdate.go | 6 ++-- wmo/reflection_test.go | 8 +++--- wpdf/wpdfTable.go | 7 +++-- 36 files changed, 187 insertions(+), 582 deletions(-) diff --git a/cursortoken/filter.go b/cursortoken/filter.go index 4aa3b29..69a0035 100644 --- a/cursortoken/filter.go +++ b/cursortoken/filter.go @@ -2,7 +2,7 @@ package cursortoken import ( "context" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/mongo" ) type RawFilter interface { diff --git a/cursortoken/tokenKeySort.go b/cursortoken/tokenKeySort.go index 4b03bb6..690176b 100644 --- a/cursortoken/tokenKeySort.go +++ b/cursortoken/tokenKeySort.go @@ -3,7 +3,7 @@ package cursortoken import ( "encoding/base32" "encoding/json" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "time" ) @@ -119,18 +119,18 @@ func (c CTKeySort) IsStart() bool { return c.Mode == CTMStart } -func (c CTKeySort) valuePrimaryObjectId() (primitive.ObjectID, bool) { - if oid, err := primitive.ObjectIDFromHex(c.ValuePrimary); err == nil { +func (c CTKeySort) valuePrimaryObjectId() (bson.ObjectID, bool) { + if oid, err := bson.ObjectIDFromHex(c.ValuePrimary); err == nil { return oid, true } else { - return primitive.ObjectID{}, false + return bson.ObjectID{}, false } } -func (c CTKeySort) valueSecondaryObjectId() (primitive.ObjectID, bool) { - if oid, err := primitive.ObjectIDFromHex(c.ValueSecondary); err == nil { +func (c CTKeySort) valueSecondaryObjectId() (bson.ObjectID, bool) { + if oid, err := bson.ObjectIDFromHex(c.ValueSecondary); err == nil { return oid, true } else { - return primitive.ObjectID{}, false + return bson.ObjectID{}, false } } diff --git a/exerr/builder.go b/exerr/builder.go index ecf8738..7cbcfda 100644 --- a/exerr/builder.go +++ b/exerr/builder.go @@ -16,7 +16,7 @@ import ( "git.blackforestbytes.com/BlackForestBytes/goext/langext" "github.com/gin-gonic/gin" "github.com/rs/zerolog" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" ) // @@ -253,7 +253,7 @@ func (b *Builder) Bytes(key string, val []byte) *Builder { return b.addMeta(key, MDTBytes, val) } -func (b *Builder) ObjectID(key string, val primitive.ObjectID) *Builder { +func (b *Builder) ObjectID(key string, val bson.ObjectID) *Builder { return b.addMeta(key, MDTObjectID, val) } diff --git a/exerr/constructor.go b/exerr/constructor.go index 9e17a90..21d44b1 100644 --- a/exerr/constructor.go +++ b/exerr/constructor.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "git.blackforestbytes.com/BlackForestBytes/goext/langext" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "maps" "reflect" "time" @@ -222,7 +222,7 @@ func getReflectedMetaValues(value any, remainingDepth int) map[string]MetaValue return map[string]MetaValue{"": {DataType: MDTIntArray, Value: ifraw}} case []int32: return map[string]MetaValue{"": {DataType: MDTInt32Array, Value: ifraw}} - case primitive.ObjectID: + case bson.ObjectID: return map[string]MetaValue{"": {DataType: MDTObjectID, Value: ifraw}} case []string: return map[string]MetaValue{"": {DataType: MDTStringArray, Value: ifraw}} diff --git a/exerr/dataCategory.go b/exerr/dataCategory.go index f1fd396..b92680f 100644 --- a/exerr/dataCategory.go +++ b/exerr/dataCategory.go @@ -4,11 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" - "reflect" + "go.mongodb.org/mongo-driver/v2/bson" ) type ErrorCategory struct{ Category string } @@ -28,8 +24,8 @@ func (e ErrorCategory) MarshalJSON() ([]byte, error) { return json.Marshal(e.Category) } -func (e *ErrorCategory) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bson.TypeNull { +func (e *ErrorCategory) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -37,11 +33,11 @@ func (e *ErrorCategory) UnmarshalBSONValue(bt bsontype.Type, data []byte) error *e = ErrorCategory{} return nil } - if bt != bson.TypeString { - return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt)) + if bson.Type(bt) != bson.TypeString { + return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bson.Type(bt))) } var tt string - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) if err != nil { return err } @@ -49,40 +45,9 @@ func (e *ErrorCategory) UnmarshalBSONValue(bt bsontype.Type, data []byte) error return nil } -func (e ErrorCategory) MarshalBSONValue() (bsontype.Type, []byte, error) { - return bson.MarshalValue(e.Category) -} - -func (e ErrorCategory) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = e.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&e)) - } else { - val.Set(reflect.ValueOf(e)) - } - - return nil +func (e ErrorCategory) MarshalBSONValue() (byte, []byte, error) { + tp, data, err := bson.MarshalValue(e.Category) + return byte(tp), data, err } //goland:noinspection GoUnusedGlobalVariable diff --git a/exerr/dataSeverity.go b/exerr/dataSeverity.go index 21b75c8..9441307 100644 --- a/exerr/dataSeverity.go +++ b/exerr/dataSeverity.go @@ -4,11 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" - "reflect" + "go.mongodb.org/mongo-driver/v2/bson" ) type ErrorSeverity struct{ Severity string } @@ -30,8 +26,8 @@ func (e ErrorSeverity) MarshalJSON() ([]byte, error) { return json.Marshal(e.Severity) } -func (e *ErrorSeverity) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bson.TypeNull { +func (e *ErrorSeverity) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -39,11 +35,11 @@ func (e *ErrorSeverity) UnmarshalBSONValue(bt bsontype.Type, data []byte) error *e = ErrorSeverity{} return nil } - if bt != bson.TypeString { - return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt)) + if bson.Type(bt) != bson.TypeString { + return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bson.Type(bt))) } var tt string - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) if err != nil { return err } @@ -51,40 +47,9 @@ func (e *ErrorSeverity) UnmarshalBSONValue(bt bsontype.Type, data []byte) error return nil } -func (e ErrorSeverity) MarshalBSONValue() (bsontype.Type, []byte, error) { - return bson.MarshalValue(e.Severity) -} - -func (e ErrorSeverity) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = e.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&e)) - } else { - val.Set(reflect.ValueOf(e)) - } - - return nil +func (e ErrorSeverity) MarshalBSONValue() (byte, []byte, error) { + tp, data, err := bson.MarshalValue(e.Severity) + return byte(tp), data, err } //goland:noinspection GoUnusedGlobalVariable diff --git a/exerr/dataType.go b/exerr/dataType.go index 2d591d2..c38f76a 100644 --- a/exerr/dataType.go +++ b/exerr/dataType.go @@ -4,13 +4,9 @@ import ( "encoding/json" "errors" "fmt" - "reflect" "git.blackforestbytes.com/BlackForestBytes/goext/dataext" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" + "go.mongodb.org/mongo-driver/v2/bson" ) type ErrorType struct { @@ -80,8 +76,8 @@ func (e ErrorType) MarshalJSON() ([]byte, error) { return json.Marshal(e.Key) } -func (e *ErrorType) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bson.TypeNull { +func (e *ErrorType) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -89,11 +85,11 @@ func (e *ErrorType) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { *e = ErrorType{} return nil } - if bt != bson.TypeString { - return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt)) + if bson.Type(bt) != bson.TypeString { + return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bson.Type(bt))) } var tt string - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) if err != nil { return err } @@ -107,40 +103,9 @@ func (e *ErrorType) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { } } -func (e ErrorType) MarshalBSONValue() (bsontype.Type, []byte, error) { - return bson.MarshalValue(e.Key) -} - -func (e ErrorType) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = e.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&e)) - } else { - val.Set(reflect.ValueOf(e)) - } - - return nil +func (e ErrorType) MarshalBSONValue() (byte, []byte, error) { + tp, data, err := bson.MarshalValue(e.Key) + return byte(tp), data, err } var registeredTypes = dataext.SyncMap[string, ErrorType]{} diff --git a/exerr/data_test.go b/exerr/data_test.go index 8687e22..3bae56b 100644 --- a/exerr/data_test.go +++ b/exerr/data_test.go @@ -3,10 +3,9 @@ package exerr import ( "context" "encoding/json" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" "git.blackforestbytes.com/BlackForestBytes/goext/tst" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" "testing" "time" ) @@ -57,7 +56,7 @@ func TestBSONMarshalErrorCategory(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) defer cancel() - client, err := mongo.Connect(ctx) + client, err := mongo.Connect() if err != nil { t.Skip("Skip test - no local mongo found") return @@ -68,7 +67,7 @@ func TestBSONMarshalErrorCategory(t *testing.T) { return } - primimd := primitive.NewObjectID() + primimd := bson.NewObjectID() _, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": CatSystem}) tst.AssertNoErr(t, err) @@ -76,8 +75,8 @@ func TestBSONMarshalErrorCategory(t *testing.T) { cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}}) var c1 struct { - ID primitive.ObjectID `bson:"_id"` - Val ErrorCategory `bson:"val"` + ID bson.ObjectID `bson:"_id"` + Val ErrorCategory `bson:"val"` } err = cursor.Decode(&c1) @@ -90,7 +89,7 @@ func TestBSONMarshalErrorSeverity(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) defer cancel() - client, err := mongo.Connect(ctx) + client, err := mongo.Connect() if err != nil { t.Skip("Skip test - no local mongo found") return @@ -101,7 +100,7 @@ func TestBSONMarshalErrorSeverity(t *testing.T) { return } - primimd := primitive.NewObjectID() + primimd := bson.NewObjectID() _, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": SevErr}) tst.AssertNoErr(t, err) @@ -109,8 +108,8 @@ func TestBSONMarshalErrorSeverity(t *testing.T) { cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}}) var c1 struct { - ID primitive.ObjectID `bson:"_id"` - Val ErrorSeverity `bson:"val"` + ID bson.ObjectID `bson:"_id"` + Val ErrorSeverity `bson:"val"` } err = cursor.Decode(&c1) @@ -123,7 +122,7 @@ func TestBSONMarshalErrorType(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) defer cancel() - client, err := mongo.Connect(ctx) + client, err := mongo.Connect() if err != nil { t.Skip("Skip test - no local mongo found") return @@ -134,7 +133,7 @@ func TestBSONMarshalErrorType(t *testing.T) { return } - primimd := primitive.NewObjectID() + primimd := bson.NewObjectID() _, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": TypeNotImplemented}) tst.AssertNoErr(t, err) @@ -142,8 +141,8 @@ func TestBSONMarshalErrorType(t *testing.T) { cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}}) var c1 struct { - ID primitive.ObjectID `bson:"_id"` - Val ErrorType `bson:"val"` + ID bson.ObjectID `bson:"_id"` + Val ErrorType `bson:"val"` } err = cursor.Decode(&c1) diff --git a/exerr/exerr.go b/exerr/exerr.go index 7d513b3..3229eb4 100644 --- a/exerr/exerr.go +++ b/exerr/exerr.go @@ -136,7 +136,7 @@ func (ee *ExErr) FormatLog(lvl LogPrintLevel) string { for curr := ee; curr != nil; curr = curr.OriginalError { indent.WriteString(" ") - str.WriteString(indent).String() + str.WriteString(indent.String()) str.WriteString("-> ") strmsg := strings.Trim(curr.Message, " \r\n\t") if lbidx := strings.Index(curr.Message, "\n"); lbidx >= 0 { diff --git a/exerr/meta.go b/exerr/meta.go index b2078b7..2662162 100644 --- a/exerr/meta.go +++ b/exerr/meta.go @@ -7,8 +7,7 @@ import ( "fmt" "git.blackforestbytes.com/BlackForestBytes/goext/langext" "github.com/rs/zerolog" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "math" "strconv" "strings" @@ -99,7 +98,7 @@ func (v MetaValue) SerializeValue() (string, error) { case MDTBytes: return hex.EncodeToString(v.Value.([]byte)), nil case MDTObjectID: - return v.Value.(primitive.ObjectID).Hex(), nil + return v.Value.(bson.ObjectID).Hex(), nil case MDTTime: return strconv.FormatInt(v.Value.(time.Time).Unix(), 10) + "|" + strconv.FormatInt(int64(v.Value.(time.Time).Nanosecond()), 10), nil case MDTDuration: @@ -178,7 +177,7 @@ func (v MetaValue) ShortString(lim int) string { case MDTBytes: return langext.StrLimit(hex.EncodeToString(v.Value.([]byte)), lim, "...") case MDTObjectID: - return v.Value.(primitive.ObjectID).Hex() + return v.Value.(bson.ObjectID).Hex() case MDTTime: return v.Value.(time.Time).Format(time.RFC3339) case MDTDuration: @@ -266,7 +265,7 @@ func (v MetaValue) Apply(key string, evt *zerolog.Event, limitLen *int) *zerolog case MDTBytes: return evt.Bytes(key, v.Value.([]byte)) case MDTObjectID: - return evt.Str(key, v.Value.(primitive.ObjectID).Hex()) + return evt.Str(key, v.Value.(bson.ObjectID).Hex()) case MDTTime: return evt.Time(key, v.Value.(time.Time)) case MDTDuration: @@ -460,7 +459,7 @@ func (v *MetaValue) Deserialize(value string, datatype metaDataType) error { v.DataType = datatype return nil case MDTObjectID: - r, err := primitive.ObjectIDFromHex(value) + r, err := bson.ObjectIDFromHex(value) if err != nil { return err } @@ -577,7 +576,7 @@ func (v MetaValue) ValueString() string { case MDTBytes: return hex.EncodeToString(v.Value.([]byte)) case MDTObjectID: - return v.Value.(primitive.ObjectID).Hex() + return v.Value.(bson.ObjectID).Hex() case MDTTime: return v.Value.(time.Time).Format(time.RFC3339Nano) case MDTDuration: @@ -628,8 +627,8 @@ func (v MetaValue) rawValueForJson() any { if v.Value.(AnyWrap).IsError { return bson.M{"@error": true} } - jsonobj := primitive.M{} - jsonarr := primitive.A{} + jsonobj := bson.M{} + jsonarr := bson.A{} if err := json.Unmarshal([]byte(v.Value.(AnyWrap).Json), &jsonobj); err == nil { return jsonobj } else if err := json.Unmarshal([]byte(v.Value.(AnyWrap).Json), &jsonarr); err == nil { @@ -654,7 +653,7 @@ func (v MetaValue) rawValueForJson() any { return v.Value.(time.Time).Format(time.RFC3339Nano) } if v.DataType == MDTObjectID { - return v.Value.(primitive.ObjectID).Hex() + return v.Value.(bson.ObjectID).Hex() } if v.DataType == MDTNil { return nil diff --git a/go.mod b/go.mod index d8d0e25..b8ac707 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/jmoiron/sqlx v1.4.0 github.com/rs/xid v1.6.0 github.com/rs/zerolog v1.35.1 - go.mongodb.org/mongo-driver v1.17.9 + go.mongodb.org/mongo-driver/v2 v2.5.1 golang.org/x/crypto v0.50.0 golang.org/x/sys v0.43.0 golang.org/x/term v0.42.0 @@ -20,6 +20,7 @@ require ( github.com/gorilla/websocket v1.5.3 github.com/jung-kurt/gofpdf v1.16.2 github.com/xuri/excelize/v2 v2.10.1 + go.mongodb.org/mongo-driver v1.17.9 golang.org/x/sync v0.20.0 ) @@ -64,7 +65,6 @@ require ( github.com/xuri/efp v0.0.1 // indirect github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect - go.mongodb.org/mongo-driver/v2 v2.5.1 // indirect golang.org/x/arch v0.26.0 // indirect golang.org/x/image v0.39.0 // indirect golang.org/x/net v0.53.0 // indirect diff --git a/mongoext/pipeline.go b/mongoext/pipeline.go index 3e430d2..5e8f264 100644 --- a/mongoext/pipeline.go +++ b/mongoext/pipeline.go @@ -1,8 +1,8 @@ package mongoext import ( - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" ) // FixTextSearchPipeline moves {$match:{$text:{$search}}} entries to the front of the pipeline (otherwise its an mongo error) diff --git a/mongoext/projections.go b/mongoext/projections.go index 2be6fce..3e2e991 100644 --- a/mongoext/projections.go +++ b/mongoext/projections.go @@ -1,7 +1,7 @@ package mongoext import ( - "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/v2/bson" "reflect" "strings" ) diff --git a/mongoext/registry.go b/mongoext/registry.go index dad9c65..c520287 100644 --- a/mongoext/registry.go +++ b/mongoext/registry.go @@ -1,51 +1,17 @@ package mongoext import ( - "git.blackforestbytes.com/BlackForestBytes/goext/exerr" - "git.blackforestbytes.com/BlackForestBytes/goext/langext" - "git.blackforestbytes.com/BlackForestBytes/goext/rfctime" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/primitive" "reflect" + + "go.mongodb.org/mongo-driver/v2/bson" ) -func CreateGoExtBsonRegistry() *bsoncodec.Registry { +func CreateGoExtBsonRegistry() *bson.Registry { reg := bson.NewRegistry() - reg.RegisterTypeDecoder(reflect.TypeFor[rfctime.RFC3339Time](), rfctime.RFC3339Time{}) - reg.RegisterTypeDecoder(reflect.TypeFor[*rfctime.RFC3339Time](), rfctime.RFC3339Time{}) - - reg.RegisterTypeDecoder(reflect.TypeFor[rfctime.RFC3339NanoTime](), rfctime.RFC3339NanoTime{}) - reg.RegisterTypeDecoder(reflect.TypeFor[*rfctime.RFC3339NanoTime](), rfctime.RFC3339NanoTime{}) - - reg.RegisterTypeDecoder(reflect.TypeFor[rfctime.UnixTime](), rfctime.UnixTime{}) - reg.RegisterTypeDecoder(reflect.TypeFor[*rfctime.UnixTime](), rfctime.UnixTime{}) - - reg.RegisterTypeDecoder(reflect.TypeFor[rfctime.UnixMilliTime](), rfctime.UnixMilliTime{}) - reg.RegisterTypeDecoder(reflect.TypeFor[*rfctime.UnixMilliTime](), rfctime.UnixMilliTime{}) - - reg.RegisterTypeDecoder(reflect.TypeFor[rfctime.UnixNanoTime](), rfctime.UnixNanoTime{}) - reg.RegisterTypeDecoder(reflect.TypeFor[*rfctime.UnixNanoTime](), rfctime.UnixNanoTime{}) - - reg.RegisterTypeDecoder(reflect.TypeFor[rfctime.Date](), rfctime.Date{}) - reg.RegisterTypeDecoder(reflect.TypeFor[*rfctime.Date](), rfctime.Date{}) - - reg.RegisterTypeDecoder(reflect.TypeFor[rfctime.SecondsF64](), rfctime.SecondsF64(0)) - reg.RegisterTypeDecoder(reflect.TypeOf(langext.Ptr(rfctime.SecondsF64(0))), rfctime.SecondsF64(0)) - - reg.RegisterTypeDecoder(reflect.TypeFor[exerr.ErrorCategory](), exerr.ErrorCategory{}) - reg.RegisterTypeDecoder(reflect.TypeOf(new(exerr.ErrorCategory{})), exerr.ErrorCategory{}) - - reg.RegisterTypeDecoder(reflect.TypeFor[exerr.ErrorSeverity](), exerr.ErrorSeverity{}) - reg.RegisterTypeDecoder(reflect.TypeOf(new(exerr.ErrorSeverity{})), exerr.ErrorSeverity{}) - - reg.RegisterTypeDecoder(reflect.TypeFor[exerr.ErrorType](), exerr.ErrorType{}) - reg.RegisterTypeDecoder(reflect.TypeOf(new(exerr.ErrorType{})), exerr.ErrorType{}) - - // otherwise we get []primitve.E when unmarshalling into any + // otherwise we get []bson.E when unmarshalling into any // which will result in {'key': .., 'value': ...}[] json when json-marshalling - reg.RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeFor[primitive.M]()) + reg.RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeFor[bson.M]()) return reg } diff --git a/pagination/filter.go b/pagination/filter.go index 93df968..bb7dc49 100644 --- a/pagination/filter.go +++ b/pagination/filter.go @@ -2,8 +2,8 @@ package pagination import ( "context" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" ) type MongoFilter interface { diff --git a/reflectext/primStrSer.go b/reflectext/primStrSer.go index 22daa52..546a2af 100644 --- a/reflectext/primStrSer.go +++ b/reflectext/primStrSer.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" "git.blackforestbytes.com/BlackForestBytes/goext/langext" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "reflect" "strconv" "strings" @@ -28,7 +28,7 @@ var primitiveSerializer = map[reflect.Type]genSerializer{ reflect.TypeFor[bool](): newGenSerializer(serBoolToString, serStringToBool), - reflect.TypeFor[primitive.ObjectID](): newGenSerializer(serObjectIDToString, serStringToObjectID), + reflect.TypeFor[bson.ObjectID](): newGenSerializer(serObjectIDToString, serStringToObjectID), reflect.TypeFor[time.Time](): newGenSerializer(serTimeToString, serStringToTime), } @@ -111,15 +111,15 @@ func serStringToBool(v string) (bool, error) { return false, errors.New(fmt.Sprintf("invalid boolean value '%s'", v)) } -func serObjectIDToString(v primitive.ObjectID) (string, error) { +func serObjectIDToString(v bson.ObjectID) (string, error) { return v.Hex(), nil } -func serStringToObjectID(v string) (primitive.ObjectID, error) { - if rv, err := primitive.ObjectIDFromHex(v); err == nil { +func serStringToObjectID(v string) (bson.ObjectID, error) { + if rv, err := bson.ObjectIDFromHex(v); err == nil { return rv, nil } else { - return primitive.ObjectID{}, err + return bson.ObjectID{}, err } } diff --git a/rfctime/date.go b/rfctime/date.go index 5e35ea3..98a2e69 100644 --- a/rfctime/date.go +++ b/rfctime/date.go @@ -4,11 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" - "reflect" + "go.mongodb.org/mongo-driver/v2/bson" "strconv" "strings" "time" @@ -83,8 +79,8 @@ func (t *Date) UnmarshalText(data []byte) error { return t.ParseString(string(data)) } -func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bsontype.Null { +func (t *Date) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -92,12 +88,12 @@ func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { *t = Date{} return nil } - if bt != bsontype.String { - return errors.New(fmt.Sprintf("cannot unmarshal %v into Date", bt)) + if bson.Type(bt) != bson.TypeString { + return errors.New(fmt.Sprintf("cannot unmarshal %v into Date", bson.Type(bt))) } var tt string - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) if err != nil { return err } @@ -120,43 +116,16 @@ func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { return nil } -func (t Date) MarshalBSONValue() (bsontype.Type, []byte, error) { +func (t Date) MarshalBSONValue() (byte, []byte, error) { + var tp bson.Type + var data []byte + var err error if t.IsZero() { - return bson.MarshalValue("") - } - return bson.MarshalValue(t.String()) -} - -func (t Date) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = t.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&t)) + tp, data, err = bson.MarshalValue("") } else { - val.Set(reflect.ValueOf(t)) + tp, data, err = bson.MarshalValue(t.String()) } - - return nil + return byte(tp), data, err } func (t Date) Serialize() string { diff --git a/rfctime/rfc3339.go b/rfctime/rfc3339.go index 8137625..ac5681e 100644 --- a/rfctime/rfc3339.go +++ b/rfctime/rfc3339.go @@ -4,13 +4,9 @@ import ( "encoding/json" "errors" "fmt" - "reflect" "time" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" + "go.mongodb.org/mongo-driver/v2/bson" ) type RFC3339Time time.Time @@ -69,8 +65,8 @@ func (t *RFC3339Time) UnmarshalText(data []byte) error { return nil } -func (t *RFC3339Time) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bson.TypeNull { +func (t *RFC3339Time) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -78,11 +74,11 @@ func (t *RFC3339Time) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { *t = RFC3339Time{} return nil } - if bt != bson.TypeDateTime { - return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339Time", bt)) + if bson.Type(bt) != bson.TypeDateTime { + return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339Time", bson.Type(bt))) } var tt time.Time - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) if err != nil { return err } @@ -90,40 +86,9 @@ func (t *RFC3339Time) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { return nil } -func (t RFC3339Time) MarshalBSONValue() (bsontype.Type, []byte, error) { - return bson.MarshalValue(time.Time(t)) -} - -func (t RFC3339Time) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = t.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&t)) - } else { - val.Set(reflect.ValueOf(t)) - } - - return nil +func (t RFC3339Time) MarshalBSONValue() (byte, []byte, error) { + tp, data, err := bson.MarshalValue(time.Time(t)) + return byte(tp), data, err } func (t RFC3339Time) Serialize() string { diff --git a/rfctime/rfc3339Nano.go b/rfctime/rfc3339Nano.go index 75e2e89..db9d9ed 100644 --- a/rfctime/rfc3339Nano.go +++ b/rfctime/rfc3339Nano.go @@ -4,11 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" - "reflect" + "go.mongodb.org/mongo-driver/v2/bson" "time" ) @@ -68,8 +64,8 @@ func (t *RFC3339NanoTime) UnmarshalText(data []byte) error { return nil } -func (t *RFC3339NanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bson.TypeNull { +func (t *RFC3339NanoTime) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -77,11 +73,11 @@ func (t *RFC3339NanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) erro *t = RFC3339NanoTime{} return nil } - if bt != bson.TypeDateTime { - return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339NanoTime", bt)) + if bson.Type(bt) != bson.TypeDateTime { + return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339NanoTime", bson.Type(bt))) } var tt time.Time - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) if err != nil { return err } @@ -89,40 +85,9 @@ func (t *RFC3339NanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) erro return nil } -func (t RFC3339NanoTime) MarshalBSONValue() (bsontype.Type, []byte, error) { - return bson.MarshalValue(time.Time(t)) -} - -func (t RFC3339NanoTime) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = t.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&t)) - } else { - val.Set(reflect.ValueOf(t)) - } - - return nil +func (t RFC3339NanoTime) MarshalBSONValue() (byte, []byte, error) { + tp, data, err := bson.MarshalValue(time.Time(t)) + return byte(tp), data, err } func (t RFC3339NanoTime) Serialize() string { diff --git a/rfctime/seconds.go b/rfctime/seconds.go index 7048a8a..6386fcb 100644 --- a/rfctime/seconds.go +++ b/rfctime/seconds.go @@ -5,11 +5,7 @@ import ( "errors" "fmt" "git.blackforestbytes.com/BlackForestBytes/goext/timeext" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" - "reflect" + "go.mongodb.org/mongo-driver/v2/bson" "time" ) @@ -61,8 +57,8 @@ func (d SecondsF64) MarshalJSON() ([]byte, error) { return json.Marshal(secs) } -func (d *SecondsF64) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bson.TypeNull { +func (d *SecondsF64) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -70,11 +66,11 @@ func (d *SecondsF64) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { *d = SecondsF64(0) return nil } - if bt != bson.TypeDouble { - return errors.New(fmt.Sprintf("cannot unmarshal %v into SecondsF64", bt)) + if bson.Type(bt) != bson.TypeDouble { + return errors.New(fmt.Sprintf("cannot unmarshal %v into SecondsF64", bson.Type(bt))) } var secValue float64 - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&secValue) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&secValue) if err != nil { return err } @@ -82,40 +78,9 @@ func (d *SecondsF64) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { return nil } -func (d SecondsF64) MarshalBSONValue() (bsontype.Type, []byte, error) { - return bson.MarshalValue(d.Seconds()) -} - -func (d SecondsF64) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = d.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&d)) - } else { - val.Set(reflect.ValueOf(d)) - } - - return nil +func (d SecondsF64) MarshalBSONValue() (byte, []byte, error) { + tp, data, err := bson.MarshalValue(d.Seconds()) + return byte(tp), data, err } func NewSecondsF64(t time.Duration) SecondsF64 { diff --git a/rfctime/unix.go b/rfctime/unix.go index 87a39e1..04ea110 100644 --- a/rfctime/unix.go +++ b/rfctime/unix.go @@ -4,11 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" - "reflect" + "go.mongodb.org/mongo-driver/v2/bson" "strconv" "time" ) @@ -66,8 +62,8 @@ func (t *UnixTime) UnmarshalText(data []byte) error { return nil } -func (t *UnixTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bson.TypeNull { +func (t *UnixTime) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -75,11 +71,11 @@ func (t *UnixTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { *t = UnixTime{} return nil } - if bt != bson.TypeDateTime { - return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixTime", bt)) + if bson.Type(bt) != bson.TypeDateTime { + return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixTime", bson.Type(bt))) } var tt time.Time - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) if err != nil { return err } @@ -87,40 +83,9 @@ func (t *UnixTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { return nil } -func (t UnixTime) MarshalBSONValue() (bsontype.Type, []byte, error) { - return bson.MarshalValue(time.Time(t)) -} - -func (t UnixTime) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = t.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&t)) - } else { - val.Set(reflect.ValueOf(t)) - } - - return nil +func (t UnixTime) MarshalBSONValue() (byte, []byte, error) { + tp, data, err := bson.MarshalValue(time.Time(t)) + return byte(tp), data, err } func (t UnixTime) Serialize() string { diff --git a/rfctime/unixMilli.go b/rfctime/unixMilli.go index d496103..b2695f3 100644 --- a/rfctime/unixMilli.go +++ b/rfctime/unixMilli.go @@ -4,11 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" - "reflect" + "go.mongodb.org/mongo-driver/v2/bson" "strconv" "time" ) @@ -66,8 +62,8 @@ func (t *UnixMilliTime) UnmarshalText(data []byte) error { return nil } -func (t *UnixMilliTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bson.TypeNull { +func (t *UnixMilliTime) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -75,11 +71,11 @@ func (t *UnixMilliTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error *t = UnixMilliTime{} return nil } - if bt != bson.TypeDateTime { - return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixMilliTime", bt)) + if bson.Type(bt) != bson.TypeDateTime { + return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixMilliTime", bson.Type(bt))) } var tt time.Time - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) if err != nil { return err } @@ -87,40 +83,9 @@ func (t *UnixMilliTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error return nil } -func (t UnixMilliTime) MarshalBSONValue() (bsontype.Type, []byte, error) { - return bson.MarshalValue(time.Time(t)) -} - -func (t UnixMilliTime) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = t.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&t)) - } else { - val.Set(reflect.ValueOf(t)) - } - - return nil +func (t UnixMilliTime) MarshalBSONValue() (byte, []byte, error) { + tp, data, err := bson.MarshalValue(time.Time(t)) + return byte(tp), data, err } func (t UnixMilliTime) Serialize() string { diff --git a/rfctime/unixNano.go b/rfctime/unixNano.go index 1ad945d..ff97f59 100644 --- a/rfctime/unixNano.go +++ b/rfctime/unixNano.go @@ -4,11 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/bsoncodec" - "go.mongodb.org/mongo-driver/bson/bsonrw" - "go.mongodb.org/mongo-driver/bson/bsontype" - "reflect" + "go.mongodb.org/mongo-driver/v2/bson" "strconv" "time" ) @@ -66,8 +62,8 @@ func (t *UnixNanoTime) UnmarshalText(data []byte) error { return nil } -func (t *UnixNanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { - if bt == bson.TypeNull { +func (t *UnixNanoTime) UnmarshalBSONValue(bt byte, data []byte) error { + if bson.Type(bt) == bson.TypeNull { // we can't set nil in UnmarshalBSONValue (so we use default(struct)) // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // https://stackoverflow.com/questions/75167597 @@ -75,11 +71,11 @@ func (t *UnixNanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { *t = UnixNanoTime{} return nil } - if bt != bson.TypeDateTime { - return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixNanoTime", bt)) + if bson.Type(bt) != bson.TypeDateTime { + return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixNanoTime", bson.Type(bt))) } var tt time.Time - err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt) + err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) if err != nil { return err } @@ -87,40 +83,9 @@ func (t *UnixNanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { return nil } -func (t UnixNanoTime) MarshalBSONValue() (bsontype.Type, []byte, error) { - return bson.MarshalValue(time.Time(t)) -} - -func (t UnixNanoTime) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { - if val.Kind() == reflect.Pointer && val.IsNil() { - if !val.CanSet() { - return errors.New("ValueUnmarshalerDecodeValue") - } - val.Set(reflect.New(val.Type().Elem())) - } - - tp, src, err := bsonrw.Copier{}.CopyValueToBytes(vr) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer && len(src) == 0 { - val.Set(reflect.Zero(val.Type())) - return nil - } - - err = t.UnmarshalBSONValue(tp, src) - if err != nil { - return err - } - - if val.Kind() == reflect.Pointer { - val.Set(reflect.ValueOf(&t)) - } else { - val.Set(reflect.ValueOf(t)) - } - - return nil +func (t UnixNanoTime) MarshalBSONValue() (byte, []byte, error) { + tp, data, err := bson.MarshalValue(time.Time(t)) + return byte(tp), data, err } func (t UnixNanoTime) Serialize() string { diff --git a/wmo/collection.go b/wmo/collection.go index 6fb6fd2..849b75a 100644 --- a/wmo/collection.go +++ b/wmo/collection.go @@ -5,13 +5,12 @@ import ( ct "git.blackforestbytes.com/BlackForestBytes/goext/cursortoken" "git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/langext" - "go.mongodb.org/mongo-driver/bson/bsontype" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/mongo" "reflect" ) type EntityID interface { - MarshalBSONValue() (bsontype.Type, []byte, error) + MarshalBSONValue() (byte, []byte, error) String() string } diff --git a/wmo/decoding.go b/wmo/decoding.go index d2357ff..5bec8d8 100644 --- a/wmo/decoding.go +++ b/wmo/decoding.go @@ -2,8 +2,8 @@ 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) { diff --git a/wmo/mongo.go b/wmo/mongo.go index 67c3cd8..1494a9b 100644 --- a/wmo/mongo.go +++ b/wmo/mongo.go @@ -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} diff --git a/wmo/queryAggregate.go b/wmo/queryAggregate.go index d82e97c..ef3df4b 100644 --- a/wmo/queryAggregate.go +++ b/wmo/queryAggregate.go @@ -2,13 +2,13 @@ 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 +29,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 +53,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)) diff --git a/wmo/queryDelete.go b/wmo/queryDelete.go index ce7de70..e8bda3e 100644 --- a/wmo/queryDelete.go +++ b/wmo/queryDelete.go @@ -2,9 +2,9 @@ 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 { diff --git a/wmo/queryFind.go b/wmo/queryFind.go index f2046c9..59ac3f7 100644 --- a/wmo/queryFind.go +++ b/wmo/queryFind.go @@ -2,11 +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" "git.blackforestbytes.com/BlackForestBytes/goext/langext" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "iter" ) @@ -51,13 +51,15 @@ func (c *Coll[TData]) createFindQuery(ctx context.Context, filter bson.M, opts . } } - convOpts := make([]*options.AggregateOptions, 0, len(opts)) + convOpts := make([]options.Lister[options.AggregateOptions], 0, len(opts)) for _, v := range opts { vConv, err := convertFindOpt(v) if err != nil { return nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build() } - convOpts = append(convOpts, vConv) + if vConv != nil { + convOpts = append(convOpts, vConv) + } } cursor, err := c.coll.Aggregate(ctx, pipeline, convOpts...) @@ -137,7 +139,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 +159,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() @@ -165,33 +167,18 @@ func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) { if v.Hint != nil { r.SetHint(v.Hint) } - if v.Max != nil { - return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'Max' (cannot convert to AggregateOptions)").Build() - } 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() - } if v.NoCursorTimeout != nil { return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'NoCursorTimeout' (cannot convert to AggregateOptions)").Build() } - if v.OplogReplay != nil { - return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'OplogReplay' (cannot convert to AggregateOptions)").Build() - } if v.ReturnKey != nil { return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'ReturnKey' (cannot convert to AggregateOptions)").Build() } 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) } diff --git a/wmo/queryFindOne.go b/wmo/queryFindOne.go index 337d216..a8a0bf8 100644 --- a/wmo/queryFindOne.go +++ b/wmo/queryFindOne.go @@ -3,10 +3,10 @@ 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) { diff --git a/wmo/queryInsert.go b/wmo/queryInsert.go index 28d007a..196e9f1 100644 --- a/wmo/queryInsert.go +++ b/wmo/queryInsert.go @@ -4,8 +4,8 @@ import ( "context" "git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/langext" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" + "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) { diff --git a/wmo/queryList.go b/wmo/queryList.go index 6a07b6c..e0a8b77 100644 --- a/wmo/queryList.go +++ b/wmo/queryList.go @@ -2,11 +2,11 @@ package wmo import ( "context" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" ct "git.blackforestbytes.com/BlackForestBytes/goext/cursortoken" "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" "iter" ) diff --git a/wmo/queryPaginate.go b/wmo/queryPaginate.go index ac0f552..1f559d0 100644 --- a/wmo/queryPaginate.go +++ b/wmo/queryPaginate.go @@ -2,11 +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" pag "git.blackforestbytes.com/BlackForestBytes/goext/pagination" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" "iter" ) diff --git a/wmo/queryUpdate.go b/wmo/queryUpdate.go index c757633..8264657 100644 --- a/wmo/queryUpdate.go +++ b/wmo/queryUpdate.go @@ -2,10 +2,10 @@ 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) { diff --git a/wmo/reflection_test.go b/wmo/reflection_test.go index f516caf..ea62282 100644 --- a/wmo/reflection_test.go +++ b/wmo/reflection_test.go @@ -9,8 +9,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) { @@ -234,8 +234,8 @@ 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 any diff --git a/wpdf/wpdfTable.go b/wpdf/wpdfTable.go index a1ccfb1..f78a2b5 100644 --- a/wpdf/wpdfTable.go +++ b/wpdf/wpdfTable.go @@ -1,11 +1,12 @@ package wpdf import ( + "regexp" + "strconv" + "git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/rext" - "regexp" - "strconv" ) // Column specifier: @@ -298,7 +299,7 @@ func (b *TableBuilder) calculateColumns() []float64 { if remainingWidth > 0.01 { rmSub := 0.0 - for i, _ := range columnDef { + for i := range columnDef { if frColumnWeights[i] != 0 { addW := (remainingWidth / float64(frColumnWidthCount)) * frColumnWeights[i] rmSub += addW