forked from mtusi2026/mtusi2026lab4
chore(solution): port lab3 sources
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
func encodeCursor(c domain.Cursor) string {
|
||||
raw := c.Scope.String() + "." + strconv.FormatInt(c.CreatedAt.UnixMicro(), 10) + "." + c.ID.String()
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(raw))
|
||||
}
|
||||
|
||||
func decodeCursor(s string) (domain.Cursor, bool) {
|
||||
raw, err := base64.RawURLEncoding.DecodeString(s)
|
||||
if err != nil {
|
||||
return domain.Cursor{}, false
|
||||
}
|
||||
parts := strings.Split(string(raw), ".")
|
||||
if len(parts) != 3 {
|
||||
return domain.Cursor{}, false
|
||||
}
|
||||
scope, err1 := uuid.Parse(parts[0])
|
||||
micros, err2 := strconv.ParseInt(parts[1], 10, 64)
|
||||
id, err3 := uuid.Parse(parts[2])
|
||||
if err1 != nil || err2 != nil || err3 != nil {
|
||||
return domain.Cursor{}, false
|
||||
}
|
||||
return domain.Cursor{Scope: scope, CreatedAt: time.UnixMicro(micros).UTC(), ID: id}, true
|
||||
}
|
||||
Reference in New Issue
Block a user