Skip to content

Commit

Permalink
[AV-68914] Bug - Move type outside of generic function (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
matty271828 authored Dec 7, 2023
1 parent 673938a commit 2ff366e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions internal/api/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ const (
SortByName = "name"
)

// overlay is a generic struct used to store data and cursor
// information from paginated responses.
type overlay[DataSchema any] struct {
Data DataSchema `json:"data"`
Cursor Cursor `json:"cursor"`
}

// GetPaginated is a generic function used to handle pagination. It executes a get request
// according to the supplied url parameter. It then iterates through remaining pages to
// flatten paginated responses into a single slice of responses.
Expand All @@ -75,11 +82,6 @@ func GetPaginated[DataSchema ~[]T, T any](
baseUrl = cfg.Url
)

type overlay struct {
Data DataSchema `json:"data"`
Cursor Cursor `json:"cursor"`
}

for {
cfg.Url = baseUrl + fmt.Sprintf("?page=%d&perPage=%d&sortBy=%s", page, perPage, string(sortBy))
cfg.Method = http.MethodGet
Expand All @@ -95,7 +97,7 @@ func GetPaginated[DataSchema ~[]T, T any](
return nil, fmt.Errorf("%s: %w", errors.ErrExecutingRequest, err)
}

var decoded overlay
var decoded overlay[DataSchema]
err = json.Unmarshal(response.Body, &decoded)
if err != nil {
return nil, fmt.Errorf("%s: %w", errors.ErrUnmarshallingResponse, err)
Expand Down

0 comments on commit 2ff366e

Please sign in to comment.