Compare commits

..

7 Commits

Author SHA1 Message Date
Mikescher dad0e3240d v0.0.636 Remove remaining traces of v1 mongo driver
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m36s
2026-04-26 14:53:24 +02:00
Mikescher a5cece2bc7 v0.0.635 mongo driver v2 (Viktor)
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m41s
2026-04-26 14:47:05 +02:00
Mikescher cdce955887 Merge remote-tracking branch 'origin/feature/mongo-driver-v2'
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s
2026-04-26 14:42:53 +02:00
Mikescher 90862ed3f5 Merge branch 'feature/upgrade-go'
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m35s
2026-04-26 14:31:48 +02:00
Mikescher 80cea13437 v0.0.634 add ReplyTo to googleapi.SendMail
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m37s
2026-04-24 13:38:00 +02:00
viktor f9576a2fec go mod tidy
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m39s
2026-04-21 18:45:52 +02:00
viktor 26d542c9a2 added mongo-driver v2
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m33s
2026-04-21 18:41:32 +02:00
43 changed files with 249 additions and 607 deletions
+9 -10
View File
@@ -2,9 +2,7 @@
package {{.PkgName}} package {{.PkgName}}
import "go.mongodb.org/mongo-driver/bson" import "go.mongodb.org/mongo-driver/v2/bson"
import "go.mongodb.org/mongo-driver/bson/bsontype"
import "go.mongodb.org/mongo-driver/bson/primitive"
import "git.blackforestbytes.com/BlackForestBytes/goext/exerr" import "git.blackforestbytes.com/BlackForestBytes/goext/exerr"
const ChecksumIDGenerator = "{{.Checksum}}" // GoExtVersion: {{.GoextVersion}} const ChecksumIDGenerator = "{{.Checksum}}" // GoExtVersion: {{.GoextVersion}}
@@ -13,9 +11,10 @@ const ChecksumIDGenerator = "{{.Checksum}}" // GoExtVersion: {{.GoextVersion}}
// ================================ {{.Name}} ({{.FileRelative}}) ================================ // ================================ {{.Name}} ({{.FileRelative}}) ================================
func (i {{.Name}}) MarshalBSONValue() (bsontype.Type, []byte, error) { func (i {{.Name}}) MarshalBSONValue() (byte, []byte, error) {
if objId, err := primitive.ObjectIDFromHex(string(i)); err == nil { if objId, err := bson.ObjectIDFromHex(string(i)); err == nil {
return bson.MarshalValue(objId) tp, data, err := bson.MarshalValue(objId)
return byte(tp), data, err
} else { } else {
return 0, nil, exerr.New(exerr.TypeMarshalEntityID, "Failed to marshal {{.Name}}("+i.String()+") to ObjectId").Str("value", string(i)).Type("type", i).Build() return 0, nil, exerr.New(exerr.TypeMarshalEntityID, "Failed to marshal {{.Name}}("+i.String()+") to ObjectId").Str("value", string(i)).Type("type", i).Build()
} }
@@ -25,12 +24,12 @@ func (i {{.Name}}) String() string {
return string(i) return string(i)
} }
func (i {{.Name}}) ObjID() (primitive.ObjectID, error) { func (i {{.Name}}) ObjID() (bson.ObjectID, error) {
return primitive.ObjectIDFromHex(string(i)) return bson.ObjectIDFromHex(string(i))
} }
func (i {{.Name}}) Valid() bool { func (i {{.Name}}) Valid() bool {
_, err := primitive.ObjectIDFromHex(string(i)) _, err := bson.ObjectIDFromHex(string(i))
return err == nil return err == nil
} }
@@ -50,7 +49,7 @@ func (i {{.Name}}) IsZero() bool {
} }
func New{{.Name}}() {{.Name}} { func New{{.Name}}() {{.Name}} {
return {{.Name}}(primitive.NewObjectID().Hex()) return {{.Name}}(bson.NewObjectID().Hex())
} }
{{end}} {{end}}
+2 -1
View File
@@ -2,7 +2,8 @@ package cursortoken
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/v2/mongo"
) )
type RawFilter interface { type RawFilter interface {
+8 -7
View File
@@ -3,8 +3,9 @@ package cursortoken
import ( import (
"encoding/base32" "encoding/base32"
"encoding/json" "encoding/json"
"go.mongodb.org/mongo-driver/bson/primitive"
"time" "time"
"go.mongodb.org/mongo-driver/v2/bson"
) )
type CTKeySort struct { type CTKeySort struct {
@@ -119,18 +120,18 @@ func (c CTKeySort) IsStart() bool {
return c.Mode == CTMStart return c.Mode == CTMStart
} }
func (c CTKeySort) valuePrimaryObjectId() (primitive.ObjectID, bool) { func (c CTKeySort) valuePrimaryObjectId() (bson.ObjectID, bool) {
if oid, err := primitive.ObjectIDFromHex(c.ValuePrimary); err == nil { if oid, err := bson.ObjectIDFromHex(c.ValuePrimary); err == nil {
return oid, true return oid, true
} else { } else {
return primitive.ObjectID{}, false return bson.ObjectID{}, false
} }
} }
func (c CTKeySort) valueSecondaryObjectId() (primitive.ObjectID, bool) { func (c CTKeySort) valueSecondaryObjectId() (bson.ObjectID, bool) {
if oid, err := primitive.ObjectIDFromHex(c.ValueSecondary); err == nil { if oid, err := bson.ObjectIDFromHex(c.ValueSecondary); err == nil {
return oid, true return oid, true
} else { } else {
return primitive.ObjectID{}, false return bson.ObjectID{}, false
} }
} }
+2 -2
View File
@@ -16,7 +16,7 @@ import (
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/langext"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog" "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) 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) return b.addMeta(key, MDTObjectID, val)
} }
+4 -3
View File
@@ -3,11 +3,12 @@ package exerr
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"go.mongodb.org/mongo-driver/bson/primitive"
"maps" "maps"
"reflect" "reflect"
"time" "time"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"go.mongodb.org/mongo-driver/v2/bson"
) )
var reflectTypeStr = reflect.TypeFor[string]() var reflectTypeStr = reflect.TypeFor[string]()
@@ -222,7 +223,7 @@ func getReflectedMetaValues(value any, remainingDepth int) map[string]MetaValue
return map[string]MetaValue{"": {DataType: MDTIntArray, Value: ifraw}} return map[string]MetaValue{"": {DataType: MDTIntArray, Value: ifraw}}
case []int32: case []int32:
return map[string]MetaValue{"": {DataType: MDTInt32Array, Value: ifraw}} return map[string]MetaValue{"": {DataType: MDTInt32Array, Value: ifraw}}
case primitive.ObjectID: case bson.ObjectID:
return map[string]MetaValue{"": {DataType: MDTObjectID, Value: ifraw}} return map[string]MetaValue{"": {DataType: MDTObjectID, Value: ifraw}}
case []string: case []string:
return map[string]MetaValue{"": {DataType: MDTStringArray, Value: ifraw}} return map[string]MetaValue{"": {DataType: MDTStringArray, Value: ifraw}}
+10 -44
View File
@@ -4,11 +4,8 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
"reflect"
) )
type ErrorCategory struct{ Category string } type ErrorCategory struct{ Category string }
@@ -28,8 +25,8 @@ func (e ErrorCategory) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Category) return json.Marshal(e.Category)
} }
func (e *ErrorCategory) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (e *ErrorCategory) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bson.TypeNull { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -37,11 +34,11 @@ func (e *ErrorCategory) UnmarshalBSONValue(bt bsontype.Type, data []byte) error
*e = ErrorCategory{} *e = ErrorCategory{}
return nil return nil
} }
if bt != bson.TypeString { if bson.Type(bt) != bson.TypeString {
return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bson.Type(bt)))
} }
var tt string 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 { if err != nil {
return err return err
} }
@@ -49,40 +46,9 @@ func (e *ErrorCategory) UnmarshalBSONValue(bt bsontype.Type, data []byte) error
return nil return nil
} }
func (e ErrorCategory) MarshalBSONValue() (bsontype.Type, []byte, error) { func (e ErrorCategory) MarshalBSONValue() (byte, []byte, error) {
return bson.MarshalValue(e.Category) tp, data, err := bson.MarshalValue(e.Category)
} return byte(tp), data, err
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
} }
//goland:noinspection GoUnusedGlobalVariable //goland:noinspection GoUnusedGlobalVariable
+10 -44
View File
@@ -4,11 +4,8 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
"reflect"
) )
type ErrorSeverity struct{ Severity string } type ErrorSeverity struct{ Severity string }
@@ -30,8 +27,8 @@ func (e ErrorSeverity) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Severity) return json.Marshal(e.Severity)
} }
func (e *ErrorSeverity) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (e *ErrorSeverity) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bson.TypeNull { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -39,11 +36,11 @@ func (e *ErrorSeverity) UnmarshalBSONValue(bt bsontype.Type, data []byte) error
*e = ErrorSeverity{} *e = ErrorSeverity{}
return nil return nil
} }
if bt != bson.TypeString { if bson.Type(bt) != bson.TypeString {
return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bson.Type(bt)))
} }
var tt string 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 { if err != nil {
return err return err
} }
@@ -51,40 +48,9 @@ func (e *ErrorSeverity) UnmarshalBSONValue(bt bsontype.Type, data []byte) error
return nil return nil
} }
func (e ErrorSeverity) MarshalBSONValue() (bsontype.Type, []byte, error) { func (e ErrorSeverity) MarshalBSONValue() (byte, []byte, error) {
return bson.MarshalValue(e.Severity) tp, data, err := bson.MarshalValue(e.Severity)
} return byte(tp), data, err
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
} }
//goland:noinspection GoUnusedGlobalVariable //goland:noinspection GoUnusedGlobalVariable
+9 -44
View File
@@ -4,13 +4,9 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"reflect"
"git.blackforestbytes.com/BlackForestBytes/goext/dataext" "git.blackforestbytes.com/BlackForestBytes/goext/dataext"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
) )
type ErrorType struct { type ErrorType struct {
@@ -80,8 +76,8 @@ func (e ErrorType) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Key) return json.Marshal(e.Key)
} }
func (e *ErrorType) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (e *ErrorType) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bson.TypeNull { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -89,11 +85,11 @@ func (e *ErrorType) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
*e = ErrorType{} *e = ErrorType{}
return nil return nil
} }
if bt != bson.TypeString { if bson.Type(bt) != bson.TypeString {
return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bson.Type(bt)))
} }
var tt string 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 { if err != nil {
return err return err
} }
@@ -107,40 +103,9 @@ func (e *ErrorType) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
} }
} }
func (e ErrorType) MarshalBSONValue() (bsontype.Type, []byte, error) { func (e ErrorType) MarshalBSONValue() (byte, []byte, error) {
return bson.MarshalValue(e.Key) tp, data, err := bson.MarshalValue(e.Key)
} return byte(tp), data, err
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
} }
var registeredTypes = dataext.SyncMap[string, ErrorType]{} var registeredTypes = dataext.SyncMap[string, ErrorType]{}
+16 -16
View File
@@ -3,12 +3,12 @@ package exerr
import ( import (
"context" "context"
"encoding/json" "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"
"testing" "testing"
"time" "time"
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
) )
func TestJSONMarshalErrorCategory(t *testing.T) { func TestJSONMarshalErrorCategory(t *testing.T) {
@@ -57,7 +57,7 @@ func TestBSONMarshalErrorCategory(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx) client, err := mongo.Connect()
if err != nil { if err != nil {
t.Skip("Skip test - no local mongo found") t.Skip("Skip test - no local mongo found")
return return
@@ -68,7 +68,7 @@ func TestBSONMarshalErrorCategory(t *testing.T) {
return return
} }
primimd := primitive.NewObjectID() primimd := bson.NewObjectID()
_, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": CatSystem}) _, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": CatSystem})
tst.AssertNoErr(t, err) tst.AssertNoErr(t, err)
@@ -76,8 +76,8 @@ func TestBSONMarshalErrorCategory(t *testing.T) {
cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}}) cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}})
var c1 struct { var c1 struct {
ID primitive.ObjectID `bson:"_id"` ID bson.ObjectID `bson:"_id"`
Val ErrorCategory `bson:"val"` Val ErrorCategory `bson:"val"`
} }
err = cursor.Decode(&c1) err = cursor.Decode(&c1)
@@ -90,7 +90,7 @@ func TestBSONMarshalErrorSeverity(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx) client, err := mongo.Connect()
if err != nil { if err != nil {
t.Skip("Skip test - no local mongo found") t.Skip("Skip test - no local mongo found")
return return
@@ -101,7 +101,7 @@ func TestBSONMarshalErrorSeverity(t *testing.T) {
return return
} }
primimd := primitive.NewObjectID() primimd := bson.NewObjectID()
_, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": SevErr}) _, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": SevErr})
tst.AssertNoErr(t, err) tst.AssertNoErr(t, err)
@@ -109,8 +109,8 @@ func TestBSONMarshalErrorSeverity(t *testing.T) {
cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}}) cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}})
var c1 struct { var c1 struct {
ID primitive.ObjectID `bson:"_id"` ID bson.ObjectID `bson:"_id"`
Val ErrorSeverity `bson:"val"` Val ErrorSeverity `bson:"val"`
} }
err = cursor.Decode(&c1) err = cursor.Decode(&c1)
@@ -123,7 +123,7 @@ func TestBSONMarshalErrorType(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond)
defer cancel() defer cancel()
client, err := mongo.Connect(ctx) client, err := mongo.Connect()
if err != nil { if err != nil {
t.Skip("Skip test - no local mongo found") t.Skip("Skip test - no local mongo found")
return return
@@ -134,7 +134,7 @@ func TestBSONMarshalErrorType(t *testing.T) {
return return
} }
primimd := primitive.NewObjectID() primimd := bson.NewObjectID()
_, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": TypeNotImplemented}) _, err = client.Database("_test").Collection("goext-cicd").InsertOne(ctx, bson.M{"_id": primimd, "val": TypeNotImplemented})
tst.AssertNoErr(t, err) tst.AssertNoErr(t, err)
@@ -142,8 +142,8 @@ func TestBSONMarshalErrorType(t *testing.T) {
cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}}) cursor := client.Database("_test").Collection("goext-cicd").FindOne(ctx, bson.M{"_id": primimd, "val": bson.M{"$type": "string"}})
var c1 struct { var c1 struct {
ID primitive.ObjectID `bson:"_id"` ID bson.ObjectID `bson:"_id"`
Val ErrorType `bson:"val"` Val ErrorType `bson:"val"`
} }
err = cursor.Decode(&c1) err = cursor.Decode(&c1)
+12 -12
View File
@@ -5,14 +5,14 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"math" "math"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/v2/bson"
) )
// This is a buffed up map[string]any // This is a buffed up map[string]any
@@ -99,7 +99,7 @@ func (v MetaValue) SerializeValue() (string, error) {
case MDTBytes: case MDTBytes:
return hex.EncodeToString(v.Value.([]byte)), nil return hex.EncodeToString(v.Value.([]byte)), nil
case MDTObjectID: case MDTObjectID:
return v.Value.(primitive.ObjectID).Hex(), nil return v.Value.(bson.ObjectID).Hex(), nil
case MDTTime: case MDTTime:
return strconv.FormatInt(v.Value.(time.Time).Unix(), 10) + "|" + strconv.FormatInt(int64(v.Value.(time.Time).Nanosecond()), 10), nil return strconv.FormatInt(v.Value.(time.Time).Unix(), 10) + "|" + strconv.FormatInt(int64(v.Value.(time.Time).Nanosecond()), 10), nil
case MDTDuration: case MDTDuration:
@@ -178,7 +178,7 @@ func (v MetaValue) ShortString(lim int) string {
case MDTBytes: case MDTBytes:
return langext.StrLimit(hex.EncodeToString(v.Value.([]byte)), lim, "...") return langext.StrLimit(hex.EncodeToString(v.Value.([]byte)), lim, "...")
case MDTObjectID: case MDTObjectID:
return v.Value.(primitive.ObjectID).Hex() return v.Value.(bson.ObjectID).Hex()
case MDTTime: case MDTTime:
return v.Value.(time.Time).Format(time.RFC3339) return v.Value.(time.Time).Format(time.RFC3339)
case MDTDuration: case MDTDuration:
@@ -266,7 +266,7 @@ func (v MetaValue) Apply(key string, evt *zerolog.Event, limitLen *int) *zerolog
case MDTBytes: case MDTBytes:
return evt.Bytes(key, v.Value.([]byte)) return evt.Bytes(key, v.Value.([]byte))
case MDTObjectID: case MDTObjectID:
return evt.Str(key, v.Value.(primitive.ObjectID).Hex()) return evt.Str(key, v.Value.(bson.ObjectID).Hex())
case MDTTime: case MDTTime:
return evt.Time(key, v.Value.(time.Time)) return evt.Time(key, v.Value.(time.Time))
case MDTDuration: case MDTDuration:
@@ -460,7 +460,7 @@ func (v *MetaValue) Deserialize(value string, datatype metaDataType) error {
v.DataType = datatype v.DataType = datatype
return nil return nil
case MDTObjectID: case MDTObjectID:
r, err := primitive.ObjectIDFromHex(value) r, err := bson.ObjectIDFromHex(value)
if err != nil { if err != nil {
return err return err
} }
@@ -577,7 +577,7 @@ func (v MetaValue) ValueString() string {
case MDTBytes: case MDTBytes:
return hex.EncodeToString(v.Value.([]byte)) return hex.EncodeToString(v.Value.([]byte))
case MDTObjectID: case MDTObjectID:
return v.Value.(primitive.ObjectID).Hex() return v.Value.(bson.ObjectID).Hex()
case MDTTime: case MDTTime:
return v.Value.(time.Time).Format(time.RFC3339Nano) return v.Value.(time.Time).Format(time.RFC3339Nano)
case MDTDuration: case MDTDuration:
@@ -628,8 +628,8 @@ func (v MetaValue) rawValueForJson() any {
if v.Value.(AnyWrap).IsError { if v.Value.(AnyWrap).IsError {
return bson.M{"@error": true} return bson.M{"@error": true}
} }
jsonobj := primitive.M{} jsonobj := bson.M{}
jsonarr := primitive.A{} jsonarr := bson.A{}
if err := json.Unmarshal([]byte(v.Value.(AnyWrap).Json), &jsonobj); err == nil { if err := json.Unmarshal([]byte(v.Value.(AnyWrap).Json), &jsonobj); err == nil {
return jsonobj return jsonobj
} else if err := json.Unmarshal([]byte(v.Value.(AnyWrap).Json), &jsonarr); err == nil { } else if err := json.Unmarshal([]byte(v.Value.(AnyWrap).Json), &jsonarr); err == nil {
@@ -654,7 +654,7 @@ func (v MetaValue) rawValueForJson() any {
return v.Value.(time.Time).Format(time.RFC3339Nano) return v.Value.(time.Time).Format(time.RFC3339Nano)
} }
if v.DataType == MDTObjectID { if v.DataType == MDTObjectID {
return v.Value.(primitive.ObjectID).Hex() return v.Value.(bson.ObjectID).Hex()
} }
if v.DataType == MDTNil { if v.DataType == MDTNil {
return nil return nil
+1 -4
View File
@@ -8,7 +8,7 @@ require (
github.com/jmoiron/sqlx v1.4.0 github.com/jmoiron/sqlx v1.4.0
github.com/rs/xid v1.6.0 github.com/rs/xid v1.6.0
github.com/rs/zerolog v1.35.1 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/crypto v0.50.0
golang.org/x/sys v0.43.0 golang.org/x/sys v0.43.0
golang.org/x/term v0.42.0 golang.org/x/term v0.42.0
@@ -36,7 +36,6 @@ require (
github.com/go-playground/validator/v10 v10.30.2 // indirect github.com/go-playground/validator/v10 v10.30.2 // indirect
github.com/goccy/go-json v0.10.6 // indirect github.com/goccy/go-json v0.10.6 // indirect
github.com/goccy/go-yaml v1.19.2 // indirect github.com/goccy/go-yaml v1.19.2 // indirect
github.com/golang/snappy v1.0.0 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.18.5 // indirect github.com/klauspost/compress v1.18.5 // indirect
@@ -47,7 +46,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/montanaflynn/stats v0.9.0 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.3.0 // indirect github.com/pelletier/go-toml/v2 v2.3.0 // indirect
github.com/quic-go/qpack v0.6.0 // indirect github.com/quic-go/qpack v0.6.0 // indirect
@@ -64,7 +62,6 @@ require (
github.com/xuri/efp v0.0.1 // indirect github.com/xuri/efp v0.0.1 // indirect
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // 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/arch v0.26.0 // indirect
golang.org/x/image v0.39.0 // indirect golang.org/x/image v0.39.0 // indirect
golang.org/x/net v0.53.0 // indirect golang.org/x/net v0.53.0 // indirect
-6
View File
@@ -40,8 +40,6 @@ github.com/goccy/go-json v0.10.6 h1:p8HrPJzOakx/mn/bQtjgNjdTcN+/S6FcG2CTtQOrHVU=
github.com/goccy/go-json v0.10.6/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/goccy/go-json v0.10.6/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs=
github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -81,8 +79,6 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw= github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8= github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/montanaflynn/stats v0.9.0 h1:tsBJ0RXwph9BmAuFoCmqGv6e8xa0MENQ8m0ptKq29mQ=
github.com/montanaflynn/stats v0.9.0/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w= github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pelletier/go-toml/v2 v2.3.0 h1:k59bC/lIZREW0/iVaQR8nDHxVq8OVlIzYCOJf421CaM= github.com/pelletier/go-toml/v2 v2.3.0 h1:k59bC/lIZREW0/iVaQR8nDHxVq8OVlIzYCOJf421CaM=
@@ -140,8 +136,6 @@ github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9/go.mod h1:WwHg+CVyzlv/T
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.17.9 h1:IexDdCuuNJ3BHrELgBlyaH9p60JXAvdzWR128q+U5tU=
go.mongodb.org/mongo-driver v1.17.9/go.mod h1:LlOhpH5NUEfhxcAwG0UEkMqwYcc4JU18gtCdGudk/tQ=
go.mongodb.org/mongo-driver/v2 v2.5.1 h1:j2U/Qp+wvueSpqitLCSZPT/+ZpVc1xzuwdHWwl7d8ro= go.mongodb.org/mongo-driver/v2 v2.5.1 h1:j2U/Qp+wvueSpqitLCSZPT/+ZpVc1xzuwdHWwl7d8ro=
go.mongodb.org/mongo-driver/v2 v2.5.1/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= go.mongodb.org/mongo-driver/v2 v2.5.1/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
+2 -2
View File
@@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.633" const GoextVersion = "0.0.636"
const GoextVersionTimestamp = "2026-04-13T16:12:09+0200" const GoextVersionTimestamp = "2026-04-26T14:53:24+0200"
+4 -1
View File
@@ -8,7 +8,7 @@ import (
) )
// https://datatracker.ietf.org/doc/html/rfc2822 // https://datatracker.ietf.org/doc/html/rfc2822
func encodeMimeMail(from string, recipients []string, cc []string, bcc []string, subject string, body MailBody, attachments []MailAttachment) string { func encodeMimeMail(from string, recipients []string, cc []string, bcc []string, replyTo []string, subject string, body MailBody, attachments []MailAttachment) string {
data := make([]string, 0, 32) data := make([]string, 0, 32)
@@ -22,6 +22,9 @@ func encodeMimeMail(from string, recipients []string, cc []string, bcc []string,
if len(bcc) > 0 { if len(bcc) > 0 {
data = append(data, "Bcc: "+strings.Join(langext.ArrMap(bcc, func(v string) string { return mime.QEncoding.Encode("UTF-8", v) }), ", ")) data = append(data, "Bcc: "+strings.Join(langext.ArrMap(bcc, func(v string) string { return mime.QEncoding.Encode("UTF-8", v) }), ", "))
} }
if len(replyTo) > 0 {
data = append(data, "Reply-To: "+strings.Join(langext.ArrMap(replyTo, func(v string) string { return mime.QEncoding.Encode("UTF-8", v) }), ", "))
}
data = append(data, "Subject: "+mime.QEncoding.Encode("UTF-8", subject)) data = append(data, "Subject: "+mime.QEncoding.Encode("UTF-8", subject))
hasInlineAttachments := langext.ArrAny(attachments, func(v MailAttachment) bool { return v.IsInline }) hasInlineAttachments := langext.ArrAny(attachments, func(v MailAttachment) bool { return v.IsInline })
+4
View File
@@ -13,6 +13,7 @@ func TestEncodeMimeMail(t *testing.T) {
[]string{"trash@mikescher.de"}, []string{"trash@mikescher.de"},
nil, nil,
nil, nil,
nil,
"Hello Test Mail", "Hello Test Mail",
MailBody{Plain: "Plain Text"}, MailBody{Plain: "Plain Text"},
nil) nil)
@@ -27,6 +28,7 @@ func TestEncodeMimeMail2(t *testing.T) {
[]string{"trash@mikescher.de"}, []string{"trash@mikescher.de"},
nil, nil,
nil, nil,
nil,
"Hello Test Mail (alternative)", "Hello Test Mail (alternative)",
MailBody{ MailBody{
Plain: "Plain Text", Plain: "Plain Text",
@@ -44,6 +46,7 @@ func TestEncodeMimeMail3(t *testing.T) {
[]string{"trash@mikescher.de"}, []string{"trash@mikescher.de"},
nil, nil,
nil, nil,
nil,
"Hello Test Mail (alternative)", "Hello Test Mail (alternative)",
MailBody{ MailBody{
HTML: "<html><body><u>Non</u> Pl<i>ai</i>n T<b>ex</b>t</body></html>", HTML: "<html><body><u>Non</u> Pl<i>ai</i>n T<b>ex</b>t</body></html>",
@@ -64,6 +67,7 @@ func TestEncodeMimeMail4(t *testing.T) {
[]string{"trash@mikescher.de"}, []string{"trash@mikescher.de"},
nil, nil,
nil, nil,
nil,
"Hello Test Mail (inline)", "Hello Test Mail (inline)",
MailBody{ MailBody{
HTML: "<html><body><u>Non</u> Pl<i>ai</i>n T<b>ex</b>t</body></html>", HTML: "<html><body><u>Non</u> Pl<i>ai</i>n T<b>ex</b>t</body></html>",
+2 -2
View File
@@ -19,9 +19,9 @@ type MailRef struct {
LabelIDs []string `json:"labelIds"` LabelIDs []string `json:"labelIds"`
} }
func (c *client) SendMail(ctx context.Context, from string, recipients []string, cc []string, bcc []string, subject string, body MailBody, attachments []MailAttachment) (MailRef, error) { func (c *client) SendMail(ctx context.Context, from string, recipients []string, cc []string, bcc []string, replyTo []string, subject string, body MailBody, attachments []MailAttachment) (MailRef, error) {
mm := encodeMimeMail(from, recipients, cc, bcc, subject, body, attachments) mm := encodeMimeMail(from, recipients, cc, bcc, replyTo, subject, body, attachments)
tok, err := c.oauth.AccessToken() tok, err := c.oauth.AccessToken()
if err != nil { if err != nil {
+4
View File
@@ -36,6 +36,7 @@ func TestSendMail1(t *testing.T) {
[]string{"trash@mikescher.de"}, []string{"trash@mikescher.de"},
nil, nil,
nil, nil,
nil,
"Hello Test Mail", "Hello Test Mail",
MailBody{Plain: "Plain Text"}, MailBody{Plain: "Plain Text"},
nil) nil)
@@ -66,6 +67,7 @@ func TestSendMail2(t *testing.T) {
[]string{"trash@mikescher.de"}, []string{"trash@mikescher.de"},
nil, nil,
nil, nil,
nil,
"Hello Test Mail (alternative)", "Hello Test Mail (alternative)",
MailBody{ MailBody{
Plain: "Plain Text", Plain: "Plain Text",
@@ -99,6 +101,7 @@ func TestSendMail3(t *testing.T) {
[]string{"trash@mikescher.de"}, []string{"trash@mikescher.de"},
nil, nil,
nil, nil,
nil,
"Hello Test Mail (attach)", "Hello Test Mail (attach)",
MailBody{ MailBody{
HTML: "<html><body><u>Non</u> Pl<i>ai</i>n T<b>ex</b>t</body></html>", HTML: "<html><body><u>Non</u> Pl<i>ai</i>n T<b>ex</b>t</body></html>",
@@ -135,6 +138,7 @@ func TestSendMail4(t *testing.T) {
[]string{"trash@mikescher.de"}, []string{"trash@mikescher.de"},
nil, nil,
nil, nil,
nil,
"Hello Test Mail (inline)", "Hello Test Mail (inline)",
MailBody{ MailBody{
HTML: "<html><body><u>Non</u> Pl<i>ai</i>n T<b>ex</b>t</body></html>", HTML: "<html><body><u>Non</u> Pl<i>ai</i>n T<b>ex</b>t</body></html>",
+1 -1
View File
@@ -6,7 +6,7 @@ import (
) )
type GoogleClient interface { type GoogleClient interface {
SendMail(ctx context.Context, from string, recipients []string, cc []string, bcc []string, subject string, body MailBody, attachments []MailAttachment) (MailRef, error) SendMail(ctx context.Context, from string, recipients []string, cc []string, bcc []string, replyTo []string, subject string, body MailBody, attachments []MailAttachment) (MailRef, error)
} }
type client struct { type client struct {
+2 -2
View File
@@ -1,8 +1,8 @@
package mongoext package mongoext
import ( import (
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/v2/mongo"
) )
// FixTextSearchPipeline moves {$match:{$text:{$search}}} entries to the front of the pipeline (otherwise its an mongo error) // FixTextSearchPipeline moves {$match:{$text:{$search}}} entries to the front of the pipeline (otherwise its an mongo error)
+2 -1
View File
@@ -1,9 +1,10 @@
package mongoext package mongoext
import ( import (
"go.mongodb.org/mongo-driver/bson"
"reflect" "reflect"
"strings" "strings"
"go.mongodb.org/mongo-driver/v2/bson"
) )
// ProjectionFromStruct automatically generated a mongodb projection for a struct // ProjectionFromStruct automatically generated a mongodb projection for a struct
+5 -39
View File
@@ -1,51 +1,17 @@
package mongoext package mongoext
import ( 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" "reflect"
"go.mongodb.org/mongo-driver/v2/bson"
) )
func CreateGoExtBsonRegistry() *bsoncodec.Registry { func CreateGoExtBsonRegistry() *bson.Registry {
reg := bson.NewRegistry() reg := bson.NewRegistry()
reg.RegisterTypeDecoder(reflect.TypeFor[rfctime.RFC3339Time](), rfctime.RFC3339Time{}) // otherwise we get []bson.E when unmarshalling into any
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
// which will result in {'key': .., 'value': ...}[] json when json-marshalling // 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 return reg
} }
+3 -2
View File
@@ -2,8 +2,9 @@ package pagination
import ( import (
"context" "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 { type MongoFilter interface {
+8 -7
View File
@@ -3,12 +3,13 @@ package reflectext
import ( import (
"errors" "errors"
"fmt" "fmt"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"go.mongodb.org/mongo-driver/bson/primitive"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"go.mongodb.org/mongo-driver/v2/bson"
) )
var primitiveSerializer = map[reflect.Type]genSerializer{ var primitiveSerializer = map[reflect.Type]genSerializer{
@@ -28,7 +29,7 @@ var primitiveSerializer = map[reflect.Type]genSerializer{
reflect.TypeFor[bool](): newGenSerializer(serBoolToString, serStringToBool), 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), reflect.TypeFor[time.Time](): newGenSerializer(serTimeToString, serStringToTime),
} }
@@ -111,15 +112,15 @@ func serStringToBool(v string) (bool, error) {
return false, errors.New(fmt.Sprintf("invalid boolean value '%s'", v)) 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 return v.Hex(), nil
} }
func serStringToObjectID(v string) (primitive.ObjectID, error) { func serStringToObjectID(v string) (bson.ObjectID, error) {
if rv, err := primitive.ObjectIDFromHex(v); err == nil { if rv, err := bson.ObjectIDFromHex(v); err == nil {
return rv, nil return rv, nil
} else { } else {
return primitive.ObjectID{}, err return bson.ObjectID{}, err
} }
} }
+14 -44
View File
@@ -4,14 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "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"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"go.mongodb.org/mongo-driver/v2/bson"
) )
type Date struct { type Date struct {
@@ -83,8 +80,8 @@ func (t *Date) UnmarshalText(data []byte) error {
return t.ParseString(string(data)) return t.ParseString(string(data))
} }
func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (t *Date) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bsontype.Null { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -92,12 +89,12 @@ func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
*t = Date{} *t = Date{}
return nil return nil
} }
if bt != bsontype.String { if bson.Type(bt) != bson.TypeString {
return errors.New(fmt.Sprintf("cannot unmarshal %v into Date", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into Date", bson.Type(bt)))
} }
var tt string 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 { if err != nil {
return err return err
} }
@@ -120,43 +117,16 @@ func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
return nil 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() { if t.IsZero() {
return bson.MarshalValue("") tp, data, err = 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))
} else { } else {
val.Set(reflect.ValueOf(t)) tp, data, err = bson.MarshalValue(t.String())
} }
return byte(tp), data, err
return nil
} }
func (t Date) Serialize() string { func (t Date) Serialize() string {
+9 -44
View File
@@ -4,13 +4,9 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"reflect"
"time" "time"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
) )
type RFC3339Time time.Time type RFC3339Time time.Time
@@ -69,8 +65,8 @@ func (t *RFC3339Time) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *RFC3339Time) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (t *RFC3339Time) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bson.TypeNull { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -78,11 +74,11 @@ func (t *RFC3339Time) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
*t = RFC3339Time{} *t = RFC3339Time{}
return nil return nil
} }
if bt != bson.TypeDateTime { if bson.Type(bt) != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339Time", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339Time", bson.Type(bt)))
} }
var tt time.Time 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 { if err != nil {
return err return err
} }
@@ -90,40 +86,9 @@ func (t *RFC3339Time) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
return nil return nil
} }
func (t RFC3339Time) MarshalBSONValue() (bsontype.Type, []byte, error) { func (t RFC3339Time) MarshalBSONValue() (byte, []byte, error) {
return bson.MarshalValue(time.Time(t)) tp, data, err := bson.MarshalValue(time.Time(t))
} return byte(tp), data, err
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) Serialize() string { func (t RFC3339Time) Serialize() string {
+10 -44
View File
@@ -4,12 +4,9 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "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"
"time" "time"
"go.mongodb.org/mongo-driver/v2/bson"
) )
type RFC3339NanoTime time.Time type RFC3339NanoTime time.Time
@@ -68,8 +65,8 @@ func (t *RFC3339NanoTime) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *RFC3339NanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (t *RFC3339NanoTime) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bson.TypeNull { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -77,11 +74,11 @@ func (t *RFC3339NanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) erro
*t = RFC3339NanoTime{} *t = RFC3339NanoTime{}
return nil return nil
} }
if bt != bson.TypeDateTime { if bson.Type(bt) != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339NanoTime", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339NanoTime", bson.Type(bt)))
} }
var tt time.Time 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 { if err != nil {
return err return err
} }
@@ -89,40 +86,9 @@ func (t *RFC3339NanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) erro
return nil return nil
} }
func (t RFC3339NanoTime) MarshalBSONValue() (bsontype.Type, []byte, error) { func (t RFC3339NanoTime) MarshalBSONValue() (byte, []byte, error) {
return bson.MarshalValue(time.Time(t)) tp, data, err := bson.MarshalValue(time.Time(t))
} return byte(tp), data, err
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) Serialize() string { func (t RFC3339NanoTime) Serialize() string {
+11 -45
View File
@@ -4,13 +4,10 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "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"
"time" "time"
"git.blackforestbytes.com/BlackForestBytes/goext/timeext"
"go.mongodb.org/mongo-driver/v2/bson"
) )
type SecondsF64 time.Duration type SecondsF64 time.Duration
@@ -61,8 +58,8 @@ func (d SecondsF64) MarshalJSON() ([]byte, error) {
return json.Marshal(secs) return json.Marshal(secs)
} }
func (d *SecondsF64) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (d *SecondsF64) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bson.TypeNull { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -70,11 +67,11 @@ func (d *SecondsF64) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
*d = SecondsF64(0) *d = SecondsF64(0)
return nil return nil
} }
if bt != bson.TypeDouble { if bson.Type(bt) != bson.TypeDouble {
return errors.New(fmt.Sprintf("cannot unmarshal %v into SecondsF64", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into SecondsF64", bson.Type(bt)))
} }
var secValue float64 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 { if err != nil {
return err return err
} }
@@ -82,40 +79,9 @@ func (d *SecondsF64) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
return nil return nil
} }
func (d SecondsF64) MarshalBSONValue() (bsontype.Type, []byte, error) { func (d SecondsF64) MarshalBSONValue() (byte, []byte, error) {
return bson.MarshalValue(d.Seconds()) tp, data, err := bson.MarshalValue(d.Seconds())
} return byte(tp), data, err
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 NewSecondsF64(t time.Duration) SecondsF64 { func NewSecondsF64(t time.Duration) SecondsF64 {
+10 -44
View File
@@ -4,13 +4,10 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "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"
"strconv" "strconv"
"time" "time"
"go.mongodb.org/mongo-driver/v2/bson"
) )
type UnixTime time.Time type UnixTime time.Time
@@ -66,8 +63,8 @@ func (t *UnixTime) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *UnixTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (t *UnixTime) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bson.TypeNull { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -75,11 +72,11 @@ func (t *UnixTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
*t = UnixTime{} *t = UnixTime{}
return nil return nil
} }
if bt != bson.TypeDateTime { if bson.Type(bt) != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixTime", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixTime", bson.Type(bt)))
} }
var tt time.Time 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 { if err != nil {
return err return err
} }
@@ -87,40 +84,9 @@ func (t *UnixTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
return nil return nil
} }
func (t UnixTime) MarshalBSONValue() (bsontype.Type, []byte, error) { func (t UnixTime) MarshalBSONValue() (byte, []byte, error) {
return bson.MarshalValue(time.Time(t)) tp, data, err := bson.MarshalValue(time.Time(t))
} return byte(tp), data, err
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) Serialize() string { func (t UnixTime) Serialize() string {
+10 -44
View File
@@ -4,13 +4,10 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "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"
"strconv" "strconv"
"time" "time"
"go.mongodb.org/mongo-driver/v2/bson"
) )
type UnixMilliTime time.Time type UnixMilliTime time.Time
@@ -66,8 +63,8 @@ func (t *UnixMilliTime) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *UnixMilliTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (t *UnixMilliTime) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bson.TypeNull { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -75,11 +72,11 @@ func (t *UnixMilliTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error
*t = UnixMilliTime{} *t = UnixMilliTime{}
return nil return nil
} }
if bt != bson.TypeDateTime { if bson.Type(bt) != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixMilliTime", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixMilliTime", bson.Type(bt)))
} }
var tt time.Time 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 { if err != nil {
return err return err
} }
@@ -87,40 +84,9 @@ func (t *UnixMilliTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error
return nil return nil
} }
func (t UnixMilliTime) MarshalBSONValue() (bsontype.Type, []byte, error) { func (t UnixMilliTime) MarshalBSONValue() (byte, []byte, error) {
return bson.MarshalValue(time.Time(t)) tp, data, err := bson.MarshalValue(time.Time(t))
} return byte(tp), data, err
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) Serialize() string { func (t UnixMilliTime) Serialize() string {
+10 -44
View File
@@ -4,13 +4,10 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "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"
"strconv" "strconv"
"time" "time"
"go.mongodb.org/mongo-driver/v2/bson"
) )
type UnixNanoTime time.Time type UnixNanoTime time.Time
@@ -66,8 +63,8 @@ func (t *UnixNanoTime) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *UnixNanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error { func (t *UnixNanoTime) UnmarshalBSONValue(bt byte, data []byte) error {
if bt == bson.TypeNull { if bson.Type(bt) == bson.TypeNull {
// we can't set nil in UnmarshalBSONValue (so we use default(struct)) // we can't set nil in UnmarshalBSONValue (so we use default(struct))
// Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values // Use mongoext.CreateGoExtBsonRegistry if you need to unmarsh pointer values
// https://stackoverflow.com/questions/75167597 // https://stackoverflow.com/questions/75167597
@@ -75,11 +72,11 @@ func (t *UnixNanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
*t = UnixNanoTime{} *t = UnixNanoTime{}
return nil return nil
} }
if bt != bson.TypeDateTime { if bson.Type(bt) != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixNanoTime", bt)) return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixNanoTime", bson.Type(bt)))
} }
var tt time.Time 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 { if err != nil {
return err return err
} }
@@ -87,40 +84,9 @@ func (t *UnixNanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
return nil return nil
} }
func (t UnixNanoTime) MarshalBSONValue() (bsontype.Type, []byte, error) { func (t UnixNanoTime) MarshalBSONValue() (byte, []byte, error) {
return bson.MarshalValue(time.Time(t)) tp, data, err := bson.MarshalValue(time.Time(t))
} return byte(tp), data, err
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) Serialize() string { func (t UnixNanoTime) Serialize() string {
+1 -4
View File
@@ -51,10 +51,7 @@ func GetIsoWeekCount(year int) int {
w2 -= 1 w2 -= 1
w3 -= 1 w3 -= 1
w := max(w2, w1) w := max(w3, max(w2, w1))
if w3 > w {
w = w3
}
return w return w
} }
+4 -4
View File
@@ -2,16 +2,16 @@ package wmo
import ( import (
"context" "context"
"reflect"
ct "git.blackforestbytes.com/BlackForestBytes/goext/cursortoken" ct "git.blackforestbytes.com/BlackForestBytes/goext/cursortoken"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/langext"
"go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/mongo"
"reflect"
) )
type EntityID interface { type EntityID interface {
MarshalBSONValue() (bsontype.Type, []byte, error) MarshalBSONValue() (byte, []byte, error)
String() string String() string
} }
+2 -1
View File
@@ -2,8 +2,9 @@ package wmo
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/bson"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr" "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) { func (c *Coll[TData]) decodeSingle(ctx context.Context, dec Decodable) (TData, error) {
+1 -1
View File
@@ -1,6 +1,6 @@
package wmo 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] { func W[TData any](collection *mongo.Collection) *Coll[TData] {
c := Coll[TData]{coll: collection} c := Coll[TData]{coll: collection}
+6 -5
View File
@@ -2,13 +2,14 @@ package wmo
import ( import (
"context" "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/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "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 { for _, ppl := range c.extraModPipeline {
pipeline = langext.ArrConcat(pipeline, ppl(ctx)) 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 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 { for _, ppl := range c.extraModPipeline {
pipeline = langext.ArrConcat(pipeline, ppl(ctx)) pipeline = langext.ArrConcat(pipeline, ppl(ctx))
@@ -53,7 +54,7 @@ func (c *Coll[TData]) AggregateOneOpt(ctx context.Context, pipeline mongo.Pipeli
return nil, nil 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 { for _, ppl := range c.extraModPipeline {
pipeline = langext.ArrConcat(pipeline, ppl(ctx)) pipeline = langext.ArrConcat(pipeline, ppl(ctx))
+3 -2
View File
@@ -2,9 +2,10 @@ package wmo
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr" "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 { func (c *Coll[TData]) DeleteOneByID(ctx context.Context, id EntityID) error {
+16 -14
View File
@@ -2,12 +2,13 @@ package wmo
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/bson" "iter"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "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) { 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 { for _, v := range opts {
vConv, err := convertFindOpt(v) vConv, err := convertFindOpt(v)
if err != nil { if err != nil {
@@ -60,7 +61,14 @@ func (c *Coll[TData]) createFindQuery(ctx context.Context, filter bson.M, opts .
convOpts = append(convOpts, vConv) 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 { if err != nil {
return nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build() 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 // converts FindOptions to AggregateOptions
func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) { func convertFindOpt(v *options.FindOptions) (*options.AggregateOptionsBuilder, error) {
if v == nil { if v == nil {
return nil, nil return nil, nil
} }
@@ -157,7 +165,7 @@ func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) {
r.SetCollation(v.Collation) r.SetCollation(v.Collation)
} }
if v.Comment != nil { if v.Comment != nil {
r.SetComment(*v.Comment) r.SetComment(v.Comment)
} }
if v.CursorType != nil { if v.CursorType != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'CursorType' (cannot convert to AggregateOptions)").Build() 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 { if v.MaxAwaitTime != nil {
r.SetMaxAwaitTime(*v.MaxAwaitTime) r.SetMaxAwaitTime(*v.MaxAwaitTime)
} }
if v.MaxTime != nil {
r.SetMaxTime(*v.MaxTime)
}
if v.Min != nil { if v.Min != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'Min' (cannot convert to AggregateOptions)").Build() 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 { if v.ShowRecordID != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'ShowRecordID' (cannot convert to AggregateOptions)").Build() 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 { if v.Let != nil {
r.SetLet(v.Let) r.SetLet(v.Let)
} }
+3 -2
View File
@@ -3,10 +3,11 @@ package wmo
import ( import (
"context" "context"
"errors" "errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "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) { func (c *Coll[TData]) FindOne(ctx context.Context, filter bson.M) (TData, error) {
+3 -2
View File
@@ -2,10 +2,11 @@ package wmo
import ( import (
"context" "context"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/langext"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/v2/mongo"
) )
func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, error) { func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, error) {
+4 -3
View File
@@ -2,12 +2,13 @@ package wmo
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/bson" "iter"
"go.mongodb.org/mongo-driver/mongo"
ct "git.blackforestbytes.com/BlackForestBytes/goext/cursortoken" ct "git.blackforestbytes.com/BlackForestBytes/goext/cursortoken"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "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) { func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CursorToken) ([]TData, ct.CursorToken, error) {
+4 -3
View File
@@ -2,12 +2,13 @@ package wmo
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/bson" "iter"
"go.mongodb.org/mongo-driver/mongo"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/langext"
pag "git.blackforestbytes.com/BlackForestBytes/goext/pagination" 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) { func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.MongoFilter, page int, limit *int) ([]TData, pag.Pagination, error) {
+4 -3
View File
@@ -2,10 +2,11 @@ package wmo
import ( import (
"context" "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/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) { func (c *Coll[TData]) FindOneAndUpdate(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (TData, error) {
+4 -4
View File
@@ -9,8 +9,8 @@ import (
"git.blackforestbytes.com/BlackForestBytes/goext/rfctime" "git.blackforestbytes.com/BlackForestBytes/goext/rfctime"
"git.blackforestbytes.com/BlackForestBytes/goext/timeext" "git.blackforestbytes.com/BlackForestBytes/goext/timeext"
"git.blackforestbytes.com/BlackForestBytes/goext/tst" "git.blackforestbytes.com/BlackForestBytes/goext/tst"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/v2/mongo"
) )
func TestReflectionGetFieldType(t *testing.T) { func TestReflectionGetFieldType(t *testing.T) {
@@ -234,8 +234,8 @@ func TestReflectionGetFieldValueAsTokenString(t *testing.T) {
func TestReflectionWithInterface(t *testing.T) { func TestReflectionWithInterface(t *testing.T) {
type TestData struct { type TestData struct {
ID primitive.ObjectID `bson:"_id"` ID bson.ObjectID `bson:"_id"`
CDate time.Time `bson:"cdate"` CDate time.Time `bson:"cdate"`
} }
type TestInterface any type TestInterface any