2023-01-04 13:41:29 -08:00
|
|
|
package db
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2023-01-04 13:41:29 -08:00
|
|
|
|
2023-03-29 14:59:51 -07:00
|
|
|
type WordStatus string
|
2023-01-04 13:41:29 -08:00
|
|
|
|
|
|
|
const (
|
2023-03-29 14:59:51 -07:00
|
|
|
StatusUnknown WordStatus = ""
|
|
|
|
StatusFavourite WordStatus = "favourite"
|
|
|
|
StatusOkay WordStatus = "okay"
|
|
|
|
StatusJokingly WordStatus = "jokingly"
|
|
|
|
StatusFriendsOnly WordStatus = "friends_only"
|
|
|
|
StatusAvoid WordStatus = "avoid"
|
2023-01-04 13:41:29 -08:00
|
|
|
)
|
|
|
|
|
2023-03-29 14:59:51 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:41:29 -08:00
|
|
|
type FieldEntry struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
Status WordStatus `json:"status"`
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
func (fe FieldEntry) Validate() string {
|
|
|
|
if fe.Value == "" {
|
|
|
|
return "value cannot be empty"
|
|
|
|
}
|
|
|
|
|
|
|
|
if len([]rune(fe.Value)) > FieldEntryMaxLength {
|
|
|
|
return fmt.Sprintf("name must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(fe.Value)))
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:15:21 -07:00
|
|
|
if !fe.Status.Valid() {
|
2023-03-29 14:59:51 -07:00
|
|
|
return "status is invalid"
|
2023-01-30 15:50:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:41:29 -08:00
|
|
|
type PronounEntry struct {
|
|
|
|
Pronouns string `json:"pronouns"`
|
|
|
|
DisplayText *string `json:"display_text"`
|
|
|
|
Status WordStatus `json:"status"`
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
func (p PronounEntry) Validate() string {
|
|
|
|
if p.Pronouns == "" {
|
|
|
|
return "pronouns cannot be empty"
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.DisplayText != nil {
|
|
|
|
if len([]rune(*p.DisplayText)) > FieldEntryMaxLength {
|
|
|
|
return fmt.Sprintf("display_text must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(*p.DisplayText)))
|
2023-01-04 13:41:29 -08:00
|
|
|
}
|
|
|
|
}
|
2023-01-30 15:50:17 -08:00
|
|
|
|
|
|
|
if len([]rune(p.Pronouns)) > FieldEntryMaxLength {
|
|
|
|
return fmt.Sprintf("pronouns must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(p.Pronouns)))
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:15:21 -07:00
|
|
|
if !p.Status.Valid() {
|
2023-03-29 14:59:51 -07:00
|
|
|
return "status is invalid"
|
2023-01-30 15:50:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PronounEntry) String() string {
|
|
|
|
if p.DisplayText != nil {
|
|
|
|
return *p.DisplayText
|
|
|
|
}
|
|
|
|
|
|
|
|
split := strings.Split(p.Pronouns, "/")
|
|
|
|
if len(split) <= 2 {
|
|
|
|
return strings.Join(split, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(split[:1], "/")
|
2023-01-04 13:41:29 -08:00
|
|
|
}
|