Skip to content

Commit

Permalink
It would help if I committed this
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Jul 14, 2023
1 parent 5051c20 commit c26f29b
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions internal/avatar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package internal

import (
"bytes"
"encoding/json"
)

// sentinel value
const deletedAvatar = "<no-avatar>"

// An AvatarChange represents a change to a room's avatar. There are three cases:
// - an empty string represents no change, and should be omitted when JSON-serisalised;

This comment has been minimized.

Copy link
@DMRobertson

DMRobertson Jul 14, 2023

Contributor

In particular I want the zero value of the type to be "no change"

// - the sentinel `avatarNotPresent` represents a room that has never had an avatar,
// or a room whose avatar has been removed. It is JSON-serialised as null.
// - All other strings represent the current avatar of the room and JSON-serialise as
// normal.
type AvatarChange string

func (a AvatarChange) MarshalJSON() ([]byte, error) {
if a == deletedAvatar {
return []byte(`null`), nil
} else {
return json.Marshal(string(a))
}
}

// Note: the unmarshalling is only used in tests.
func (a *AvatarChange) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("null")) {
*a = deletedAvatar
return nil
}
return json.Unmarshal(data, (*string)(a))
}

var DeletedAvatar = AvatarChange(deletedAvatar)
var UnchangedAvatar AvatarChange

0 comments on commit c26f29b

Please sign in to comment.