2024-03-11 12:39:52 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-06-12 14:38:49 +01:00
|
|
|
"database/sql/driver"
|
2024-03-11 12:39:52 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-05-13 19:55:09 +01:00
|
|
|
"github.com/mrmelon54/pronouns"
|
2024-03-11 12:39:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ sql.Scanner = &UserPronoun{}
|
2024-06-12 14:47:05 +01:00
|
|
|
_ driver.Valuer = &UserPronoun{}
|
2024-03-11 12:39:52 +00:00
|
|
|
_ json.Marshaler = &UserPronoun{}
|
|
|
|
_ json.Unmarshaler = &UserPronoun{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserPronoun struct{ pronouns.Pronoun }
|
|
|
|
|
|
|
|
func (p *UserPronoun) Scan(src any) error {
|
|
|
|
s, ok := src.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, p)
|
|
|
|
}
|
|
|
|
pro, err := pronouns.FindPronoun(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Pronoun = pro
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-12 14:38:49 +01:00
|
|
|
|
2024-06-12 14:47:05 +01:00
|
|
|
func (p UserPronoun) Value() (driver.Value, error) {
|
2024-06-12 14:38:49 +01:00
|
|
|
return p.Pronoun.String(), nil
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:39:52 +00:00
|
|
|
func (p UserPronoun) MarshalJSON() ([]byte, error) { return json.Marshal(p.Pronoun.String()) }
|
2024-06-12 14:38:49 +01:00
|
|
|
|
2024-03-11 12:39:52 +00:00
|
|
|
func (p *UserPronoun) UnmarshalJSON(bytes []byte) error {
|
|
|
|
var a string
|
|
|
|
err := json.Unmarshal(bytes, &a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.Scan(a)
|
|
|
|
}
|