v0.0.300 add custom unmarshal-hooks to wmo
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m45s

This commit is contained in:
2023-11-04 18:55:44 +01:00
parent 498785e213
commit 3a8baaa6d9
5 changed files with 50 additions and 15 deletions

View File

@@ -44,6 +44,7 @@ type Coll[TData any] struct {
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)
unmarshalHooks []func(d TData) TData // called for every object after unmarshalling
}
func (c *Coll[TData]) Collection() *mongo.Collection {
@@ -54,14 +55,6 @@ func (c *Coll[TData]) Name() string {
return c.coll.Name()
}
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
}
func (c *Coll[TData]) Indexes() mongo.IndexView {
return c.coll.Indexes()
}
@@ -74,6 +67,20 @@ func (c *Coll[TData]) Drop(ctx context.Context) error {
return nil
}
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
}
func (c *Coll[TData]) WithUnmarshalHook(fn func(d TData) TData) *Coll[TData] {
c.unmarshalHooks = append(c.unmarshalHooks, fn)
return c
}
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)