-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from cloudradar-monitoring/DEV-2496
[DEV-2496] Add pagination support
- Loading branch information
Showing
20 changed files
with
108 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/cloudradar-monitoring/rportcli/internal/pkg/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func addPaginationFlags(cmd *cobra.Command, defaultLimit int) { | ||
cmd.Flags().IntP(api.PaginationLimit, "", defaultLimit, "Number of items to fetch") | ||
cmd.Flags().IntP(api.PaginationOffset, "", 0, "Offset for fetch") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package cmd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package api | ||
|
||
import ( | ||
"net/url" | ||
"strconv" | ||
|
||
options "github.com/breathbath/go_utils/v2/pkg/config" | ||
) | ||
|
||
const ( | ||
PaginationOffset = "offset" | ||
PaginationLimit = "limit" | ||
) | ||
|
||
type Pagination struct { | ||
Limit int | ||
Offset int | ||
} | ||
|
||
func NewPaginationFromParams(params *options.ParameterBag) Pagination { | ||
return Pagination{ | ||
Offset: params.ReadInt(PaginationOffset, 0), | ||
Limit: params.ReadInt(PaginationLimit, -1), | ||
} | ||
} | ||
|
||
func NewPaginationWithLimit(max int) Pagination { | ||
return Pagination{ | ||
Offset: 0, | ||
Limit: max, | ||
} | ||
} | ||
|
||
func (p Pagination) Apply(q url.Values) { | ||
q.Set("page[offset]", strconv.Itoa(p.Offset)) | ||
q.Set("page[limit]", strconv.Itoa(p.Limit)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package api | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
options "github.com/breathbath/go_utils/v2/pkg/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPaginationFromParams(t *testing.T) { | ||
pagination := NewPaginationFromParams(options.New(options.NewMapValuesProvider(map[string]interface{}{ | ||
"limit": 30, | ||
"offset": 90, | ||
}))) | ||
|
||
q := url.Values{} | ||
pagination.Apply(q) | ||
|
||
assert.Equal(t, q.Get("page[limit]"), "30") | ||
assert.Equal(t, q.Get("page[offset]"), "90") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package rdp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters