Skip to content

Commit

Permalink
Added a custom type to the pagination content to facilitate conversio…
Browse files Browse the repository at this point in the history
…n to the desired slice and item type.
  • Loading branch information
Gabriel Cataldo committed Jan 30, 2024
1 parent 3d396ad commit 8eb9c9e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 24 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MongoDB Template
<!--suppress ALL -->
<img align="right" src="gopher-mongo.png" alt="">

[![Project status](https://img.shields.io/badge/version-v1.1.4-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.1.4)
[![Project status](https://img.shields.io/badge/version-v1.1.5-vividgreen.svg)](https://github.com/GabrielHCataldo/go-mongo-template/releases/tag/v1.1.5)
[![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)
Expand Down Expand Up @@ -129,7 +129,7 @@ func main() {

Output:

[INFO 2023/12/22 13:02:07] main.go:35: document inserted successfully: {"id":[101,133,178,255,197,19,21,77,11,38,26,152],"random":6628457526937947134,"name":"Foo Bar","birthDate":"1999-01-21T02:00:00Z","emails":["[email protected]","[email protected]"],"balance":190.12,"createdAt":"2023-12-22T16:02:07.322Z"}
[INFO 2023/12/22 13:02:07] main.go:35: document inserted successfully: {"id":"65b8a8b753748d924631520c","random":6628457526937947134,"name":"Foo Bar","birthDate":"1999-01-21T02:00:00Z","emails":["[email protected]","[email protected]"],"balance":190.12,"createdAt":"2023-12-22T16:02:07.322Z"}

We are able to insert multiple documents, remembering that if no ID is provided, and if you have
configured a field with annotation **bson** _id and the type is **primitive.ObjectId** we will fill in the
Expand Down Expand Up @@ -190,7 +190,7 @@ func main() {

Output:

[INFO 2023/12/22 16:11:33] main.go:46: document inserted successfully: [{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T19:11:33.206Z","emails":["[email protected]","[email protected]"],"id":[101,133,223,101,71,229,174,223,115,181,143,210],"name":"Foo Bar","random":8094092400336225232},{"balance":290.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T19:11:33.206Z","emails":["[email protected]","[email protected]"],"id":[101,133,223,101,71,229,174,223,115,181,143,211],"name":"Foo Bar 2","random":4318945546650338065}]
[INFO 2023/12/22 16:11:33] main.go:46: document inserted successfully: [{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T19:11:33.206Z","emails":["[email protected]","[email protected]"],"id":"65b8a8b753748d924631520b","name":"Foo Bar","random":8094092400336225232},{"balance":290.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T19:11:33.206Z","emails":["[email protected]","[email protected]"],"id":"65b8a8b753748d924631520f","name":"Foo Bar 2","random":4318945546650338065}]

For more insert examples visit [link](https://github/GabrielHCataldo/go-mongo-template/blob/main/_example/insert/main).

Expand Down Expand Up @@ -538,7 +538,7 @@ func main() {

Output:

[INFO 2023/12/22 17:02:22] main.go:28: find all documents successfully: [{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T18:53:26.695Z","emails":["[email protected]","[email protected]"],"id":[101,133,219,38,99,62,34,92,190,173,245,83],"name":"Foo Bar Updated","random":2576121145493409319},{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T19:11:33.206Z","emails":["[email protected]","[email protected]"],"id":[101,133,223,101,71,229,174,223,115,181,143,210],"name":"Foo Bar","random":8094092400336225232},{"balance":290.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T19:11:33.206Z","emails":["[email protected]","[email protected]"],"id":[101,133,223,101,71,229,174,223,115,181,143,211],"name":"Foo Bar 2","random":4318945546650338065}]
[INFO 2023/12/22 17:02:22] main.go:28: find all documents successfully: [{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T18:53:26.695Z","emails":["[email protected]","[email protected]"],"id":"65b8a8b753748d924631520g","name":"Foo Bar Updated","random":2576121145493409319},{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T19:11:33.206Z","emails":["[email protected]","[email protected]"],"id":"65b8a8b753748d924631520j","name":"Foo Bar","random":8094092400336225232},{"balance":290.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T19:11:33.206Z","emails":["[email protected]","[email protected]"],"id":"65b8a8b753748d924631520k","name":"Foo Bar 2","random":4318945546650338065}]

Be able to page in a simple and intuitive way, see:

Expand Down Expand Up @@ -576,14 +576,18 @@ func main() {
if helper.IsNotNil(err) {
logger.Error("error find pageable documents:", err)
} else {
var dest []test
_ = pageOutput.Content.Parse(&dest)
logger.Info("find pageable documents successfully:", pageOutput)
logger.Info("find pageable get first value:", dest[0])
}
}
```

Output:

[INFO 2023/12/22 18:04:54] main.go:33: find pageable documents successfully: {"page":0,"pageSize":10,"pageTotal":2,"totalElements":16,"content":[{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T21:03:03.589Z","emails":["[email protected]","[email protected]"],"id":[101,133,249,135,99,77,8,140,0,92,121,166],"name":"Foo Bar","random":9062939652073059162},{"balance":290.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T21:03:03.288Z","emails":["[email protected]","[email protected]"],"id":[101,133,249,135,99,77,8,140,0,92,121,164],"name":"Foo Bar 2","random":3964041099041384637},{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T21:03:03.288Z","emails":["[email protected]","[email protected]"],"id":[101,133,249,135,99,77,8,140,0,92,121,163],"name":"Foo Bar","random":7118470983031370710},{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T21:03:02.895Z","emails":["[email protected]","[email protected]"],"id":[101,133,249,134,99,77,8,140,0,92,121,161],"name":"Foo Bar","random":685662021554519416},{"balance":290.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T21:03:02.032Z","emails":["[email protected]","[email protected]"],"id":[101,133,249,134,109,6,181,94,237,141,156,103],"name":"Foo Bar 2","random":4825201869427500545},{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T21:03:02.032Z","emails":["[email protected]","[email protected]"],"id":[101,133,249,134,109,6,181,94,237,141,156,102],"name":"Foo Bar","random":5424075018439786230},{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T21:03:01.71Z","emails":["[email protected]","[email protected]"],"id":[101,133,249,133,109,6,181,94,237,141,156,100],"name":"Foo Bar","random":4901802038316432856},{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T20:46:48.598Z","emails":["[email protected]","[email protected]"],"id":[101,133,245,184,253,143,169,125,86,36,25,247],"name":"Foo Bar","random":5045147651911492796},{"balance":290.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T20:46:48.262Z","emails":["[email protected]","[email protected]"],"id":[101,133,245,184,253,143,169,125,86,36,25,245],"name":"Foo Bar 2","random":5267773685750720182},{"balance":190.12,"birthDate":"1999-01-21T02:00:00Z","createdAt":"2023-12-22T20:46:48.262Z","emails":["[email protected]","[email protected]"],"id":[101,133,245,184,253,143,169,125,86,36,25,244],"name":"Foo Bar","random":6973387066252926888}],"lastQueryAt":"2023-12-22T21:04:54Z"}
[INFO 2024/01/30 04:57:45] main.go:33: find pageable documents successfully: {"page":null,"pageSize":"10","pageTotal":"1","totalElements":"2","content":[{"balance":"190.12","birthDate":"1999-01-21T02:00:00Z","createdAt":"2024-01-30T07:57:35.865Z","emails":["[email protected]","[email protected]"],"id":"65b8abef44f575c583b91a81","name":"Foo Bar","random":"5208816839613347000"},{"balance":"290.12","birthDate":"1999-01-21T02:00:00Z","createdAt":"2024-01-30T07:57:35.865Z","emails":["[email protected]","[email protected]"],"id":"65b8abef44f575c583b91a82","name":"Foo Bar 2","random":"1873557180388720600"}],"lastQueryAt":"2024-01-30T07:57:45.444808Z"}
[INFO 2024/01/30 04:57:45] main.go:34: find pageable get first value: {"id":"65b8abef44f575c583b91a81","random":"5208816839613347000","name":"Foo Bar","birthDate":"1999-01-21T02:00:00Z","emails":["[email protected]","[email protected]"],"balance":"190.12","createdAt":"2024-01-30T07:57:35.865Z"}

Also obtain a single document by id, see below:

Expand Down Expand Up @@ -624,7 +628,7 @@ func main() {

Output:

[INFO 2023/12/22 17:14:47] main.go:29: find by id document successfully: {"id":[101,133,219,38,99,62,34,92,190,173,245,83],"random":2576121145493409319,"name":"Foo Bar Updated","birthDate":"1999-01-21T02:00:00Z","emails":["[email protected]","[email protected]"],"balance":190.12,"createdAt":"2023-12-22T18:53:26.695Z"}
[INFO 2023/12/22 17:14:47] main.go:29: find by id document successfully: {"id":"65b8a8b753748d924631521z","random":2576121145493409319,"name":"Foo Bar Updated","birthDate":"1999-01-21T02:00:00Z","emails":["[email protected]","[email protected]"],"balance":190.12,"createdAt":"2023-12-22T18:53:26.695Z"}

For more find examples visit [link](https://github/GabrielHCataldo/go-mongo-template/blob/main/_example/find/main).

Expand Down
3 changes: 3 additions & 0 deletions _example/find/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ func findPageable() {
if helper.IsNotNil(err) {
logger.Error("error find pageable documents:", err)
} else {
var dest []test
_ = pageOutput.Content.Parse(&dest)
logger.Info("find pageable documents successfully:", pageOutput)
logger.Info("find pageable get first value:", dest[0])
}
}

Expand Down
4 changes: 2 additions & 2 deletions _example/insert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type test struct {
}

func main() {
insertOne()
//insertMany()
//insertOne()
insertMany()
//insertOneManualCloseSession()
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ go 1.21.3
require (
github.com/GabrielHCataldo/go-errors v1.1.1
github.com/GabrielHCataldo/go-helper v1.3.5
github.com/GabrielHCataldo/go-logger v1.2.0
github.com/GabrielHCataldo/go-logger v1.2.2
github.com/iancoleman/orderedmap v0.3.0
go.mongodb.org/mongo-driver v1.13.1
)

Expand All @@ -16,7 +17,6 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/klassmann/cpfcnpj v0.0.0-20200907140233-a595c5fd8de1 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/leekchan/accounting v1.0.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ github.com/GabrielHCataldo/go-errors v1.1.1 h1:cbEJIfWRk44843wE8eVpyvr9F7zOn/AHD
github.com/GabrielHCataldo/go-errors v1.1.1/go.mod h1:rrwIP14EEGJ0N8BUpxuwHNM8auO5ZsVj9jGEj442ibo=
github.com/GabrielHCataldo/go-helper v1.3.5 h1:H8BOYT5mkfMExWuyzp4guetI8cjQ7idyhyCsJsym6iw=
github.com/GabrielHCataldo/go-helper v1.3.5/go.mod h1:+whI36aUg1BfvLpZWJ8j81+d5iPuxl58ApOJqTIliCU=
github.com/GabrielHCataldo/go-logger v1.2.0 h1:fGdlkx+ihU7nhNYTvi/KOlLb2WEHmJrCuIFU9/zGQ1g=
github.com/GabrielHCataldo/go-logger v1.2.0/go.mod h1:Q7+M9m9gu1MtgGiFkoYOHtobLLM/kJCPZVhszIJtsJ0=
github.com/GabrielHCataldo/go-logger v1.2.1 h1:5pKq4449safXvGR5rYkq7Op2lGgqeDR4B87MTkwZaGY=
github.com/GabrielHCataldo/go-logger v1.2.1/go.mod h1:Q7+M9m9gu1MtgGiFkoYOHtobLLM/kJCPZVhszIJtsJ0=
github.com/GabrielHCataldo/go-logger v1.2.2 h1:+CFRBha2Pr7mwHpQrPI+4i+XX8J2WSFo7bNotZg7E8U=
github.com/GabrielHCataldo/go-logger v1.2.2/go.mod h1:Q7+M9m9gu1MtgGiFkoYOHtobLLM/kJCPZVhszIJtsJ0=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
33 changes: 26 additions & 7 deletions mongo/page.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mongo

import (
"github.com/GabrielHCataldo/go-errors/errors"
"github.com/GabrielHCataldo/go-helper/helper"
"math"
"time"
Expand All @@ -14,12 +15,28 @@ type PageInput struct {
}

type PageResult struct {
Page int64 `json:"page"`
PageSize int64 `json:"pageSize"`
PageTotal int64 `json:"pageTotal"`
TotalElements int64 `json:"totalElements"`
Content any `json:"content,omitempty"`
LastQueryAt time.Time `json:"lastQueryAt,omitempty"`
Page int64 `json:"page"`
PageSize int64 `json:"pageSize"`
PageTotal int64 `json:"pageTotal"`
TotalElements int64 `json:"totalElements"`
Content pageContent `json:"content,omitempty"`
LastQueryAt time.Time `json:"lastQueryAt,omitempty"`
}

type pageContent []pageItemContent
type pageItemContent map[string]any

func (p pageContent) Parse(dest any) error {
if helper.IsNotSlice(dest) {
return errors.NewSkipCaller(2, "mongo: dest is not a slice or array")
}
err := helper.ConvertToDest(p, dest)
return errors.NewSkipCaller(2, err)
}

func (p pageItemContent) Parse(dest any) error {
err := helper.ConvertToDest(p, dest)
return errors.NewSkipCaller(2, err)
}

func newPageResult(pageInput PageInput, result any, countTotal int64) *PageResult {
Expand All @@ -30,12 +47,14 @@ func newPageResult(pageInput PageInput, result any, countTotal int64) *PageResul
fPageTotal := math.Ceil(float64(countTotal) / float64(pageInput.PageSize))
pageTotal := helper.MinInt(int(fPageTotal), minPageTotal)
lastQueryAt := time.Now().UTC()
var content pageContent
_ = helper.ConvertToDest(result, &content)
return &PageResult{
Page: pageInput.Page,
PageSize: pageInput.PageSize,
PageTotal: int64(pageTotal),
TotalElements: countTotal,
Content: result,
Content: content,
LastQueryAt: lastQueryAt,
}
}
10 changes: 9 additions & 1 deletion mongo/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,15 @@ func TestTemplateFindPageable(t *testing.T) {
} else if helper.IsNotNil(err) {
t.Log("err expected:", err)
} else {
logger.Info("result pageable:", v)
var destContent []testStruct
_ = v.Content.Parse(destContent)
_ = v.Content.Parse(&destContent)
logger.Info("result pageable:", destContent)
if helper.IsGreaterThan(len(v.Content), 0) {
var destItemContent testStruct
_ = v.Content[0].Parse(&destItemContent)
logger.Info("result item content:", destItemContent)
}
}
})
}
Expand Down
7 changes: 3 additions & 4 deletions mongo/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ type EventHandler func(ctx *EventContext)

// ParseToStruct convert Event.FullDocument to struct
func (f FullDocument) ParseToStruct(dest any) error {
if helper.IsNotPointer(dest) {
return errDestIsNotPointer(2)
} else if helper.IsNotStruct(dest) {
if helper.IsNotStruct(dest) {
return errDestIsNotStruct(2)
}
return errors.NewSkipCaller(2, helper.ConvertToDest(f, dest))
err := helper.ConvertToDest(f, dest)
return errors.NewSkipCaller(2, err)
}

func processNextEvent(handler EventHandler, event Event, opt *option.WatchWithHandler) {
Expand Down

0 comments on commit 8eb9c9e

Please sign in to comment.