91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package wmo
|
|
|
|
import (
|
|
"context"
|
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
"reflect"
|
|
)
|
|
|
|
type EntityID interface {
|
|
MarshalBSONValue() (bsontype.Type, []byte, error)
|
|
String() string
|
|
}
|
|
|
|
type Decodable interface {
|
|
Decode(v any) error
|
|
}
|
|
|
|
type Cursorable interface {
|
|
Decode(v any) error
|
|
Err() error
|
|
Close(ctx context.Context) error
|
|
All(ctx context.Context, results any) error
|
|
RemainingBatchLength() int
|
|
Next(ctx context.Context) bool
|
|
}
|
|
|
|
type fullTypeRef[TData any] struct {
|
|
IsPointer bool
|
|
Kind reflect.Kind
|
|
RealType reflect.Type
|
|
Type reflect.Type
|
|
UnderlyingType reflect.Type
|
|
Name string
|
|
Index []int
|
|
}
|
|
|
|
type Coll[TData any] struct {
|
|
coll *mongo.Collection
|
|
dataTypeMap map[string]fullTypeRef[TData]
|
|
customDecoder *func(ctx context.Context, dec Decodable) (TData, error)
|
|
}
|
|
|
|
func (c *Coll[TData]) Collection() *mongo.Collection {
|
|
return c.coll
|
|
}
|
|
|
|
func (c *Coll[TData]) Name() string {
|
|
return c.coll.Name()
|
|
}
|
|
|
|
func (c *Coll[TData]) WithDecodeFunc(cdf func(ctx context.Context, dec Decodable) (TData, error)) *Coll[TData] {
|
|
c.customDecoder = langext.Ptr(cdf)
|
|
return c
|
|
}
|
|
|
|
func (c *Coll[TData]) Indexes() mongo.IndexView {
|
|
return c.coll.Indexes()
|
|
}
|
|
|
|
func (c *Coll[TData]) Drop(ctx context.Context) error {
|
|
return c.coll.Drop(ctx)
|
|
}
|
|
|
|
func (c *Coll[TData]) createToken(fieldPrimary string, dirPrimary ct.SortDirection, fieldSecondary *string, dirSecondary *ct.SortDirection, lastEntity TData, pageSize *int) (ct.CursorToken, error) {
|
|
|
|
valuePrimary, err := c.getFieldValueAsTokenString(lastEntity, fieldPrimary)
|
|
if err != nil {
|
|
return ct.CursorToken{}, err
|
|
}
|
|
|
|
valueSeconary := ""
|
|
if fieldSecondary != nil && dirSecondary != nil {
|
|
valueSeconary, err = c.getFieldValueAsTokenString(lastEntity, *fieldSecondary)
|
|
if err != nil {
|
|
return ct.CursorToken{}, err
|
|
}
|
|
}
|
|
|
|
return ct.CursorToken{
|
|
Mode: ct.CTMNormal,
|
|
ValuePrimary: valuePrimary,
|
|
ValueSecondary: valueSeconary,
|
|
Direction: dirPrimary,
|
|
PageSize: langext.Coalesce(pageSize, 0),
|
|
Extra: ct.Extra{},
|
|
}, nil
|
|
}
|