v0.0.448 wmo marshalHook
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 25s
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 25s
This commit is contained in:
parent
631006a4e1
commit
fe4cdc48af
@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.447"
|
||||
const GoextVersion = "0.0.448"
|
||||
|
||||
const GoextVersionTimestamp = "2024-05-10T21:33:01+0200"
|
||||
const GoextVersionTimestamp = "2024-05-12T16:45:45+0200"
|
||||
|
@ -52,6 +52,7 @@ type Coll[TData any] struct {
|
||||
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
|
||||
marshalHooks []func(d TData) TData // called for every object before marshalling
|
||||
extraModPipeline []func(ctx context.Context) mongo.Pipeline // appended to pipelines after filter/limit/skip/sort, used for $lookup, $set, $unset, $project, etc
|
||||
}
|
||||
|
||||
@ -83,18 +84,32 @@ func (c *Coll[TData]) WithDecodeFunc(cdf func(ctx context.Context, dec Decodable
|
||||
return c
|
||||
}
|
||||
|
||||
// WithUnmarshalHook
|
||||
// function that is called for every object after reading from DB
|
||||
func (c *Coll[TData]) WithUnmarshalHook(fn func(d TData) TData) *Coll[TData] {
|
||||
c.unmarshalHooks = append(c.unmarshalHooks, fn)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// WithMarshalHook
|
||||
// function that is called for every object before writing to DB
|
||||
func (c *Coll[TData]) WithMarshalHook(fn func(d TData) TData) *Coll[TData] {
|
||||
c.extraModPipeline = append(c.marshalHooks, fn)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// WithModifyingPipeline
|
||||
// pipeline that is appended to all read operations (after filtering)
|
||||
func (c *Coll[TData]) WithModifyingPipeline(p mongo.Pipeline) *Coll[TData] {
|
||||
c.extraModPipeline = append(c.extraModPipeline, func(ctx context.Context) mongo.Pipeline { return p })
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// WithModifyingPipelineFunc
|
||||
// pipeline that is appended to all read operations (after filtering)
|
||||
func (c *Coll[TData]) WithModifyingPipelineFunc(fn func(ctx context.Context) mongo.Pipeline) *Coll[TData] {
|
||||
c.extraModPipeline = append(c.extraModPipeline, fn)
|
||||
|
||||
|
@ -9,6 +9,10 @@ import (
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, error) {
|
||||
for _, hook := range c.marshalHooks {
|
||||
valueIn = hook(valueIn)
|
||||
}
|
||||
|
||||
insRes, err := c.coll.InsertOne(ctx, valueIn)
|
||||
if err != nil {
|
||||
return *new(TData), exerr.Wrap(err, "mongo-query[insert-one] failed").Str("collection", c.Name()).Build()
|
||||
@ -36,6 +40,12 @@ func (c *Coll[TData]) InsertOneUnchecked(ctx context.Context, valueIn any) (TDat
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) InsertMany(ctx context.Context, valueIn []TData) (*mongo.InsertManyResult, error) {
|
||||
for _, hook := range c.marshalHooks {
|
||||
for i := 0; i < len(valueIn); i++ {
|
||||
valueIn[i] = hook(valueIn[i])
|
||||
}
|
||||
}
|
||||
|
||||
insRes, err := c.coll.InsertMany(ctx, langext.ArrayToInterface(valueIn))
|
||||
if err != nil {
|
||||
return nil, exerr.Wrap(err, "mongo-query[insert-many] failed").Int("len(valueIn)", len(valueIn)).Str("collection", c.Name()).Build()
|
||||
|
@ -61,6 +61,10 @@ func (c *Coll[TData]) UpdateMany(ctx context.Context, filterQuery bson.M, update
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) ReplaceOne(ctx context.Context, filterQuery bson.M, value TData) error {
|
||||
for _, hook := range c.marshalHooks {
|
||||
value = hook(value)
|
||||
}
|
||||
|
||||
_, err := c.coll.UpdateOne(ctx, filterQuery, bson.M{"$set": value})
|
||||
if err != nil {
|
||||
return exerr.Wrap(err, "mongo-query[replace-one] failed").
|
||||
@ -73,6 +77,10 @@ func (c *Coll[TData]) ReplaceOne(ctx context.Context, filterQuery bson.M, value
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) FindOneAndReplace(ctx context.Context, filterQuery bson.M, value TData) (TData, error) {
|
||||
for _, hook := range c.marshalHooks {
|
||||
value = hook(value)
|
||||
}
|
||||
|
||||
mongoRes := c.coll.FindOneAndReplace(ctx, filterQuery, value, options.FindOneAndReplace().SetReturnDocument(options.After))
|
||||
if err := mongoRes.Err(); err != nil {
|
||||
return *new(TData), exerr.Wrap(err, "mongo-query[find-one-and-update] failed").
|
||||
|
Loading…
Reference in New Issue
Block a user