77 "encoding/json"
88 "time"
99
10+ "github.com/gofrs/uuid"
1011 "github.com/pkg/errors"
1112 "github.com/ssoready/hyrumtoken"
1213
@@ -21,13 +22,21 @@ type (
2122 cols []Column
2223 }
2324 jsonPageToken = struct {
24- ExpiresAt time.Time `json:"e"`
25- Cols []Column `json:"c"`
25+ ExpiresAt time.Time `json:"e"`
26+ Cols []jsonColumn `json:"c"`
27+ }
28+ jsonColumn = struct {
29+ Name string `json:"n"`
30+ Order Order `json:"o"`
31+ ValueAny any `json:"v"`
32+ ValueTime time.Time `json:"vt"`
33+ ValueUUID uuid.UUID `json:"vu"`
34+ ValueInt int64 `json:"vi"`
2635 }
2736 Column struct {
28- Name string `json:"n"`
29- Order Order `json:"o"`
30- Value any `json:"v"`
37+ Name string
38+ Order Order
39+ Value any
3140 }
3241)
3342
@@ -50,7 +59,23 @@ func (t PageToken) MarshalJSON() ([]byte, error) {
5059 }
5160 toEncode := jsonPageToken {
5261 ExpiresAt : now ().Add (time .Hour ).UTC (),
53- Cols : t .cols ,
62+ Cols : make ([]jsonColumn , len (t .cols )),
63+ }
64+ for i , col := range t .cols {
65+ toEncode .Cols [i ] = jsonColumn {
66+ Name : col .Name ,
67+ Order : col .Order ,
68+ }
69+ switch v := col .Value .(type ) {
70+ case time.Time :
71+ toEncode .Cols [i ].ValueTime = v
72+ case uuid.UUID :
73+ toEncode .Cols [i ].ValueUUID = v
74+ case int64 :
75+ toEncode .Cols [i ].ValueInt = v
76+ default :
77+ toEncode .Cols [i ].ValueAny = v
78+ }
5479 }
5580 return json .Marshal (toEncode )
5681}
@@ -62,7 +87,23 @@ func (t *PageToken) UnmarshalJSON(data []byte) error {
6287 if err := json .Unmarshal (data , & rawToken ); err != nil {
6388 return err
6489 }
65- t .cols = rawToken .Cols
90+ t .cols = make ([]Column , len (rawToken .Cols ))
91+ for i , col := range rawToken .Cols {
92+ t .cols [i ] = Column {
93+ Name : col .Name ,
94+ Order : col .Order ,
95+ }
96+ switch {
97+ case col .ValueAny != nil :
98+ t .cols [i ].Value = col .ValueAny
99+ case ! col .ValueTime .IsZero ():
100+ t .cols [i ].Value = col .ValueTime
101+ case col .ValueUUID != uuid .Nil :
102+ t .cols [i ].Value = col .ValueUUID
103+ case col .ValueInt != 0 :
104+ t .cols [i ].Value = col .ValueInt
105+ }
106+ }
66107 now := time .Now
67108 if t .testNow != nil {
68109 now = t .testNow
@@ -73,6 +114,4 @@ func (t *PageToken) UnmarshalJSON(data []byte) error {
73114 return nil
74115}
75116
76- func NewPageToken (cols ... Column ) PageToken {
77- return PageToken {cols : cols }
78- }
117+ func NewPageToken (cols ... Column ) PageToken { return PageToken {cols : cols } }
0 commit comments