feat(!): use strings for WordStatus enum instead of ints

This commit is contained in:
Sam 2023-03-29 23:59:51 +02:00
parent 11ef4d548a
commit 96376516b0
No known key found for this signature in database
GPG Key ID: B4EF20DDE721CAA1
3 changed files with 56 additions and 20 deletions

View File

@ -5,18 +5,54 @@ import (
"strings"
)
type WordStatus int
type WordStatus string
const (
StatusUnknown WordStatus = 0
StatusFavourite WordStatus = 1
StatusOkay WordStatus = 2
StatusJokingly WordStatus = 3
StatusFriendsOnly WordStatus = 4
StatusAvoid WordStatus = 5
wordStatusMax WordStatus = 6
StatusUnknown WordStatus = ""
StatusFavourite WordStatus = "favourite"
StatusOkay WordStatus = "okay"
StatusJokingly WordStatus = "jokingly"
StatusFriendsOnly WordStatus = "friends_only"
StatusAvoid WordStatus = "avoid"
)
func (w *WordStatus) UnmarshalJSON(src []byte) error {
if string(src) == "null" {
return nil
}
s := strings.Trim(string(src), `"`)
switch s {
case "1":
*w = "favourite"
case "2":
*w = "okay"
case "3":
*w = "jokingly"
case "4":
*w = "friends_only"
case "5":
*w = "avoid"
default:
*w = WordStatus(s)
}
return nil
}
func (w WordStatus) Valid(extra ...WordStatus) bool {
if w == StatusFavourite || w == StatusOkay || w == StatusJokingly || w == StatusFriendsOnly || w == StatusAvoid {
return true
}
for i := range extra {
if w == extra[i] {
return true
}
}
return false
}
type FieldEntry struct {
Value string `json:"value"`
Status WordStatus `json:"status"`
@ -31,8 +67,8 @@ func (fe FieldEntry) Validate() string {
return fmt.Sprintf("name must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(fe.Value)))
}
if fe.Status == StatusUnknown || fe.Status >= wordStatusMax {
return fmt.Sprintf("status is invalid, must be between 1 and %d, is %d", wordStatusMax-1, fe.Status)
if fe.Status.Valid() {
return "status is invalid"
}
return ""
@ -59,8 +95,8 @@ func (p PronounEntry) Validate() string {
return fmt.Sprintf("pronouns must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(p.Pronouns)))
}
if p.Status == StatusUnknown || p.Status >= wordStatusMax {
return fmt.Sprintf("status is invalid, must be between 1 and %d, is %d", wordStatusMax-1, p.Status)
if p.Status.Valid() {
return "status is invalid"
}
return ""

View File

@ -42,8 +42,8 @@ func (f Field) Validate() string {
return fmt.Sprintf("entries.%d: max length is %d characters, length is %d", i, FieldEntryMaxLength, length)
}
if entry.Status == StatusUnknown || entry.Status >= wordStatusMax {
return fmt.Sprintf("entries.%d: status is invalid, must be between 1 and %d, is %d", i, wordStatusMax-1, entry.Status)
if !entry.Status.Valid() {
return fmt.Sprintf("entries.%d: status is invalid", i)
}
}

View File

@ -43,12 +43,12 @@ export interface Pronoun {
}
export enum WordStatus {
Unknown = 0,
Favourite = 1,
Okay = 2,
Jokingly = 3,
FriendsOnly = 4,
Avoid = 5,
Unknown = "",
Favourite = "favourite",
Okay = "okay",
Jokingly = "jokingly",
FriendsOnly = "friends_only",
Avoid = "avoid",
}
export interface PartialMember {