diff --git a/country/alpha2.go b/country/alpha2.go index dec90bc..9e4caac 100644 --- a/country/alpha2.go +++ b/country/alpha2.go @@ -17,12 +17,14 @@ func (code *Alpha2Code) UnmarshalJSON(data []byte) error { enumValue := Alpha2Code(str) if len(enumValue) != 0 { - if _, err := ByAlpha2CodeErr(enumValue); err != nil { + country, err := ByAlpha2CodeErr(enumValue) + if err != nil { return err } + + *code = country.Alpha2Code() } - *code = enumValue return nil } diff --git a/country/alpha3.go b/country/alpha3.go index 7106a30..4076cca 100644 --- a/country/alpha3.go +++ b/country/alpha3.go @@ -17,12 +17,14 @@ func (code *Alpha3Code) UnmarshalJSON(data []byte) error { enumValue := Alpha3Code(str) if len(enumValue) != 0 { - if _, err := ByAlpha3CodeErr(enumValue); err != nil { + country, err := ByAlpha3CodeErr(enumValue) + if err != nil { return err } + + *code = country.Alpha3Code() } - *code = enumValue return nil } diff --git a/country/country_test.go b/country/country_test.go index 1ea558c..1bdd2f4 100644 --- a/country/country_test.go +++ b/country/country_test.go @@ -415,6 +415,11 @@ func TestAlpha2CodeUnmarshalJson(t *testing.T) { t.FailNow() } + var lowercase Alpha2CodeStruct + if err := json.Unmarshal([]byte(fmt.Sprintf(`{"code":"%s"}`, "ca")), &lowercase); err != nil || lowercase.Alpha2Code != Canada.Alpha2Code() { + t.FailNow() + } + var wrongCode Alpha2CodeStruct if err := json.Unmarshal([]byte(fmt.Sprintf(`{"code":"%s"}`, "wrong code")), &wrongCode); err == nil { t.FailNow() @@ -441,6 +446,11 @@ func TestAlpha3CodeUnmarshalJson(t *testing.T) { t.FailNow() } + var lowercase Alpha3CodeStruct + if err := json.Unmarshal([]byte(fmt.Sprintf(`{"code":"%s"}`, "can")), &lowercase); err != nil || lowercase.Alpha3Code != Canada.Alpha3Code() { + t.FailNow() + } + var wrongCode Alpha3CodeStruct if err := json.Unmarshal([]byte(fmt.Sprintf(`{"code":"%s"}`, "wrong code")), &wrongCode); err == nil { t.FailNow()