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)
}
// 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 {
err := json.Unmarshal(data, &l.tag)
if err == nil {
@ -86,6 +90,7 @@ func (l *Locale) UnmarshalJSON(data []byte) error {
// catch "well-formed but unknown" errors
var target language.ValueError
if errors.As(err, &target) {
l.tag = language.Tag{}
return nil
}
return err