From b613b122e35426db262fe69ad692acc50899a65c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 10 Jun 2023 16:22:14 +0200 Subject: [PATCH] v0.0.157 --- goextVersion.go | 4 ++-- wmo/collection.go | 23 ++++++++++++++++-- wmo/decoding.go | 54 +++++++++++++++++++++++++++++++++++++++++++ wmo/queryAggregate.go | 3 +-- wmo/queryFind.go | 29 ++++++++--------------- wmo/queryInsert.go | 9 ++------ wmo/queryList.go | 11 ++++++++- wmo/queryUpdate.go | 18 ++++----------- 8 files changed, 103 insertions(+), 48 deletions(-) create mode 100644 wmo/decoding.go diff --git a/goextVersion.go b/goextVersion.go index 497c764..be0826d 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.156" +const GoextVersion = "0.0.157" -const GoextVersionTimestamp = "2023-06-10T00:19:17+0200" +const GoextVersionTimestamp = "2023-06-10T16:22:14+0200" diff --git a/wmo/collection.go b/wmo/collection.go index cfcfaf9..31a8a82 100644 --- a/wmo/collection.go +++ b/wmo/collection.go @@ -14,6 +14,19 @@ type EntityID interface { String() string } +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 +} + type fullTypeRef[TData any] struct { IsPointer bool Kind reflect.Kind @@ -25,8 +38,9 @@ type fullTypeRef[TData any] struct { } type Coll[TData any] struct { - coll *mongo.Collection - dataTypeMap map[string]fullTypeRef[TData] + coll *mongo.Collection + dataTypeMap map[string]fullTypeRef[TData] + customDecoder *func(ctx context.Context, dec Decodable) (TData, error) } func (c *Coll[TData]) Collection() *mongo.Collection { @@ -37,6 +51,11 @@ 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] { + c.customDecoder = langext.Ptr(cdf) + return c +} + func (c *Coll[TData]) Indexes() mongo.IndexView { return c.coll.Indexes() } diff --git a/wmo/decoding.go b/wmo/decoding.go new file mode 100644 index 0000000..10d5923 --- /dev/null +++ b/wmo/decoding.go @@ -0,0 +1,54 @@ +package wmo + +import ( + "context" +) + +func (c *Coll[TData]) decodeSingle(ctx context.Context, dec Decodable) (TData, error) { + if c.customDecoder != nil { + + return (*c.customDecoder)(ctx, dec) + + } else { + + var res TData + + err := dec.Decode(&res) + if err != nil { + return *new(TData), err + } + + return res, nil + + } +} + +func (c *Coll[TData]) decodeAll(ctx context.Context, cursor Cursorable) ([]TData, error) { + if c.customDecoder != nil { + + res := make([]TData, 0, cursor.RemainingBatchLength()) + + for cursor.Next(ctx) { + entry, err := (*c.customDecoder)(ctx, cursor) + if err != nil { + return nil, err + } + res = append(res, entry) + } + + return res, nil + + } else { + + res := make([]TData, 0, cursor.RemainingBatchLength()) + + err := cursor.All(ctx, &res) + if err != nil { + return nil, err + } + + return res, nil + + } + +} diff --git a/wmo/queryAggregate.go b/wmo/queryAggregate.go index 9572c99..e900751 100644 --- a/wmo/queryAggregate.go +++ b/wmo/queryAggregate.go @@ -12,8 +12,7 @@ func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, op return nil, err } - res := make([]TData, 0, cursor.RemainingBatchLength()) - err = cursor.All(ctx, &res) + res, err := c.decodeAll(ctx, cursor) if err != nil { return nil, err } diff --git a/wmo/queryFind.go b/wmo/queryFind.go index 0b92306..6e35a4d 100644 --- a/wmo/queryFind.go +++ b/wmo/queryFind.go @@ -8,20 +8,15 @@ import ( ) func (c *Coll[TData]) FindOne(ctx context.Context, filter bson.M) (TData, error) { - var res TData + mongoRes := c.coll.FindOne(ctx, filter) - err := c.coll.FindOne(ctx, filter).Decode(&res) - if err != nil { - return *new(TData), err - } - - return res, nil + return c.decodeSingle(ctx, mongoRes) } func (c *Coll[TData]) FindOneOpt(ctx context.Context, filter bson.M) (*TData, error) { - var res TData + mongoRes := c.coll.FindOne(ctx, filter) - err := c.coll.FindOne(ctx, filter).Decode(&res) + res, err := c.decodeSingle(ctx, mongoRes) if err == mongo.ErrNoDocuments { return nil, nil } @@ -33,20 +28,15 @@ func (c *Coll[TData]) FindOneOpt(ctx context.Context, filter bson.M) (*TData, er } func (c *Coll[TData]) FindOneByID(ctx context.Context, id EntityID) (TData, error) { - var res TData + mongoRes := c.coll.FindOne(ctx, bson.M{"_id": id}) - err := c.coll.FindOne(ctx, bson.M{"_id": id}).Decode(&res) - if err != nil { - return *new(TData), err - } - - return res, nil + return c.decodeSingle(ctx, mongoRes) } func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id EntityID) (*TData, error) { - var res TData + mongoRes := c.coll.FindOne(ctx, bson.M{"_id": id}) - err := c.coll.FindOne(ctx, bson.M{"_id": id}).Decode(&res) + res, err := c.decodeSingle(ctx, mongoRes) if err == mongo.ErrNoDocuments { return nil, nil } @@ -63,8 +53,7 @@ func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options. return nil, err } - res := make([]TData, 0, cursor.RemainingBatchLength()) - err = cursor.All(ctx, &res) + res, err := c.decodeAll(ctx, cursor) if err != nil { return nil, err } diff --git a/wmo/queryInsert.go b/wmo/queryInsert.go index ab246c1..8ea13ca 100644 --- a/wmo/queryInsert.go +++ b/wmo/queryInsert.go @@ -11,12 +11,7 @@ func (c *Coll[TData]) InsertOne(ctx context.Context, valueIn TData) (TData, erro return *new(TData), err } - var res TData + mongoRes := c.coll.FindOne(ctx, bson.M{"_id": insRes.InsertedID}) - err = c.coll.FindOne(ctx, bson.M{"_id": insRes.InsertedID}).Decode(&res) - if err != nil { - return *new(TData), err - } - - return res, nil + return c.decodeSingle(ctx, mongoRes) } diff --git a/wmo/queryList.go b/wmo/queryList.go index bbb9c84..28c7888 100644 --- a/wmo/queryList.go +++ b/wmo/queryList.go @@ -37,10 +37,19 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, return nil, ct.CursorToken{}, err } + // fast branch + if pageSize == nil { + entries, err := c.decodeAll(ctx, cursor) + if err != nil { + return nil, ct.CursorToken{}, err + } + return entries, ct.End(), nil + } + entities := make([]TData, 0, cursor.RemainingBatchLength()) for (pageSize == nil || len(entities) != *pageSize) && cursor.Next(ctx) { var entry TData - err = cursor.Decode(&entry) + entry, err = c.decodeSingle(ctx, cursor) if err != nil { return nil, ct.CursorToken{}, err } diff --git a/wmo/queryUpdate.go b/wmo/queryUpdate.go index 6226ff8..b004335 100644 --- a/wmo/queryUpdate.go +++ b/wmo/queryUpdate.go @@ -8,14 +8,9 @@ import ( ) func (c *Coll[TData]) FindOneAndUpdate(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (TData, error) { - var res TData + mongoRes := c.coll.FindOneAndUpdate(ctx, filterQuery, updateQuery, options.FindOneAndUpdate().SetReturnDocument(options.After)) - err := c.coll.FindOneAndUpdate(ctx, filterQuery, updateQuery, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&res) - if err != nil { - return *new(TData), err - } - - return res, nil + return c.decodeSingle(ctx, mongoRes) } func (c *Coll[TData]) UpdateOne(ctx context.Context, filterQuery bson.M, updateQuery bson.M) error { @@ -55,12 +50,7 @@ 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) { - var res TData + mongoRes := c.coll.FindOneAndUpdate(ctx, filterQuery, bson.M{"$set": value}, options.FindOneAndUpdate().SetReturnDocument(options.After)) - err := c.coll.FindOneAndUpdate(ctx, filterQuery, bson.M{"$set": value}, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&res) - if err != nil { - return *new(TData), err - } - - return res, nil + return c.decodeSingle(ctx, mongoRes) }