Add pagination using opaque cursors

This commit is contained in:
Vikram Rangnekar
2020-02-10 12:15:37 +05:30
parent 12007db76e
commit 7413813138
31 changed files with 1142 additions and 1048 deletions

View File

@ -109,7 +109,7 @@ func Filter(w *bytes.Buffer, b []byte, keys []string) error {
case state == expectValue && b[i] == 'n':
state = expectNull
case state == expectNull && b[i] == 'l':
case state == expectNull && (b[i-1] == 'l' && b[i] == 'l'):
e = i
}

View File

@ -117,8 +117,9 @@ func Get(b []byte, keys [][]byte) []Field {
case state == expectValue && b[i] == 'n':
state = expectNull
s = i
case state == expectNull && b[i] == 'l':
case state == expectNull && (b[i-1] == 'l' && b[i] == 'l'):
e = i
}

View File

@ -158,6 +158,9 @@ var (
input5 = `
{"data":{"title":"In September 2018, Slovak police stated that Kuciak was murdered because of his investigative work, and that the murder had been ordered.[9][10] They arrested eight suspects,[11] charging three of them with first-degree murder.[11]","topics":["cpp"]},"a":["1111"]},"thread_slug":"in-september-2018-slovak-police-stated-that-kuciak-7929",}`
input6 = `
{"users" : [{"id" : 1, "email" : "vicram@gmail.com", "slug" : "vikram-rangnekar", "threads" : [], "threads_cursor" : null}, {"id" : 3, "email" : "marareilly@lang.name", "slug" : "raymundo-corwin", "threads" : [{"id" : 9, "title" : "Et alias et aut porro praesentium nam in voluptatem reiciendis quisquam perspiciatis inventore eos quia et et enim qui amet."}, {"id" : 25, "title" : "Ipsam quam nemo culpa tempore amet optio sit sed eligendi autem consequatur quaerat rem velit quibusdam quibusdam optio a voluptatem."}], "threads_cursor" : 25}], "users_cursor" : 3}`
)
func TestGet(t *testing.T) {
@ -227,6 +230,32 @@ func TestGet1(t *testing.T) {
}
}
func TestGet2(t *testing.T) {
values := Get([]byte(input6), [][]byte{
[]byte("users_cursor"), []byte("threads_cursor"),
})
expected := []Field{
{[]byte("threads_cursor"), []byte(`null`)},
{[]byte("threads_cursor"), []byte(`25`)},
{[]byte("users_cursor"), []byte(`3`)},
}
if len(values) != len(expected) {
t.Fatal("len(values) != len(expected)")
}
for i := range expected {
if !bytes.Equal(values[i].Key, expected[i].Key) {
t.Error(string(values[i].Key), " != ", string(expected[i].Key))
}
if !bytes.Equal(values[i].Value, expected[i].Value) {
t.Error(string(values[i].Value), " != ", string(expected[i].Value))
}
}
}
func TestValue(t *testing.T) {
v1 := []byte("12345")
if !bytes.Equal(Value(v1), v1) {

View File

@ -101,8 +101,9 @@ func Keys(b []byte) [][]byte {
case state == expectValue && b[i] == 'n':
state = expectNull
s = i
case state == expectNull && b[i] == 'l':
case state == expectNull && (b[i-1] == 'l' && b[i] == 'l'):
e = i
}

View File

@ -104,8 +104,9 @@ func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
case state == expectValue && b[i] == 'n':
state = expectNull
s = i
case state == expectNull && b[i] == 'l':
case state == expectNull && (b[i-1] == 'l' && b[i] == 'l'):
e = i
}

View File

@ -82,8 +82,9 @@ func Strip(b []byte, path [][]byte) []byte {
case state == expectValue && b[i] == 'n':
state = expectNull
s = i
case state == expectNull && b[i] == 'l':
case state == expectNull && (b[i-1] == 'l' && b[i] == 'l'):
e = i
}