Skip to content

Commit

Permalink
feat(paginate): add package
Browse files Browse the repository at this point in the history
  • Loading branch information
pinglin committed May 17, 2022
1 parent c80ecc2 commit 5a70916
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions paginate/paginate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package paginate

import (
"encoding/base64"
"errors"
"fmt"
"strings"
"time"
)

// DecodeToken decodes the token string into create_time and UUID
func DecodeToken(encodedToken string) (time.Time, string, error) {
byt, err := base64.StdEncoding.DecodeString(encodedToken)
if err != nil {
return time.Time{}, "", err
}

arrStr := strings.Split(string(byt), ",")
if len(arrStr) != 2 {
err = errors.New("Token is invalid")
return time.Time{}, "", err
}

createdAt, err := time.Parse(time.RFC3339Nano, arrStr[0])
if err != nil {
return time.Time{}, "", err
}
uuid := arrStr[1]

return createdAt, uuid, nil
}

// EncodeToken encodes create_time and UUID into a single string
func EncodeToken(t time.Time, uuid string) string {
key := fmt.Sprintf("%s,%s", t.Format(time.RFC3339Nano), uuid)
return base64.StdEncoding.EncodeToString([]byte(key))
}

0 comments on commit 5a70916

Please sign in to comment.