aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/registry/response/pagination.go
blob: 75a925490a2ad93e0715abc3bed1607382d5ca1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package response

import (
	"net/url"
	"strconv"
)

// PaginationMeta is a structure included in responses for pagination.
type PaginationMeta struct {
	Limit         int    `json:"limit"`
	CurrentOffset int    `json:"current_offset"`
	NextOffset    *int   `json:"next_offset,omitempty"`
	PrevOffset    *int   `json:"prev_offset,omitempty"`
	NextURL       string `json:"next_url,omitempty"`
	PrevURL       string `json:"prev_url,omitempty"`
}

// NewPaginationMeta populates pagination meta data from result parameters
func NewPaginationMeta(offset, limit int, hasMore bool, currentURL string) PaginationMeta {
	pm := PaginationMeta{
		Limit:         limit,
		CurrentOffset: offset,
	}

	// Calculate next/prev offsets, leave nil if not valid pages
	nextOffset := offset + limit
	if hasMore {
		pm.NextOffset = &nextOffset
	}

	prevOffset := offset - limit
	if prevOffset < 0 {
		prevOffset = 0
	}
	if prevOffset < offset {
		pm.PrevOffset = &prevOffset
	}

	// If URL format provided, populate URLs. Intentionally swallow URL errors for now, API should
	// catch missing URLs if we call with bad URL arg (and we care about them being present).
	if currentURL != "" && pm.NextOffset != nil {
		pm.NextURL, _ = setQueryParam(currentURL, "offset", *pm.NextOffset, 0)
	}
	if currentURL != "" && pm.PrevOffset != nil {
		pm.PrevURL, _ = setQueryParam(currentURL, "offset", *pm.PrevOffset, 0)
	}

	return pm
}

func setQueryParam(baseURL, key string, val, defaultVal int) (string, error) {
	u, err := url.Parse(baseURL)
	if err != nil {
		return "", err
	}
	q := u.Query()
	if val == defaultVal {
		// elide param if it's the default value
		q.Del(key)
	} else {
		q.Set(key, strconv.Itoa(val))
	}
	u.RawQuery = q.Encode()
	return u.String(), nil
}