-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl pagination, split exec log out of task list
- Loading branch information
Showing
22 changed files
with
6,601 additions
and
6,762 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,97 @@ | ||
package taskengine | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"strconv" | ||
|
||
"github.com/oklog/ulid/v2" | ||
) | ||
|
||
type CursorDirection string | ||
|
||
const ( | ||
CursorDirectionNext = CursorDirection("next") | ||
CursorDirectionPrevious = CursorDirection("prev") | ||
) | ||
|
||
type Cursor struct { | ||
Direction CursorDirection `json:"d"` | ||
Position string `json:"p"` | ||
|
||
parsePos bool `json:"-"` | ||
int64Pos int64 `json:"-"` | ||
ulidPos ulid.ULID `json:"-"` | ||
} | ||
|
||
func CursorFromString(data string) (*Cursor, error) { | ||
c := &Cursor{ | ||
Direction: CursorDirectionNext, | ||
Position: "0", | ||
parsePos: false, | ||
int64Pos: 0, | ||
ulidPos: ulid.Zero, | ||
} | ||
|
||
decoded, err := base64.StdEncoding.DecodeString(data) | ||
if err != nil { | ||
return c, err | ||
} | ||
|
||
if err = json.Unmarshal(decoded, &c); err == nil { | ||
return c, nil | ||
} else { | ||
return c, err | ||
} | ||
} | ||
|
||
func NewCursor(direction CursorDirection, position string) *Cursor { | ||
return &Cursor{ | ||
Direction: direction, | ||
Position: position, | ||
|
||
parsePos: false, | ||
int64Pos: 0, | ||
} | ||
} | ||
func (c *Cursor) String() string { | ||
var d []byte | ||
d, err := json.Marshal(c) | ||
|
||
if err != nil { | ||
return "" | ||
} | ||
|
||
encoded := base64.StdEncoding.EncodeToString(d) | ||
|
||
return encoded | ||
} | ||
|
||
// Given a value, return true if the value is after the cursor | ||
func (c *Cursor) AfterInt64(value int64) bool { | ||
if !c.parsePos { | ||
c.int64Pos, _ = strconv.ParseInt(c.Position, 10, 64) | ||
c.parsePos = true | ||
} | ||
if c.Direction == CursorDirectionNext { | ||
return c.int64Pos <= value | ||
} | ||
|
||
return c.int64Pos >= value | ||
} | ||
|
||
// Given a value, return true if the value is after the cursor | ||
func (c *Cursor) AfterUlid(value ulid.ULID) bool { | ||
if !c.parsePos { | ||
var err error | ||
c.ulidPos, err = ulid.Parse(c.Position) | ||
if err != nil { | ||
c.ulidPos = ulid.Zero | ||
} | ||
c.parsePos = true | ||
} | ||
if c.Direction == CursorDirectionNext { | ||
return c.ulidPos.Compare(value) < 0 | ||
} | ||
return c.ulidPos.Compare(value) > 0 | ||
} |
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.