Skip to content

Commit

Permalink
Rebase fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles committed Nov 14, 2023
1 parent de2972c commit 54daea8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
102 changes: 102 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
package model

import (
"bytes"
"context"
"fmt"
"strconv"
stdlibtime "time"

"github.com/pkg/errors"

"github.com/ice-blockchain/eskimo/users"
"github.com/ice-blockchain/wintr/log"
Expand Down Expand Up @@ -281,3 +286,100 @@ func SerializedUsersKey(val any) string {
panic(fmt.Sprintf("%#v cannot be used as users key", val))
}
}

type (
TimeSlice []*time.Time
)

func (t *TimeSlice) Equals(other *TimeSlice) bool {
if t != nil && other != nil && len(*t) == (len(*other)) {
var equals int
for ix, thisVal := range *t {
if otherVal := (*other)[ix]; (thisVal.IsNil() && otherVal.IsNil()) || (!thisVal.IsNil() && !otherVal.IsNil() && thisVal.Equal(*otherVal.Time)) {
equals++
}
}

return equals == len(*t)
}

return t == nil && other == nil
}

func (t *TimeSlice) UnmarshalBinary(text []byte) error {
return t.UnmarshalText(text)
}

func (t *TimeSlice) UnmarshalJSON(text []byte) error {
if len(text) == 0 || (len(text) == 2 && string(text) == "[]") {
return nil
}
sep := make([]byte, 1)
sep[0] = ','
elems := bytes.Split(text[1:len(text)-1], sep)
timeSlice := make(TimeSlice, 0, len(elems))
for _, val := range elems {
unmarshalledTime := new(time.Time)
if err := unmarshalledTime.UnmarshalJSON(context.Background(), val); err != nil {
return errors.Wrapf(err, "failed to UnmarshalJSON %#v:%v", unmarshalledTime, string(val))
}
if !unmarshalledTime.IsNil() {
timeSlice = append(timeSlice, unmarshalledTime)
}
}
*t = timeSlice

return nil
}

func (t *TimeSlice) UnmarshalText(text []byte) error {
if len(text) == 0 || (len(text) == 1 && string(text) == "") {
return nil
}
sep := make([]byte, 1)
sep[0] = ','
elems := bytes.Split(text, sep)
timeSlice := make(TimeSlice, 0, len(elems))
for _, val := range elems {
unmarshalledTime := new(time.Time)
if err := unmarshalledTime.UnmarshalText(val); err != nil {
return errors.Wrapf(err, "failed to unmarshall %#v:%v", unmarshalledTime, string(val))
}
if !unmarshalledTime.IsNil() {
timeSlice = append(timeSlice, unmarshalledTime)
}
}
*t = timeSlice

return nil
}

func (t *TimeSlice) MarshalText() ([]byte, error) {
return t.MarshalBinary()
}

func (t *TimeSlice) MarshalBinary() ([]byte, error) {
if t == nil || len(*t) == 0 {
return nil, nil
}
timeSlice := *t
text := make([]byte, 0, len(timeSlice)*(len(stdlibtime.RFC3339Nano)+1))
for ix, val := range timeSlice {
marshalledTime, err := val.MarshalText()
if err != nil {
return nil, errors.Wrapf(err, "failed to marshall: %#v", val)
}
if len(marshalledTime) == 0 {
continue
}
text = append(text, marshalledTime...)
if ix != len(timeSlice)-1 {
text = append(text, ',')
}
}
if len(text) == 0 {
return nil, nil
}

return text, nil
}
1 change: 0 additions & 1 deletion tokenomics/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func (s *usersTableSource) replaceUser(ctx context.Context, usr *users.User) err
model.IDT0Field
model.IDTMinus1Field
model.HideRankingField
model.BalanceForTMinus1Field
}
)
dbUser, err := storage.Get[user](ctx, s.db, model.SerializedUsersKey(internalID))
Expand Down

0 comments on commit 54daea8

Please sign in to comment.