37 lines
706 B
Go
37 lines
706 B
Go
package utils
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func DecodeCursor(encodedCursor string) (res time.Time, id uuid.UUID, err error) {
|
|
byt, err := base64.StdEncoding.DecodeString(encodedCursor)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
arrStr := strings.Split(string(byt), ",")
|
|
if len(arrStr) != 2 {
|
|
err = errors.New("cursor is invalid")
|
|
return
|
|
}
|
|
|
|
res, err = time.Parse(time.RFC3339Nano, arrStr[0])
|
|
if err != nil {
|
|
return
|
|
}
|
|
id = uuid.MustParse(arrStr[1])
|
|
return
|
|
}
|
|
|
|
func EncodeCursor(t time.Time, id uuid.UUID) string {
|
|
key := fmt.Sprintf("%s,%s", t.Format(time.RFC3339Nano), id.String())
|
|
return base64.StdEncoding.EncodeToString([]byte(key))
|
|
}
|