-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Robertson
committed
Jul 14, 2023
1 parent
5051c20
commit c26f29b
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
// - 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 |
In particular I want the zero value of the type to be "no change"