do not set nil to Locale field

This commit is contained in:
Tim Möhlmann 2023-12-22 10:35:03 +02:00
parent bc1fbf5eaa
commit ca83a6032a
2 changed files with 6 additions and 7 deletions

View file

@ -77,6 +77,10 @@ func (l *Locale) MarshalJSON() ([]byte, error) {
return json.Marshal(tag) return json.Marshal(tag)
} }
// UnmarshalJSON implements json.Unmarshaler.
// When [language.ValueError] is encountered, the containing tag will be set
// to an empty value (language "und") and no error will be returned.
// This state can be checked with the `l.Tag().IsRoot()` method.
func (l *Locale) UnmarshalJSON(data []byte) error { func (l *Locale) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, &l.tag) err := json.Unmarshal(data, &l.tag)
if err == nil { if err == nil {
@ -86,6 +90,7 @@ func (l *Locale) UnmarshalJSON(data []byte) error {
// catch "well-formed but unknown" errors // catch "well-formed but unknown" errors
var target language.ValueError var target language.ValueError
if errors.As(err, &target) { if errors.As(err, &target) {
l.tag = language.Tag{}
return nil return nil
} }
return err return err

View file

@ -41,13 +41,7 @@ func (u *UserInfo) MarshalJSON() ([]byte, error) {
} }
func (u *UserInfo) UnmarshalJSON(data []byte) error { func (u *UserInfo) UnmarshalJSON(data []byte) error {
if err := unmarshalJSONMulti(data, (*uiAlias)(u), &u.Claims); err != nil { return unmarshalJSONMulti(data, (*uiAlias)(u), &u.Claims)
return err
}
if u.Locale != nil && u.Locale.tag.IsRoot() {
u.Locale = nil
}
return nil
} }
type UserInfoProfile struct { type UserInfoProfile struct {