Skip to content

Commit

Permalink
events/beeper: make order_string a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Nov 18, 2024
1 parent c13ec82 commit 88dd813
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
29 changes: 19 additions & 10 deletions event/beeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,35 +114,44 @@ type BeeperEncodedOrder struct {
suborder int64
}

func NewBeeperEncodedOrder(order int64, suborder int64) BeeperEncodedOrder {
return BeeperEncodedOrder{order: order, suborder: suborder}
func NewBeeperEncodedOrder(order int64, suborder int64) *BeeperEncodedOrder {
return &BeeperEncodedOrder{order: order, suborder: suborder}
}

func BeeperEncodedOrderFromString(str string) (BeeperEncodedOrder, error) {
func BeeperEncodedOrderFromString(str string) (*BeeperEncodedOrder, error) {
order, suborder, err := decodeIntPair(str)
if err != nil {
return BeeperEncodedOrder{}, err
return nil, err
}
return BeeperEncodedOrder{order: order, suborder: suborder}, nil
return &BeeperEncodedOrder{order: order, suborder: suborder}, nil
}

func (b BeeperEncodedOrder) String() string {
func (b *BeeperEncodedOrder) String() string {
if b == nil {
return ""
}
return encodeIntPair(b.order, b.suborder)
}

func (b BeeperEncodedOrder) OrderPair() (int64, int64) {
func (b *BeeperEncodedOrder) OrderPair() (int64, int64) {
if b == nil {
return 0, 0
}
return b.order, b.suborder
}

func (b BeeperEncodedOrder) IsZero() bool {
return b.order == 0 && b.suborder == 0
func (b *BeeperEncodedOrder) IsZero() bool {
return b == nil || (b.order == 0 && b.suborder == 0)
}

func (b BeeperEncodedOrder) MarshalJSON() ([]byte, error) {
func (b *BeeperEncodedOrder) MarshalJSON() ([]byte, error) {
return []byte(`"` + b.String() + `"`), nil
}

func (b *BeeperEncodedOrder) UnmarshalJSON(data []byte) error {
if b == nil {
return fmt.Errorf("BeeperEncodedOrder: receiver is nil")
}
str := string(data)
if len(str) < 2 {
return fmt.Errorf("invalid encoded order string: %s", str)
Expand Down
8 changes: 4 additions & 4 deletions event/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ type Unsigned struct {
RedactedBecause *Event `json:"redacted_because,omitempty"`
InviteRoomState []StrippedState `json:"invite_room_state,omitempty"`

BeeperHSOrder int64 `json:"com.beeper.hs.order,omitempty"`
BeeperHSSuborder int64 `json:"com.beeper.hs.suborder,omitempty"`
BeeperHSOrderString BeeperEncodedOrder `json:"com.beeper.hs.order_string,omitempty"`
BeeperFromBackup bool `json:"com.beeper.from_backup,omitempty"`
BeeperHSOrder int64 `json:"com.beeper.hs.order,omitempty"`
BeeperHSSuborder int64 `json:"com.beeper.hs.suborder,omitempty"`
BeeperHSOrderString *BeeperEncodedOrder `json:"com.beeper.hs.order_string,omitempty"`
BeeperFromBackup bool `json:"com.beeper.from_backup,omitempty"`
}

func (us *Unsigned) IsEmpty() bool {
Expand Down

0 comments on commit 88dd813

Please sign in to comment.