Mercurial > gemma
comparison pkg/models/common.go @ 1979:0bc0312105e4
Improved the error message a bit if an invalid country name is passed via JSON api.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 23 Jan 2019 12:26:09 +0100 |
parents | 71b722809b2b |
children | ae0021feaac8 |
comparison
equal
deleted
inserted
replaced
1978:0a8fa6893181 | 1979:0bc0312105e4 |
---|---|
59 var ( | 59 var ( |
60 validCountries = []string{ | 60 validCountries = []string{ |
61 "AT", "BG", "DE", "HU", "HR", | 61 "AT", "BG", "DE", "HU", "HR", |
62 "MD", "RO", "RS", "SK", "UA", | 62 "MD", "RO", "RS", "SK", "UA", |
63 } | 63 } |
64 errNoValidCountry = errors.New("Not a valid country") | |
65 ) | 64 ) |
66 | 65 |
67 // UnmarshalJSON ensures that the given string forms a valid | 66 // UnmarshalJSON ensures that the given string forms a valid |
68 // two letter country code. | 67 // two letter country code. |
69 func (c *Country) UnmarshalJSON(data []byte) error { | 68 func (c *Country) UnmarshalJSON(data []byte) error { |
70 var s string | 69 var s string |
71 if err := json.Unmarshal(data, &s); err != nil { | 70 if err := json.Unmarshal(data, &s); err != nil { |
72 return err | 71 return err |
73 } | 72 } |
74 s = strings.ToUpper(s) | 73 u := strings.ToUpper(s) |
75 for _, v := range validCountries { | 74 for _, v := range validCountries { |
76 if v == s { | 75 if v == u { |
77 *c = Country(v) | 76 *c = Country(v) |
78 return nil | 77 return nil |
79 } | 78 } |
80 } | 79 } |
81 return errNoValidCountry | 80 return fmt.Errorf("'%s' is not a valid country", s) |
82 } | 81 } |
83 | 82 |
84 // Value implements the driver.Valuer interface. | 83 // Value implements the driver.Valuer interface. |
85 func (c Country) Value() (driver.Value, error) { | 84 func (c Country) Value() (driver.Value, error) { |
86 return string(c), nil | 85 return string(c), nil |