-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add additional column ordering to keysetpagination (#640)
Co-authored-by: Patrik <[email protected]>
- Loading branch information
1 parent
d5dfdaa
commit d608f03
Showing
5 changed files
with
305 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright © 2022 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package keysetpagination | ||
|
||
import ( | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/instana/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHeader(t *testing.T) { | ||
p := &Paginator{ | ||
defaultToken: StringPageToken("default"), | ||
token: StringPageToken("next"), | ||
size: 2, | ||
} | ||
|
||
u, err := url.Parse("http://ory.sh/") | ||
require.NoError(t, err) | ||
|
||
r := httptest.NewRecorder() | ||
|
||
Header(r, u, p) | ||
|
||
links := r.HeaderMap["Link"] | ||
require.Len(t, links, 2) | ||
assert.Contains(t, links[0], "page_token=default") | ||
assert.Contains(t, links[1], "page_token=next") | ||
|
||
t.Run("with isLast", func(t *testing.T) { | ||
p.isLast = true | ||
|
||
Header(r, u, p) | ||
|
||
links := r.HeaderMap["Link"] | ||
require.Len(t, links, 1) | ||
assert.Contains(t, links[0], "page_token=default") | ||
}) | ||
|
||
} |
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,74 @@ | ||
// Copyright © 2022 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package keysetpagination | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type PageToken interface { | ||
Parse(string) map[string]string | ||
Encode() string | ||
} | ||
|
||
var _ PageToken = new(StringPageToken) | ||
var _ PageToken = new(MapPageToken) | ||
|
||
type StringPageToken string | ||
|
||
func (s StringPageToken) Parse(idField string) map[string]string { | ||
return map[string]string{idField: string(s)} | ||
} | ||
|
||
func (s StringPageToken) Encode() string { | ||
return string(s) | ||
} | ||
|
||
func NewStringPageToken(s string) (PageToken, error) { | ||
return StringPageToken(s), nil | ||
} | ||
|
||
type MapPageToken map[string]string | ||
|
||
func (m MapPageToken) Parse(_ string) map[string]string { | ||
return map[string]string(m) | ||
} | ||
|
||
const pageTokenColumnDelim = "/" | ||
|
||
func (m MapPageToken) Encode() string { | ||
elems := make([]string, 0, len(m)) | ||
for k, v := range m { | ||
elems = append(elems, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
|
||
// For now: use Base64 instead of URL escaping, as the Timestamp format we need to use can contain a `+` sign, | ||
// which represents a space in URLs, so it's not properly encoded by the Go library. | ||
return base64.RawStdEncoding.EncodeToString([]byte(strings.Join(elems, pageTokenColumnDelim))) | ||
} | ||
|
||
func NewMapPageToken(s string) (PageToken, error) { | ||
b, err := base64.RawStdEncoding.DecodeString(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tokens := strings.Split(string(b), pageTokenColumnDelim) | ||
|
||
r := map[string]string{} | ||
|
||
for _, p := range tokens { | ||
if columnName, value, found := strings.Cut(p, "="); found { | ||
r[columnName] = value | ||
} | ||
} | ||
|
||
return MapPageToken(r), nil | ||
} | ||
|
||
var _ PageTokenConstructor = NewMapPageToken | ||
var _ PageTokenConstructor = NewStringPageToken | ||
|
||
type PageTokenConstructor = func(string) (PageToken, error) |
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
Oops, something went wrong.