From 15db7c4b9834a60a1b0415917c2a76a4004356b8 Mon Sep 17 00:00:00 2001 From: Ramky-Infoblox <111125171+Ramky-Infoblox@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:58:52 +0530 Subject: [PATCH] added prefix '_' to total_size_needed (#427) * added prefix '_' to total_size_needed * fixed lint error --- gateway/gateway.go | 3 ++- gateway/operator.go | 26 +++++++++++++++--------- query/pagination.go | 10 ++++++++- query/pagination_test.go | 44 ++++++++++++++++++++++++++++++++-------- 4 files changed, 62 insertions(+), 21 deletions(-) diff --git a/gateway/gateway.go b/gateway/gateway.go index 645a0d41..c391ceaf 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -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()) } diff --git a/gateway/operator.go b/gateway/operator.go index ae698a71..eecfedbf 100644 --- a/gateway/operator.go +++ b/gateway/operator.go @@ -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" ) @@ -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)) } diff --git a/query/pagination.go b/query/pagination.go index aa6495e7..1c8c6dfe 100644 --- a/query/pagination.go +++ b/query/pagination.go @@ -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 != "" { @@ -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 } diff --git a/query/pagination_test.go b/query/pagination_test.go index 18ff106e..b43014d5 100644 --- a/query/pagination_test.go +++ b/query/pagination_test.go @@ -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") } @@ -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") } @@ -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") } @@ -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") } @@ -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") } @@ -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) } @@ -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) } @@ -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) } @@ -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) {