Skip to content

Commit

Permalink
added prefix '_' to total_size_needed (infobloxopen#427)
Browse files Browse the repository at this point in the history
* added prefix '_' to total_size_needed

* fixed lint error
  • Loading branch information
Ramky-Infoblox committed Dec 3, 2024
1 parent e27de18 commit 15db7c4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
3 changes: 2 additions & 1 deletion gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ func ClientUnaryInterceptor(parentCtx context.Context, method string, req, reply
l := vals.Get(limitQueryKey)
o := vals.Get(offsetQueryKey)
pt := vals.Get(pageTokenQueryKey)
t := vals.Get(isTotalSizeNeededQueryKey)

p, err = query.ParsePagination(l, o, pt)
p, err = query.ParsePagination(l, o, pt, t)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
Expand Down
26 changes: 16 additions & 10 deletions gateway/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
)

const (
filterQueryKey = "_filter"
sortQueryKey = "_order_by"
fieldsQueryKey = "_fields"
limitQueryKey = "_limit"
offsetQueryKey = "_offset"
pageTokenQueryKey = "_page_token"
searchQueryKey = "_fts"
pageInfoSizeMetaKey = "status-page-info-size"
pageInfoOffsetMetaKey = "status-page-info-offset"
pageInfoPageTokenMetaKey = "status-page-info-page_token"
filterQueryKey = "_filter"
sortQueryKey = "_order_by"
fieldsQueryKey = "_fields"
limitQueryKey = "_limit"
offsetQueryKey = "_offset"
pageTokenQueryKey = "_page_token"
searchQueryKey = "_fts"
isTotalSizeNeededQueryKey = "_is_total_size_needed"
pageInfoSizeMetaKey = "status-page-info-size"
pageInfoOffsetMetaKey = "status-page-info-offset"
pageInfoPageTokenMetaKey = "status-page-info-page_token"
pageInfoPageTotalSizeMetaKey = "status-page-info-total_size"

query_url = "query_url"
)
Expand Down Expand Up @@ -55,5 +57,9 @@ func SetPageInfo(ctx context.Context, p *query.PageInfo) error {
m[pageInfoSizeMetaKey] = strconv.FormatUint(uint64(s), 10)
}

if t := p.GetTotalSize(); t != 0 {
m[pageInfoPageTotalSizeMetaKey] = strconv.FormatUint(uint64(t), 10)
}

return grpc.SetHeader(ctx, metadata.New(m))
}
10 changes: 9 additions & 1 deletion query/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (

// Pagination parses string representation of pagination limit, offset.
// Returns error if limit or offset has invalid syntax or out of range.
func ParsePagination(limit, offset, ptoken string) (*Pagination, error) {
func ParsePagination(limit, offset, ptoken, isTotalSizeNeeded string) (*Pagination, error) {
p := new(Pagination)

if limit != "" {
Expand Down Expand Up @@ -43,6 +43,14 @@ func ParsePagination(limit, offset, ptoken string) (*Pagination, error) {
p.PageToken = ptoken
}

if isTotalSizeNeeded != "" {
u, err := strconv.ParseBool(isTotalSizeNeeded)
if err != nil {
return nil, fmt.Errorf("pagination: is_total_size_needed - %s", err.(*strconv.NumError).Err)
}
p.IsTotalSizeNeeded = u
}

return p, nil
}

Expand Down
44 changes: 35 additions & 9 deletions query/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package query

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParsePagination(t *testing.T) {
// invalid limit
_, err := ParsePagination("1s", "0", "ptoken")
_, err := ParsePagination("1s", "0", "ptoken", "")
if err == nil {
t.Error("unexpected nil error - expected: pagination: limit - invalid syntax")
}
Expand All @@ -15,7 +17,7 @@ func TestParsePagination(t *testing.T) {
}

// negative limit
_, err = ParsePagination("-1", "0", "ptoken")
_, err = ParsePagination("-1", "0", "ptoken", "")
if err == nil {
t.Error("unexpected nil error - expected: pagination: limit must be a positive value")
}
Expand All @@ -24,7 +26,7 @@ func TestParsePagination(t *testing.T) {
}

// zero limit
_, err = ParsePagination("0", "0", "ptoken")
_, err = ParsePagination("0", "0", "ptoken", "")
if err == nil {
t.Error("unexpected nil error - expected: pagination: limit must be a positive value")
}
Expand All @@ -33,7 +35,7 @@ func TestParsePagination(t *testing.T) {
}

// invalid offset
_, err = ParsePagination("", "0w", "ptoken")
_, err = ParsePagination("", "0w", "ptoken", "")
if err == nil {
t.Error("unexpected nil error - expected: pagination: offset - invalid syntax")
}
Expand All @@ -42,7 +44,7 @@ func TestParsePagination(t *testing.T) {
}

// negative offset
_, err = ParsePagination("", "-1", "ptoken")
_, err = ParsePagination("", "-1", "ptoken", "")
if err == nil {
t.Error("unexpected nil error - expected: pagination: offset - negative value")
}
Expand All @@ -51,7 +53,7 @@ func TestParsePagination(t *testing.T) {
}

// null offset
p, err := ParsePagination("", "null", "ptoken")
p, err := ParsePagination("", "null", "ptoken", "")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand All @@ -60,14 +62,14 @@ func TestParsePagination(t *testing.T) {
}

// first page
p, err = ParsePagination("", "0", "ptoken")
p, err = ParsePagination("", "0", "ptoken", "")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !p.FirstPage() {
t.Errorf("invalid value of first page: %v - expected: true", p.FirstPage())
}
p, err = ParsePagination("", "100", "null")
p, err = ParsePagination("", "100", "null", "")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -81,7 +83,7 @@ func TestParsePagination(t *testing.T) {
}

// valid pagination
p, err = ParsePagination("1000", "100", "ptoken")
p, err = ParsePagination("1000", "100", "ptoken", "")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand All @@ -94,6 +96,30 @@ func TestParsePagination(t *testing.T) {
if p.GetPageToken() != "ptoken" {
t.Errorf("invalid page token: %q - expected: ptoken", p.GetPageToken())
}

// valid pagination with isTotalSizeNeeded=true
p, err = ParsePagination("1000", "100", "ptoken", "true")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
assert.Equal(t, true, p.GetIsTotalSizeNeeded())

// valid pagination with isTotalSizeNeeded=false
p, err = ParsePagination("1000", "100", "ptoken", "false")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
assert.Equal(t, false, p.GetIsTotalSizeNeeded())

// valid pagination with isTotalSizeNeeded=null
_, err = ParsePagination("1000", "100", "ptoken", "null")
if err == nil {
t.Error("unexpected nil error - expected: pagination: is_total_size_needed - invalid syntax")
}
if err.Error() != "pagination: is_total_size_needed - invalid syntax" {
t.Errorf("invalid error: %s - expected: pagination: is_total_size_needed - invalid syntax", err)
}

}

func TestPageInfo(t *testing.T) {
Expand Down

0 comments on commit 15db7c4

Please sign in to comment.