From 727e3e68116346ed16e80eeb78165b3f43f93779 Mon Sep 17 00:00:00 2001 From: Gabriel Cataldo Date: Mon, 5 Feb 2024 16:28:37 -0300 Subject: [PATCH] Bug fix find pageable input ref --- README.md | 2 +- mongo/main_test.go | 10 +++++----- mongo/page.go | 2 +- mongo/template.go | 5 ++++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4ae0468..06dd42e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ MongoDB Template -[![Project status](https://img.shields.io/badge/version-v1.1.9-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.1.9) +[![Project status](https://img.shields.io/badge/version-v1.2.0-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.2.0) [![Go Report Card](https://goreportcard.com/badge/github.com/GabrielHCataldo/go-mongo-template)](https://goreportcard.com/report/github.com/GabrielHCataldo/go-mongo-template) [![Coverage Status](https://coveralls.io/repos/GabrielHCataldo/go-mongo-template/badge.svg?branch=main&service=github)](https://coveralls.io/github/GabrielHCataldo/go-mongo?branch=main) [![Open Source Helpers](https://www.codetriage.com/gabrielhcataldo/go-mongo-template/badges/users.svg)](https://www.codetriage.com/gabrielhcataldo/go-mongo) diff --git a/mongo/main_test.go b/mongo/main_test.go index 0780964..2540cff 100644 --- a/mongo/main_test.go +++ b/mongo/main_test.go @@ -1163,7 +1163,7 @@ func initListTestFindPageable() []testFindPageable { pageInput: PageInput{ Page: 0, PageSize: 10, - Ref: []testStruct{}, + Ref: testStruct{}, Sort: bson.M{"createdAt": SortAsc}, }, option: initOptionFindPageable(), @@ -1175,7 +1175,7 @@ func initListTestFindPageable() []testFindPageable { pageInput: PageInput{ Page: 0, PageSize: 10, - Ref: []testStruct{}, + Ref: testStruct{}, Sort: nil, }, option: initOptionFindPageable(), @@ -1187,7 +1187,7 @@ func initListTestFindPageable() []testFindPageable { pageInput: PageInput{ Page: 0, PageSize: 10, - Ref: []testStruct{}, + Ref: testStruct{}, Sort: nil, }, option: initOptionFindPageable(). @@ -1205,7 +1205,7 @@ func initListTestFindPageable() []testFindPageable { pageInput: PageInput{ Page: 0, PageSize: 10, - Ref: []testInvalidStruct{}, + Ref: testInvalidStruct{}, Sort: nil, }, option: initOptionFindPageable(), @@ -1218,7 +1218,7 @@ func initListTestFindPageable() []testFindPageable { pageInput: PageInput{ Page: 0, PageSize: 10, - Ref: &testStruct{}, + Ref: []testStruct{}, Sort: nil, }, option: initOptionFindPageable(), diff --git a/mongo/page.go b/mongo/page.go index 501a195..617481c 100644 --- a/mongo/page.go +++ b/mongo/page.go @@ -12,7 +12,7 @@ type PageInput struct { Page int64 // PageSize page size (required) PageSize int64 - // Ref slice of the struct reference contained database and collection configured + // Ref struct reference contained database and collection configured Ref any // Sort value sort to result Sort any diff --git a/mongo/template.go b/mongo/template.go index b43fa0c..b814aec 100644 --- a/mongo/template.go +++ b/mongo/template.go @@ -515,6 +515,9 @@ func (t *Template) FindAll(ctx context.Context, dest any, opts ...*option.Find) // For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/. func (t *Template) FindPageable(ctx context.Context, filter any, input PageInput, opts ...*option.FindPageable) ( *PageResult, error) { + if helper.IsNotStruct(input.Ref) { + return nil, errors.NewSkipCaller(2, "mongo: input.Ref need to be structure") + } _, collection, err := t.getMongoInfosByAny(2, input.Ref) if helper.IsNotNil(err) { return nil, err @@ -543,7 +546,7 @@ func (t *Template) FindPageable(ctx context.Context, filter any, input PageInput }) defer t.closeCursor(ctx, cursor) if helper.IsNil(err) { - dest := reflect.MakeSlice(reflect.TypeOf(input.Ref), 0, 0).Interface() + dest := reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(input.Ref)), 0, 0).Interface() err = cursor.All(ctx, &dest) if helper.IsNil(err) { countTotal, _ := collection.CountDocuments(ctx, filter)