-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add concept of driver & customize cursor
- Loading branch information
1 parent
cecfe98
commit a3428b6
Showing
12 changed files
with
498 additions
and
269 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
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,65 @@ | ||
package cursor | ||
|
||
import ( | ||
"encoding/base64" | ||
"github.com/vmihailenco/msgpack/v5" | ||
) | ||
|
||
type Type int | ||
|
||
const ( | ||
Before Type = 1 << iota | ||
After | ||
) | ||
|
||
type Cursor struct { | ||
Limit int | ||
Type Type | ||
Value interface{} | ||
} | ||
|
||
type EncoderDecoder interface { | ||
// When input is nil, must return an empty string | ||
Encode(input interface{}) (string, error) | ||
|
||
// When encoded is an empty string, return value must be nil | ||
Decode(encoded string) (interface{}, error) | ||
} | ||
|
||
func MsgPackBase64EncoderDecoder() EncoderDecoder { | ||
return msgpackBase64{} | ||
} | ||
|
||
type msgpackBase64 struct { | ||
} | ||
|
||
func (m msgpackBase64) Encode(input interface{}) (string, error) { | ||
if input == nil { | ||
return "", nil | ||
} | ||
|
||
data, err := msgpack.Marshal(input) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(data), nil | ||
} | ||
|
||
func (m msgpackBase64) Decode(encoded string) (interface{}, error) { | ||
if len(encoded) == 0 { | ||
return nil, nil | ||
} | ||
|
||
decoded, err := base64.StdEncoding.DecodeString(encoded) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data interface{} | ||
if err = msgpack.Unmarshal(decoded, &data); err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} |
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,36 @@ | ||
package driver | ||
|
||
import "github.com/raphaelvigee/go-paginate/cursor" | ||
|
||
type Driver interface { | ||
Init() | ||
|
||
// Used to convert data from the driver layer to the cursor layer | ||
// This should ideally be the smallest representation of the data | ||
// (ex: prefer literal, over array, over map...) | ||
CursorEncode(input interface{}) (interface{}, error) | ||
|
||
// Used to convert data from the cursor layer to the driver layer | ||
// input can be nil | ||
CursorDecode(input interface{}) (interface{}, error) | ||
|
||
Paginate(c cursor.Cursor, input interface{}) (Page, error) | ||
} | ||
|
||
type Executor interface { | ||
Query(dst interface{}) error | ||
Count() (uint64, error) | ||
} | ||
|
||
type PageInfo struct { | ||
HasNextPage bool | ||
HasPreviousPage bool | ||
StartCursor interface{} | ||
EndCursor interface{} | ||
} | ||
|
||
type Page interface { | ||
Executor | ||
Cursor(i int64) (interface{}, error) | ||
Info() PageInfo | ||
} |
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.