This commit is contained in:
2023-06-10 18:35:56 +02:00
parent a30da61419
commit ceff0161c6
5 changed files with 91 additions and 16 deletions

View File

@@ -27,7 +27,7 @@ type Cursorable interface {
Next(ctx context.Context) bool
}
type fullTypeRef[TData any] struct {
type fullTypeRef struct {
IsPointer bool
Kind reflect.Kind
RealType reflect.Type
@@ -38,9 +38,11 @@ type fullTypeRef[TData any] struct {
}
type Coll[TData any] struct {
coll *mongo.Collection
dataTypeMap map[string]fullTypeRef[TData]
customDecoder *func(ctx context.Context, dec Decodable) (TData, error)
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)
}
func (c *Coll[TData]) Collection() *mongo.Collection {
@@ -51,7 +53,10 @@ 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] {
func (c *Coll[TData]) WithDecodeFunc(cdf func(ctx context.Context, dec Decodable) (TData, error), example TData) *Coll[TData] {
c.EnsureInitializedReflection(example)
c.customDecoder = langext.Ptr(cdf)
return c
}