Add value method to UserLocale and UserZone

This commit is contained in:
Melon 2024-06-12 14:49:44 +01:00
parent 5751f0296b
commit 33b5c99234
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 11 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package types
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"golang.org/x/text/language"
@ -9,6 +10,7 @@ import (
var (
_ sql.Scanner = &UserLocale{}
_ driver.Valuer = &UserLocale{}
_ json.Marshaler = &UserLocale{}
_ json.Unmarshaler = &UserLocale{}
)
@ -27,7 +29,13 @@ func (l *UserLocale) Scan(src any) error {
l.Tag = lang
return nil
}
func (l UserLocale) Value() (driver.Value, error) {
return l.Tag.String(), nil
}
func (l UserLocale) MarshalJSON() ([]byte, error) { return json.Marshal(l.Tag.String()) }
func (l *UserLocale) UnmarshalJSON(bytes []byte) error {
var a string
err := json.Unmarshal(bytes, &a)

View File

@ -29,12 +29,15 @@ func (l *UserZone) Scan(src any) error {
l.Location = loc
return nil
}
func (l UserZone) Value() (driver.Value, error) {
return l.Location.String(), nil
}
func (l UserZone) MarshalJSON() ([]byte, error) {
return json.Marshal(l.Location.String())
}
func (l *UserZone) UnmarshalJSON(bytes []byte) error {
var a string
err := json.Unmarshal(bytes, &a)