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)

View File

@@ -12,6 +12,11 @@ func (c *Coll[TData]) decodeSingle(ctx context.Context, dec Decodable) (TData, e
if err != nil {
return *new(TData), exerr.Wrap(err, "failed to decode single entity with custom-decoder").Type("decoder", *c.customDecoder).Build()
}
for _, hook := range c.unmarshalHooks {
res = hook(res)
}
return res, nil
} else {
@@ -23,6 +28,10 @@ func (c *Coll[TData]) decodeSingle(ctx context.Context, dec Decodable) (TData, e
return *new(TData), exerr.Wrap(err, "failed to decode single entity").Type("target-type", res).Build()
}
for _, hook := range c.unmarshalHooks {
res = hook(res)
}
return res, nil
}
@@ -38,6 +47,9 @@ func (c *Coll[TData]) decodeAll(ctx context.Context, cursor Cursorable) ([]TData
if err != nil {
return nil, exerr.Wrap(err, "failed to decode entity with custom-decoder").Type("decoder", *c.customDecoder).Build()
}
for _, hook := range c.unmarshalHooks {
entry = hook(entry)
}
res = append(res, entry)
}
@@ -52,6 +64,12 @@ func (c *Coll[TData]) decodeAll(ctx context.Context, cursor Cursorable) ([]TData
return nil, exerr.Wrap(err, "failed to batch-decode entity").Type("target-type", res).Build()
}
for i := 0; i < len(res); i++ {
for _, hook := range c.unmarshalHooks {
res[i] = hook(res[i])
}
}
return res, nil
}