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" "strings"
) )
type WordStatus int type WordStatus string
const ( const (
StatusUnknown WordStatus = 0 StatusUnknown WordStatus = ""
StatusFavourite WordStatus = 1 StatusFavourite WordStatus = "favourite"
StatusOkay WordStatus = 2 StatusOkay WordStatus = "okay"
StatusJokingly WordStatus = 3 StatusJokingly WordStatus = "jokingly"
StatusFriendsOnly WordStatus = 4 StatusFriendsOnly WordStatus = "friends_only"
StatusAvoid WordStatus = 5 StatusAvoid WordStatus = "avoid"
wordStatusMax WordStatus = 6
) )
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 { type FieldEntry struct {
Value string `json:"value"` Value string `json:"value"`
Status WordStatus `json:"status"` 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))) return fmt.Sprintf("name must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(fe.Value)))
} }
if fe.Status == StatusUnknown || fe.Status >= wordStatusMax { if fe.Status.Valid() {
return fmt.Sprintf("status is invalid, must be between 1 and %d, is %d", wordStatusMax-1, fe.Status) return "status is invalid"
} }
return "" 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))) return fmt.Sprintf("pronouns must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(p.Pronouns)))
} }
if p.Status == StatusUnknown || p.Status >= wordStatusMax { if p.Status.Valid() {
return fmt.Sprintf("status is invalid, must be between 1 and %d, is %d", wordStatusMax-1, p.Status) return "status is invalid"
} }
return "" 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) return fmt.Sprintf("entries.%d: max length is %d characters, length is %d", i, FieldEntryMaxLength, length)
} }
if entry.Status == StatusUnknown || entry.Status >= wordStatusMax { if !entry.Status.Valid() {
return fmt.Sprintf("entries.%d: status is invalid, must be between 1 and %d, is %d", i, wordStatusMax-1, entry.Status) return fmt.Sprintf("entries.%d: status is invalid", i)
} }
} }

View File

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