2023-06-06 21:18:40 +02:00
|
|
|
package wmo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-08 16:26:06 +02:00
|
|
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
2023-06-06 21:18:40 +02:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2023-06-08 16:26:06 +02:00
|
|
|
type EntityID interface {
|
|
|
|
MarshalBSONValue() (bsontype.Type, []byte, error)
|
|
|
|
String() string
|
|
|
|
}
|
2023-06-06 21:24:13 +02:00
|
|
|
|
2023-06-10 16:22:14 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-10 18:35:56 +02:00
|
|
|
type fullTypeRef struct {
|
2023-06-06 21:18:40 +02:00
|
|
|
IsPointer bool
|
|
|
|
Kind reflect.Kind
|
2023-06-07 16:58:17 +02:00
|
|
|
RealType reflect.Type
|
2023-06-06 21:18:40 +02:00
|
|
|
Type reflect.Type
|
|
|
|
UnderlyingType reflect.Type
|
|
|
|
Name string
|
|
|
|
Index []int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Coll[TData any] struct {
|
2023-06-10 18:35:56 +02:00
|
|
|
coll *mongo.Collection // internal mongo collection, access via Collection()
|
|
|
|
dataTypeMap map[string]fullTypeRef // list of TData fields (only if TData is not an interface)
|
|
|
|
implDataTypeMap map[reflect.Type]map[string]fullTypeRef // dynamic list of fields of TData implementations (only if TData is an interface)
|
|
|
|
customDecoder *func(ctx context.Context, dec Decodable) (TData, error) // custom decoding function (useful if TData is an interface)
|
|
|
|
isInterfaceDataType bool // true if TData is an interface (not a struct)
|
2023-06-06 21:18:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Coll[TData]) Collection() *mongo.Collection {
|
|
|
|
return c.coll
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Coll[TData]) Name() string {
|
|
|
|
return c.coll.Name()
|
|
|
|
}
|
|
|
|
|
2023-06-10 18:35:56 +02:00
|
|
|
func (c *Coll[TData]) WithDecodeFunc(cdf func(ctx context.Context, dec Decodable) (TData, error), example TData) *Coll[TData] {
|
|
|
|
|
|
|
|
c.EnsureInitializedReflection(example)
|
|
|
|
|
2023-06-10 16:22:14 +02:00
|
|
|
c.customDecoder = langext.Ptr(cdf)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-06-06 21:18:40 +02:00
|
|
|
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
|
|
|
|
}
|