Compare commits

..

3 Commits

Author SHA1 Message Date
Mikescher 18c172d69a Revert gojson changes
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s
2026-04-26 14:29:28 +02:00
viktor d30e778bd4 fixed test
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m35s
2026-04-21 16:54:52 +02:00
viktor 73e867f75a fixed test to properly wait for goroutine completion 2026-04-21 16:54:28 +02:00
44 changed files with 661 additions and 227 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ package cursortoken
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
type RawFilter interface { type RawFilter interface {
+7 -7
View File
@@ -3,7 +3,7 @@ package cursortoken
import ( import (
"encoding/base32" "encoding/base32"
"encoding/json" "encoding/json"
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/bson/primitive"
"time" "time"
) )
@@ -119,18 +119,18 @@ func (c CTKeySort) IsStart() bool {
return c.Mode == CTMStart return c.Mode == CTMStart
} }
func (c CTKeySort) valuePrimaryObjectId() (bson.ObjectID, bool) { func (c CTKeySort) valuePrimaryObjectId() (primitive.ObjectID, bool) {
if oid, err := bson.ObjectIDFromHex(c.ValuePrimary); err == nil { if oid, err := primitive.ObjectIDFromHex(c.ValuePrimary); err == nil {
return oid, true return oid, true
} else { } else {
return bson.ObjectID{}, false return primitive.ObjectID{}, false
} }
} }
func (c CTKeySort) valueSecondaryObjectId() (bson.ObjectID, bool) { func (c CTKeySort) valueSecondaryObjectId() (primitive.ObjectID, bool) {
if oid, err := bson.ObjectIDFromHex(c.ValueSecondary); err == nil { if oid, err := primitive.ObjectIDFromHex(c.ValueSecondary); err == nil {
return oid, true return oid, true
} else { } else {
return bson.ObjectID{}, false return primitive.ObjectID{}, false
} }
} }
+11 -1
View File
@@ -90,15 +90,17 @@ func TestBroadcast_SubscribeByIter(t *testing.T) {
// Channel to communicate when message is received // Channel to communicate when message is received
done := make(chan bool) done := make(chan bool)
goroutineDone := make(chan struct{})
received := false received := false
// Start a goroutine to use the iterator // Start a goroutine to use the iterator
go func() { go func() {
defer close(goroutineDone)
for msg := range iterSeq { for msg := range iterSeq {
if msg == "hello" { if msg == "hello" {
received = true received = true
done <- true done <- true
return // Stop iteration return // Stop iteration — triggers Unsubscribe via yield returning false
} }
} }
}() }()
@@ -119,6 +121,14 @@ func TestBroadcast_SubscribeByIter(t *testing.T) {
t.Fatal("Timed out waiting for message") t.Fatal("Timed out waiting for message")
} }
// Wait for the goroutine to fully exit so Unsubscribe (triggered by the
// iterator cleanup when yield returns false) has completed.
select {
case <-goroutineDone:
case <-time.After(time.Second):
t.Fatal("Timed out waiting for goroutine to finish")
}
subCount := bb.SubscriberCount() subCount := bb.SubscriberCount()
if subCount != 0 { if subCount != 0 {
t.Fatalf("Expected 0 receivers, got %d", subCount) t.Fatalf("Expected 0 receivers, got %d", subCount)
+11 -1
View File
@@ -129,15 +129,17 @@ func TestPubSub_SubscribeByIter(t *testing.T) {
// Channel to communicate when message is received // Channel to communicate when message is received
done := make(chan bool) done := make(chan bool)
goroutineDone := make(chan struct{})
received := false received := false
// Start a goroutine to use the iterator // Start a goroutine to use the iterator
go func() { go func() {
defer close(goroutineDone)
for msg := range iterSeq { for msg := range iterSeq {
if msg == "hello" { if msg == "hello" {
received = true received = true
done <- true done <- true
return // Stop iteration return // Stop iteration — triggers Unsubscribe via yield returning false
} }
} }
}() }()
@@ -158,6 +160,14 @@ func TestPubSub_SubscribeByIter(t *testing.T) {
t.Fatal("Timed out waiting for message") t.Fatal("Timed out waiting for message")
} }
// Wait for the goroutine to fully exit so Unsubscribe (triggered by the
// iterator cleanup when yield returns false) has completed.
select {
case <-goroutineDone:
case <-time.After(time.Second):
t.Fatal("Timed out waiting for goroutine to finish")
}
subCount := ps.SubscriberCount("test-ns") subCount := ps.SubscriberCount("test-ns")
if subCount != 0 { if subCount != 0 {
t.Fatalf("Expected 0 receivers, got %d", subCount) t.Fatalf("Expected 0 receivers, got %d", subCount)
+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/v2/bson" "go.mongodb.org/mongo-driver/bson/primitive"
) )
// //
@@ -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 bson.ObjectID) *Builder { func (b *Builder) ObjectID(key string, val primitive.ObjectID) *Builder {
return b.addMeta(key, MDTObjectID, val) return b.addMeta(key, MDTObjectID, val)
} }
+2 -2
View File
@@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/langext"
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/bson/primitive"
"maps" "maps"
"reflect" "reflect"
"time" "time"
@@ -222,7 +222,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 bson.ObjectID: case primitive.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}}
+44 -9
View File
@@ -4,7 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/v2/bson" "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"
) )
type ErrorCategory struct{ Category string } type ErrorCategory struct{ Category string }
@@ -24,8 +28,8 @@ func (e ErrorCategory) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Category) return json.Marshal(e.Category)
} }
func (e *ErrorCategory) UnmarshalBSONValue(bt byte, data []byte) error { func (e *ErrorCategory) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if 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
@@ -33,11 +37,11 @@ func (e *ErrorCategory) UnmarshalBSONValue(bt byte, data []byte) error {
*e = ErrorCategory{} *e = ErrorCategory{}
return nil return nil
} }
if bson.Type(bt) != bson.TypeString { if bt != bson.TypeString {
return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt))
} }
var tt string var tt string
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
if err != nil { if err != nil {
return err return err
} }
@@ -45,9 +49,40 @@ func (e *ErrorCategory) UnmarshalBSONValue(bt byte, data []byte) error {
return nil return nil
} }
func (e ErrorCategory) MarshalBSONValue() (byte, []byte, error) { func (e ErrorCategory) MarshalBSONValue() (bsontype.Type, []byte, error) {
tp, data, err := bson.MarshalValue(e.Category) return 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
+44 -9
View File
@@ -4,7 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/v2/bson" "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"
) )
type ErrorSeverity struct{ Severity string } type ErrorSeverity struct{ Severity string }
@@ -26,8 +30,8 @@ func (e ErrorSeverity) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Severity) return json.Marshal(e.Severity)
} }
func (e *ErrorSeverity) UnmarshalBSONValue(bt byte, data []byte) error { func (e *ErrorSeverity) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if 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
@@ -35,11 +39,11 @@ func (e *ErrorSeverity) UnmarshalBSONValue(bt byte, data []byte) error {
*e = ErrorSeverity{} *e = ErrorSeverity{}
return nil return nil
} }
if bson.Type(bt) != bson.TypeString { if bt != bson.TypeString {
return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt))
} }
var tt string var tt string
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
if err != nil { if err != nil {
return err return err
} }
@@ -47,9 +51,40 @@ func (e *ErrorSeverity) UnmarshalBSONValue(bt byte, data []byte) error {
return nil return nil
} }
func (e ErrorSeverity) MarshalBSONValue() (byte, []byte, error) { func (e ErrorSeverity) MarshalBSONValue() (bsontype.Type, []byte, error) {
tp, data, err := bson.MarshalValue(e.Severity) return 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
+44 -9
View File
@@ -4,9 +4,13 @@ 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/v2/bson" "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"
) )
type ErrorType struct { type ErrorType struct {
@@ -76,8 +80,8 @@ func (e ErrorType) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Key) return json.Marshal(e.Key)
} }
func (e *ErrorType) UnmarshalBSONValue(bt byte, data []byte) error { func (e *ErrorType) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if 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
@@ -85,11 +89,11 @@ func (e *ErrorType) UnmarshalBSONValue(bt byte, data []byte) error {
*e = ErrorType{} *e = ErrorType{}
return nil return nil
} }
if bson.Type(bt) != bson.TypeString { if bt != bson.TypeString {
return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into String", bt))
} }
var tt string var tt string
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
if err != nil { if err != nil {
return err return err
} }
@@ -103,9 +107,40 @@ func (e *ErrorType) UnmarshalBSONValue(bt byte, data []byte) error {
} }
} }
func (e ErrorType) MarshalBSONValue() (byte, []byte, error) { func (e ErrorType) MarshalBSONValue() (bsontype.Type, []byte, error) {
tp, data, err := bson.MarshalValue(e.Key) return 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]{}
+15 -14
View File
@@ -3,9 +3,10 @@ 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" "git.blackforestbytes.com/BlackForestBytes/goext/tst"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"testing" "testing"
"time" "time"
) )
@@ -56,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() client, err := mongo.Connect(ctx)
if err != nil { if err != nil {
t.Skip("Skip test - no local mongo found") t.Skip("Skip test - no local mongo found")
return return
@@ -67,7 +68,7 @@ func TestBSONMarshalErrorCategory(t *testing.T) {
return return
} }
primimd := bson.NewObjectID() primimd := primitive.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)
@@ -75,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 bson.ObjectID `bson:"_id"` ID primitive.ObjectID `bson:"_id"`
Val ErrorCategory `bson:"val"` Val ErrorCategory `bson:"val"`
} }
err = cursor.Decode(&c1) err = cursor.Decode(&c1)
@@ -89,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() client, err := mongo.Connect(ctx)
if err != nil { if err != nil {
t.Skip("Skip test - no local mongo found") t.Skip("Skip test - no local mongo found")
return return
@@ -100,7 +101,7 @@ func TestBSONMarshalErrorSeverity(t *testing.T) {
return return
} }
primimd := bson.NewObjectID() primimd := primitive.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)
@@ -108,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 bson.ObjectID `bson:"_id"` ID primitive.ObjectID `bson:"_id"`
Val ErrorSeverity `bson:"val"` Val ErrorSeverity `bson:"val"`
} }
err = cursor.Decode(&c1) err = cursor.Decode(&c1)
@@ -122,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() client, err := mongo.Connect(ctx)
if err != nil { if err != nil {
t.Skip("Skip test - no local mongo found") t.Skip("Skip test - no local mongo found")
return return
@@ -133,7 +134,7 @@ func TestBSONMarshalErrorType(t *testing.T) {
return return
} }
primimd := bson.NewObjectID() primimd := primitive.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)
@@ -141,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 bson.ObjectID `bson:"_id"` ID primitive.ObjectID `bson:"_id"`
Val ErrorType `bson:"val"` Val ErrorType `bson:"val"`
} }
err = cursor.Decode(&c1) err = cursor.Decode(&c1)
+4 -3
View File
@@ -2,12 +2,13 @@ package exerr
import ( import (
"fmt" "fmt"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"github.com/rs/xid"
"github.com/rs/zerolog"
"reflect" "reflect"
"strings" "strings"
"time" "time"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"github.com/rs/xid"
"github.com/rs/zerolog"
) )
type ExErr struct { type ExErr struct {
+10 -9
View File
@@ -7,7 +7,8 @@ import (
"fmt" "fmt"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/langext"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"math" "math"
"strconv" "strconv"
"strings" "strings"
@@ -98,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.(bson.ObjectID).Hex(), nil return v.Value.(primitive.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:
@@ -177,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.(bson.ObjectID).Hex() return v.Value.(primitive.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:
@@ -265,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.(bson.ObjectID).Hex()) return evt.Str(key, v.Value.(primitive.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:
@@ -459,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 := bson.ObjectIDFromHex(value) r, err := primitive.ObjectIDFromHex(value)
if err != nil { if err != nil {
return err return err
} }
@@ -576,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.(bson.ObjectID).Hex() return v.Value.(primitive.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:
@@ -627,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 := bson.M{} jsonobj := primitive.M{}
jsonarr := bson.A{} jsonarr := primitive.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 {
@@ -653,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.(bson.ObjectID).Hex() return v.Value.(primitive.ObjectID).Hex()
} }
if v.DataType == MDTNil { if v.DataType == MDTNil {
return nil return nil
+2 -2
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/v2 v2.5.1 go.mongodb.org/mongo-driver v1.17.9
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
@@ -20,7 +20,6 @@ require (
github.com/gorilla/websocket v1.5.3 github.com/gorilla/websocket v1.5.3
github.com/jung-kurt/gofpdf v1.16.2 github.com/jung-kurt/gofpdf v1.16.2
github.com/xuri/excelize/v2 v2.10.1 github.com/xuri/excelize/v2 v2.10.1
go.mongodb.org/mongo-driver v1.17.9
golang.org/x/sync v0.20.0 golang.org/x/sync v0.20.0
) )
@@ -65,6 +64,7 @@ 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
+8 -8
View File
@@ -108,11 +108,11 @@ func (u unmarshalerText) MarshalText() ([]byte, error) {
} }
func (u *unmarshalerText) UnmarshalText(b []byte) error { func (u *unmarshalerText) UnmarshalText(b []byte) error {
before, after, ok := bytes.Cut(b, []byte{':'}) pos := bytes.IndexByte(b, ':')
if !ok { if pos == -1 {
return errors.New("missing separator") return errors.New("missing separator")
} }
u.A, u.B = string(before), string(after) u.A, u.B = string(b[:pos]), string(b[pos+1:])
return nil return nil
} }
@@ -126,7 +126,7 @@ type ustructText struct {
type u8marshal uint8 type u8marshal uint8
func (u8 u8marshal) MarshalText() ([]byte, error) { func (u8 u8marshal) MarshalText() ([]byte, error) {
return fmt.Appendf(nil, "u%d", u8), nil return []byte(fmt.Sprintf("u%d", u8)), nil
} }
var errMissingU8Prefix = errors.New("missing 'u' prefix") var errMissingU8Prefix = errors.New("missing 'u' prefix")
@@ -275,7 +275,7 @@ func (unexportedWithMethods) F() {}
type byteWithMarshalJSON byte type byteWithMarshalJSON byte
func (b byteWithMarshalJSON) MarshalJSON() ([]byte, error) { func (b byteWithMarshalJSON) MarshalJSON() ([]byte, error) {
return fmt.Appendf(nil, `"Z%.2x"`, byte(b)), nil return []byte(fmt.Sprintf(`"Z%.2x"`, byte(b))), nil
} }
func (b *byteWithMarshalJSON) UnmarshalJSON(data []byte) error { func (b *byteWithMarshalJSON) UnmarshalJSON(data []byte) error {
@@ -303,7 +303,7 @@ func (b *byteWithPtrMarshalJSON) UnmarshalJSON(data []byte) error {
type byteWithMarshalText byte type byteWithMarshalText byte
func (b byteWithMarshalText) MarshalText() ([]byte, error) { func (b byteWithMarshalText) MarshalText() ([]byte, error) {
return fmt.Appendf(nil, `Z%.2x`, byte(b)), nil return []byte(fmt.Sprintf(`Z%.2x`, byte(b))), nil
} }
func (b *byteWithMarshalText) UnmarshalText(data []byte) error { func (b *byteWithMarshalText) UnmarshalText(data []byte) error {
@@ -331,7 +331,7 @@ func (b *byteWithPtrMarshalText) UnmarshalText(data []byte) error {
type intWithMarshalJSON int type intWithMarshalJSON int
func (b intWithMarshalJSON) MarshalJSON() ([]byte, error) { func (b intWithMarshalJSON) MarshalJSON() ([]byte, error) {
return fmt.Appendf(nil, `"Z%.2x"`, int(b)), nil return []byte(fmt.Sprintf(`"Z%.2x"`, int(b))), nil
} }
func (b *intWithMarshalJSON) UnmarshalJSON(data []byte) error { func (b *intWithMarshalJSON) UnmarshalJSON(data []byte) error {
@@ -359,7 +359,7 @@ func (b *intWithPtrMarshalJSON) UnmarshalJSON(data []byte) error {
type intWithMarshalText int type intWithMarshalText int
func (b intWithMarshalText) MarshalText() ([]byte, error) { func (b intWithMarshalText) MarshalText() ([]byte, error) {
return fmt.Appendf(nil, `Z%.2x`, int(b)), nil return []byte(fmt.Sprintf(`Z%.2x`, int(b))), nil
} }
func (b *intWithMarshalText) UnmarshalText(data []byte) error { func (b *intWithMarshalText) UnmarshalText(data []byte) error {
+13 -5
View File
@@ -177,7 +177,7 @@ type IndentOpt struct {
// MarshalSafeCollections is like Marshal except it will marshal nil maps and // MarshalSafeCollections is like Marshal except it will marshal nil maps and
// slices as '{}' and '[]' respectfully instead of 'null' // slices as '{}' and '[]' respectfully instead of 'null'
func MarshalSafeCollections(v any, nilSafeSlices bool, nilSafeMaps bool, indent *IndentOpt, filter *string) ([]byte, error) { func MarshalSafeCollections(v interface{}, nilSafeSlices bool, nilSafeMaps bool, indent *IndentOpt, filter *string) ([]byte, error) {
e := &encodeState{} e := &encodeState{}
err := e.marshal(v, encOpts{escapeHTML: true, nilSafeSlices: nilSafeSlices, nilSafeMaps: nilSafeMaps, filter: filter}) err := e.marshal(v, encOpts{escapeHTML: true, nilSafeSlices: nilSafeSlices, nilSafeMaps: nilSafeMaps, filter: filter})
if err != nil { if err != nil {
@@ -891,7 +891,7 @@ func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
// Here we use a struct to memorize the pointer to the first element of the slice // Here we use a struct to memorize the pointer to the first element of the slice
// and its length. // and its length.
ptr := struct { ptr := struct {
ptr any // always an unsafe.Pointer, but avoids a dependency on package unsafe ptr interface{} // always an unsafe.Pointer, but avoids a dependency on package unsafe
len int len int
}{v.UnsafePointer(), v.Len()} }{v.UnsafePointer(), v.Len()}
if _, ok := e.ptrSeen[ptr]; ok { if _, ok := e.ptrSeen[ptr]; ok {
@@ -923,7 +923,7 @@ type arrayEncoder struct {
func (ae arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { func (ae arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
e.WriteByte('[') e.WriteByte('[')
n := v.Len() n := v.Len()
for i := range n { for i := 0; i < n; i++ {
if i > 0 { if i > 0 {
e.WriteByte(',') e.WriteByte(',')
} }
@@ -1075,7 +1075,10 @@ func appendString[Bytes []byte | string](dst []byte, src Bytes, escapeHTML bool)
// For now, cast only a small portion of byte slices to a string // For now, cast only a small portion of byte slices to a string
// so that it can be stack allocated. This slows down []byte slightly // so that it can be stack allocated. This slows down []byte slightly
// due to the extra copy, but keeps string performance roughly the same. // due to the extra copy, but keeps string performance roughly the same.
n := min(len(src)-i, utf8.UTFMax) n := len(src) - i
if n > utf8.UTFMax {
n = utf8.UTFMax
}
c, size := utf8.DecodeRuneInString(string(src[i : i+n])) c, size := utf8.DecodeRuneInString(string(src[i : i+n]))
if c == utf8.RuneError && size == 1 { if c == utf8.RuneError && size == 1 {
dst = append(dst, src[start:i]...) dst = append(dst, src[start:i]...)
@@ -1127,7 +1130,12 @@ type field struct {
type jsonfilter []string type jsonfilter []string
func (j jsonfilter) Contains(t string) bool { func (j jsonfilter) Contains(t string) bool {
return slices.Contains(j, t) for _, tag := range j {
if t == tag {
return true
}
}
return false
} }
// typeFields returns a list of fields that JSON should recognize for the given type. // typeFields returns a list of fields that JSON should recognize for the given type.
+15 -14
View File
@@ -41,7 +41,7 @@ type Optionals struct {
Uo uint `json:"uo,omitempty"` Uo uint `json:"uo,omitempty"`
Str struct{} `json:"str"` Str struct{} `json:"str"`
Sto struct{} `json:"sto"` Sto struct{} `json:"sto,omitempty"`
} }
func TestOmitEmpty(t *testing.T) { func TestOmitEmpty(t *testing.T) {
@@ -1166,7 +1166,8 @@ func TestMarshalUncommonFieldNames(t *testing.T) {
} }
func TestMarshalerError(t *testing.T) { func TestMarshalerError(t *testing.T) {
st := reflect.TypeFor[string]() s := "test variable"
st := reflect.TypeOf(s)
const errText = "json: test error" const errText = "json: test error"
tests := []struct { tests := []struct {
@@ -1221,18 +1222,18 @@ func TestIssue63379(t *testing.T) {
func TestMarshalSafeCollections(t *testing.T) { func TestMarshalSafeCollections(t *testing.T) {
var ( var (
nilSlice []any nilSlice []interface{}
pNilSlice *[]any pNilSlice *[]interface{}
nilMap map[string]any nilMap map[string]interface{}
pNilMap *map[string]any pNilMap *map[string]interface{}
) )
type ( type (
nilSliceStruct struct { nilSliceStruct struct {
NilSlice []any `json:"nil_slice"` NilSlice []interface{} `json:"nil_slice"`
} }
nilMapStruct struct { nilMapStruct struct {
NilMap map[string]any `json:"nil_map"` NilMap map[string]interface{} `json:"nil_map"`
} }
testWithFilter struct { testWithFilter struct {
Test1 string `json:"test1" jsonfilter:"FILTERONE"` Test1 string `json:"test1" jsonfilter:"FILTERONE"`
@@ -1241,19 +1242,19 @@ func TestMarshalSafeCollections(t *testing.T) {
) )
tests := []struct { tests := []struct {
in any in interface{}
want string want string
}{ }{
{nilSlice, "[]"}, {nilSlice, "[]"},
{[]any{}, "[]"}, {[]interface{}{}, "[]"},
{make([]any, 0), "[]"}, {make([]interface{}, 0), "[]"},
{[]int{1, 2, 3}, "[1,2,3]"}, {[]int{1, 2, 3}, "[1,2,3]"},
{pNilSlice, "null"}, {pNilSlice, "null"},
{nilSliceStruct{}, "{\"nil_slice\":[]}"}, {nilSliceStruct{}, "{\"nil_slice\":[]}"},
{nilMap, "{}"}, {nilMap, "{}"},
{map[string]any{}, "{}"}, {map[string]interface{}{}, "{}"},
{make(map[string]any, 0), "{}"}, {make(map[string]interface{}, 0), "{}"},
{map[string]any{"1": 1, "2": 2, "3": 3}, "{\"1\":1,\"2\":2,\"3\":3}"}, {map[string]interface{}{"1": 1, "2": 2, "3": 3}, "{\"1\":1,\"2\":2,\"3\":3}"},
{pNilMap, "null"}, {pNilMap, "null"},
{nilMapStruct{}, "{\"nil_map\":{}}"}, {nilMapStruct{}, "{\"nil_map\":{}}"},
{testWithFilter{}, "{\"test1\":\"\"}"}, {testWithFilter{}, "{\"test1\":\"\"}"},
+4 -4
View File
@@ -28,10 +28,10 @@ func FuzzUnmarshalJSON(f *testing.F) {
}`)) }`))
f.Fuzz(func(t *testing.T, b []byte) { f.Fuzz(func(t *testing.T, b []byte) {
for _, typ := range []func() any{ for _, typ := range []func() interface{}{
func() any { return new(any) }, func() interface{} { return new(interface{}) },
func() any { return new(map[string]any) }, func() interface{} { return new(map[string]interface{}) },
func() any { return new([]any) }, func() interface{} { return new([]interface{}) },
} { } {
i := typ() i := typ()
if err := Unmarshal(b, i); err != nil { if err := Unmarshal(b, i); err != nil {
+1 -1
View File
@@ -90,7 +90,7 @@ func appendCompact(dst, src []byte, escape bool) ([]byte, error) {
func appendNewline(dst []byte, prefix, indent string, depth int) []byte { func appendNewline(dst []byte, prefix, indent string, depth int) []byte {
dst = append(dst, '\n') dst = append(dst, '\n')
dst = append(dst, prefix...) dst = append(dst, prefix...)
for range depth { for i := 0; i < depth; i++ {
dst = append(dst, indent...) dst = append(dst, indent...)
} }
return dst return dst
+12 -3
View File
@@ -210,7 +210,10 @@ func diff(t *testing.T, a, b []byte) {
t.Helper() t.Helper()
for i := 0; ; i++ { for i := 0; ; i++ {
if i >= len(a) || i >= len(b) || a[i] != b[i] { if i >= len(a) || i >= len(b) || a[i] != b[i] {
j := max(i-10, 0) j := i - 10
if j < 0 {
j = 0
}
t.Errorf("diverge at %d: «%s» vs «%s»", i, trim(a[j:]), trim(b[j:])) t.Errorf("diverge at %d: «%s» vs «%s»", i, trim(a[j:]), trim(b[j:]))
return return
} }
@@ -271,7 +274,10 @@ func genString(stddev float64) string {
} }
func genArray(n int) []any { func genArray(n int) []any {
f := min(int(math.Abs(rand.NormFloat64())*math.Min(10, float64(n/2))), n) f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2)))
if f > n {
f = n
}
if f < 1 { if f < 1 {
f = 1 f = 1
} }
@@ -283,7 +289,10 @@ func genArray(n int) []any {
} }
func genMap(n int) map[string]any { func genMap(n int) map[string]any {
f := min(int(math.Abs(rand.NormFloat64())*math.Min(10, float64(n/2))), n) f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2)))
if f > n {
f = n
}
if n > 0 && f == 0 { if n > 0 && f == 0 {
f = 1 f = 1
} }
+2 -2
View File
@@ -1,8 +1,8 @@
package mongoext package mongoext
import ( import (
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/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)
+1 -1
View File
@@ -1,7 +1,7 @@
package mongoext package mongoext
import ( import (
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/bson"
"reflect" "reflect"
"strings" "strings"
) )
+39 -5
View File
@@ -1,17 +1,51 @@
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() *bson.Registry { func CreateGoExtBsonRegistry() *bsoncodec.Registry {
reg := bson.NewRegistry() reg := bson.NewRegistry()
// otherwise we get []bson.E when unmarshalling into any 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
// 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[bson.M]()) reg.RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeFor[primitive.M]())
return reg return reg
} }
+2 -2
View File
@@ -2,8 +2,8 @@ package pagination
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
type MongoFilter interface { type MongoFilter interface {
+6 -6
View File
@@ -4,7 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/langext"
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/bson/primitive"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@@ -28,7 +28,7 @@ var primitiveSerializer = map[reflect.Type]genSerializer{
reflect.TypeFor[bool](): newGenSerializer(serBoolToString, serStringToBool), reflect.TypeFor[bool](): newGenSerializer(serBoolToString, serStringToBool),
reflect.TypeFor[bson.ObjectID](): newGenSerializer(serObjectIDToString, serStringToObjectID), reflect.TypeFor[primitive.ObjectID](): newGenSerializer(serObjectIDToString, serStringToObjectID),
reflect.TypeFor[time.Time](): newGenSerializer(serTimeToString, serStringToTime), 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)) return false, errors.New(fmt.Sprintf("invalid boolean value '%s'", v))
} }
func serObjectIDToString(v bson.ObjectID) (string, error) { func serObjectIDToString(v primitive.ObjectID) (string, error) {
return v.Hex(), nil return v.Hex(), nil
} }
func serStringToObjectID(v string) (bson.ObjectID, error) { func serStringToObjectID(v string) (primitive.ObjectID, error) {
if rv, err := bson.ObjectIDFromHex(v); err == nil { if rv, err := primitive.ObjectIDFromHex(v); err == nil {
return rv, nil return rv, nil
} else { } else {
return bson.ObjectID{}, err return primitive.ObjectID{}, err
} }
} }
+45 -14
View File
@@ -4,7 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/v2/bson" "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"
@@ -79,8 +83,8 @@ func (t *Date) UnmarshalText(data []byte) error {
return t.ParseString(string(data)) return t.ParseString(string(data))
} }
func (t *Date) UnmarshalBSONValue(bt byte, data []byte) error { func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if bt == bsontype.Null {
// 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
@@ -88,12 +92,12 @@ func (t *Date) UnmarshalBSONValue(bt byte, data []byte) error {
*t = Date{} *t = Date{}
return nil return nil
} }
if bson.Type(bt) != bson.TypeString { if bt != bsontype.String {
return errors.New(fmt.Sprintf("cannot unmarshal %v into Date", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into Date", bt))
} }
var tt string var tt string
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
if err != nil { if err != nil {
return err return err
} }
@@ -116,16 +120,43 @@ func (t *Date) UnmarshalBSONValue(bt byte, data []byte) error {
return nil return nil
} }
func (t Date) MarshalBSONValue() (byte, []byte, error) { func (t Date) MarshalBSONValue() (bsontype.Type, []byte, error) {
var tp bson.Type
var data []byte
var err error
if t.IsZero() { if t.IsZero() {
tp, data, err = bson.MarshalValue("") return bson.MarshalValue("")
} else {
tp, data, err = bson.MarshalValue(t.String())
} }
return byte(tp), data, err 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 {
val.Set(reflect.ValueOf(t))
}
return nil
} }
func (t Date) Serialize() string { func (t Date) Serialize() string {
+44 -9
View File
@@ -4,9 +4,13 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"reflect"
"time" "time"
"go.mongodb.org/mongo-driver/v2/bson" "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"
) )
type RFC3339Time time.Time type RFC3339Time time.Time
@@ -65,8 +69,8 @@ func (t *RFC3339Time) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *RFC3339Time) UnmarshalBSONValue(bt byte, data []byte) error { func (t *RFC3339Time) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if 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
@@ -74,11 +78,11 @@ func (t *RFC3339Time) UnmarshalBSONValue(bt byte, data []byte) error {
*t = RFC3339Time{} *t = RFC3339Time{}
return nil return nil
} }
if bson.Type(bt) != bson.TypeDateTime { if bt != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339Time", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339Time", bt))
} }
var tt time.Time var tt time.Time
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
if err != nil { if err != nil {
return err return err
} }
@@ -86,9 +90,40 @@ func (t *RFC3339Time) UnmarshalBSONValue(bt byte, data []byte) error {
return nil return nil
} }
func (t RFC3339Time) MarshalBSONValue() (byte, []byte, error) { func (t RFC3339Time) MarshalBSONValue() (bsontype.Type, []byte, error) {
tp, data, err := bson.MarshalValue(time.Time(t)) return 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 {
+44 -9
View File
@@ -4,7 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/v2/bson" "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"
) )
@@ -64,8 +68,8 @@ func (t *RFC3339NanoTime) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *RFC3339NanoTime) UnmarshalBSONValue(bt byte, data []byte) error { func (t *RFC3339NanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if 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
@@ -73,11 +77,11 @@ func (t *RFC3339NanoTime) UnmarshalBSONValue(bt byte, data []byte) error {
*t = RFC3339NanoTime{} *t = RFC3339NanoTime{}
return nil return nil
} }
if bson.Type(bt) != bson.TypeDateTime { if bt != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339NanoTime", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into RFC3339NanoTime", bt))
} }
var tt time.Time var tt time.Time
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
if err != nil { if err != nil {
return err return err
} }
@@ -85,9 +89,40 @@ func (t *RFC3339NanoTime) UnmarshalBSONValue(bt byte, data []byte) error {
return nil return nil
} }
func (t RFC3339NanoTime) MarshalBSONValue() (byte, []byte, error) { func (t RFC3339NanoTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
tp, data, err := bson.MarshalValue(time.Time(t)) return 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 {
+44 -9
View File
@@ -5,7 +5,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"git.blackforestbytes.com/BlackForestBytes/goext/timeext" "git.blackforestbytes.com/BlackForestBytes/goext/timeext"
"go.mongodb.org/mongo-driver/v2/bson" "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"
) )
@@ -57,8 +61,8 @@ func (d SecondsF64) MarshalJSON() ([]byte, error) {
return json.Marshal(secs) return json.Marshal(secs)
} }
func (d *SecondsF64) UnmarshalBSONValue(bt byte, data []byte) error { func (d *SecondsF64) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if 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
@@ -66,11 +70,11 @@ func (d *SecondsF64) UnmarshalBSONValue(bt byte, data []byte) error {
*d = SecondsF64(0) *d = SecondsF64(0)
return nil return nil
} }
if bson.Type(bt) != bson.TypeDouble { if bt != bson.TypeDouble {
return errors.New(fmt.Sprintf("cannot unmarshal %v into SecondsF64", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into SecondsF64", bt))
} }
var secValue float64 var secValue float64
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&secValue) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&secValue)
if err != nil { if err != nil {
return err return err
} }
@@ -78,9 +82,40 @@ func (d *SecondsF64) UnmarshalBSONValue(bt byte, data []byte) error {
return nil return nil
} }
func (d SecondsF64) MarshalBSONValue() (byte, []byte, error) { func (d SecondsF64) MarshalBSONValue() (bsontype.Type, []byte, error) {
tp, data, err := bson.MarshalValue(d.Seconds()) return 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 {
+44 -9
View File
@@ -4,7 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/v2/bson" "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"
) )
@@ -62,8 +66,8 @@ func (t *UnixTime) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *UnixTime) UnmarshalBSONValue(bt byte, data []byte) error { func (t *UnixTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if 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
@@ -71,11 +75,11 @@ func (t *UnixTime) UnmarshalBSONValue(bt byte, data []byte) error {
*t = UnixTime{} *t = UnixTime{}
return nil return nil
} }
if bson.Type(bt) != bson.TypeDateTime { if bt != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixTime", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixTime", bt))
} }
var tt time.Time var tt time.Time
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
if err != nil { if err != nil {
return err return err
} }
@@ -83,9 +87,40 @@ func (t *UnixTime) UnmarshalBSONValue(bt byte, data []byte) error {
return nil return nil
} }
func (t UnixTime) MarshalBSONValue() (byte, []byte, error) { func (t UnixTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
tp, data, err := bson.MarshalValue(time.Time(t)) return 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 {
+44 -9
View File
@@ -4,7 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/v2/bson" "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"
) )
@@ -62,8 +66,8 @@ func (t *UnixMilliTime) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *UnixMilliTime) UnmarshalBSONValue(bt byte, data []byte) error { func (t *UnixMilliTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if 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
@@ -71,11 +75,11 @@ func (t *UnixMilliTime) UnmarshalBSONValue(bt byte, data []byte) error {
*t = UnixMilliTime{} *t = UnixMilliTime{}
return nil return nil
} }
if bson.Type(bt) != bson.TypeDateTime { if bt != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixMilliTime", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixMilliTime", bt))
} }
var tt time.Time var tt time.Time
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
if err != nil { if err != nil {
return err return err
} }
@@ -83,9 +87,40 @@ func (t *UnixMilliTime) UnmarshalBSONValue(bt byte, data []byte) error {
return nil return nil
} }
func (t UnixMilliTime) MarshalBSONValue() (byte, []byte, error) { func (t UnixMilliTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
tp, data, err := bson.MarshalValue(time.Time(t)) return 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 {
+44 -9
View File
@@ -4,7 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/v2/bson" "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"
) )
@@ -62,8 +66,8 @@ func (t *UnixNanoTime) UnmarshalText(data []byte) error {
return nil return nil
} }
func (t *UnixNanoTime) UnmarshalBSONValue(bt byte, data []byte) error { func (t *UnixNanoTime) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
if bson.Type(bt) == bson.TypeNull { if 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
@@ -71,11 +75,11 @@ func (t *UnixNanoTime) UnmarshalBSONValue(bt byte, data []byte) error {
*t = UnixNanoTime{} *t = UnixNanoTime{}
return nil return nil
} }
if bson.Type(bt) != bson.TypeDateTime { if bt != bson.TypeDateTime {
return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixNanoTime", bson.Type(bt))) return errors.New(fmt.Sprintf("cannot unmarshal %v into UnixNanoTime", bt))
} }
var tt time.Time var tt time.Time
err := bson.RawValue{Type: bson.Type(bt), Value: data}.Unmarshal(&tt) err := bson.RawValue{Type: bt, Value: data}.Unmarshal(&tt)
if err != nil { if err != nil {
return err return err
} }
@@ -83,9 +87,40 @@ func (t *UnixNanoTime) UnmarshalBSONValue(bt byte, data []byte) error {
return nil return nil
} }
func (t UnixNanoTime) MarshalBSONValue() (byte, []byte, error) { func (t UnixNanoTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
tp, data, err := bson.MarshalValue(time.Time(t)) return 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 {
+3 -2
View File
@@ -5,12 +5,13 @@ import (
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/v2/mongo" "go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/mongo"
"reflect" "reflect"
) )
type EntityID interface { type EntityID interface {
MarshalBSONValue() (byte, []byte, error) MarshalBSONValue() (bsontype.Type, []byte, error)
String() string String() string
} }
+1 -1
View File
@@ -2,8 +2,8 @@ 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/v2/mongo" import "go.mongodb.org/mongo-driver/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}
+5 -5
View File
@@ -2,13 +2,13 @@ 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.Lister[options.AggregateOptions]) ([]TData, error) { func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, opts ...*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 +29,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.Lister[options.AggregateOptions]) (*TData, error) { func (c *Coll[TData]) AggregateOneOpt(ctx context.Context, pipeline mongo.Pipeline, opts ...*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 +53,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.Lister[options.AggregateOptions]) (TData, error) { func (c *Coll[TData]) AggregateOne(ctx context.Context, pipeline mongo.Pipeline, opts ...*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))
+2 -2
View File
@@ -2,9 +2,9 @@ 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 {
+22 -9
View File
@@ -2,11 +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"
"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"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"iter" "iter"
) )
@@ -51,15 +51,13 @@ func (c *Coll[TData]) createFindQuery(ctx context.Context, filter bson.M, opts .
} }
} }
convOpts := make([]options.Lister[options.AggregateOptions], 0, len(opts)) convOpts := make([]*options.AggregateOptions, 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 {
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()
} }
if vConv != nil { convOpts = append(convOpts, vConv)
convOpts = append(convOpts, vConv)
}
} }
cursor, err := c.coll.Aggregate(ctx, pipeline, convOpts...) cursor, err := c.coll.Aggregate(ctx, pipeline, convOpts...)
@@ -139,7 +137,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.AggregateOptionsBuilder, error) { func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) {
if v == nil { if v == nil {
return nil, nil return nil, nil
} }
@@ -159,7 +157,7 @@ func convertFindOpt(v *options.FindOptions) (*options.AggregateOptionsBuilder, e
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()
@@ -167,18 +165,33 @@ func convertFindOpt(v *options.FindOptions) (*options.AggregateOptionsBuilder, e
if v.Hint != nil { if v.Hint != nil {
r.SetHint(v.Hint) 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 { if v.MaxAwaitTime != nil {
r.SetMaxAwaitTime(*v.MaxAwaitTime) 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 { if v.NoCursorTimeout != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'NoCursorTimeout' (cannot convert to AggregateOptions)").Build() 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 { if v.ReturnKey != nil {
return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'ReturnKey' (cannot convert to AggregateOptions)").Build() return nil, exerr.New(exerr.TypeMongoInvalidOpt, "Invalid option 'ReturnKey' (cannot convert to AggregateOptions)").Build()
} }
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)
} }
+2 -2
View File
@@ -3,10 +3,10 @@ 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) {
+2 -2
View File
@@ -4,8 +4,8 @@ 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/v2/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/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) {
+2 -2
View File
@@ -2,11 +2,11 @@ package wmo
import ( import (
"context" "context"
"go.mongodb.org/mongo-driver/bson"
"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"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"iter" "iter"
) )
+2 -2
View File
@@ -2,11 +2,11 @@ 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"
"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"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"iter" "iter"
) )
+3 -3
View File
@@ -2,10 +2,10 @@ 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/v2/bson" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/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 bson.ObjectID `bson:"_id"` ID primitive.ObjectID `bson:"_id"`
CDate time.Time `bson:"cdate"` CDate time.Time `bson:"cdate"`
} }
type TestInterface any type TestInterface any
+3 -4
View File
@@ -1,12 +1,11 @@
package wpdf package wpdf
import ( import (
"regexp"
"strconv"
"git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/langext"
"git.blackforestbytes.com/BlackForestBytes/goext/rext" "git.blackforestbytes.com/BlackForestBytes/goext/rext"
"regexp"
"strconv"
) )
// Column specifier: // Column specifier:
@@ -299,7 +298,7 @@ func (b *TableBuilder) calculateColumns() []float64 {
if remainingWidth > 0.01 { if remainingWidth > 0.01 {
rmSub := 0.0 rmSub := 0.0
for i := range columnDef { for i, _ := range columnDef {
if frColumnWeights[i] != 0 { if frColumnWeights[i] != 0 {
addW := (remainingWidth / float64(frColumnWidthCount)) * frColumnWeights[i] addW := (remainingWidth / float64(frColumnWidthCount)) * frColumnWeights[i]
rmSub += addW rmSub += addW