From 9b447fdc142e0a56c72ec075a63e48456e17b8cf Mon Sep 17 00:00:00 2001 From: Gregor Best <gbe@unobtanium.de> Date: Sat, 22 May 2021 16:51:00 +0200 Subject: [PATCH] Clean up handling and loading of ISO2 country codes --- handler-details.go | 2 +- vino.go | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/handler-details.go b/handler-details.go index e237733..721ff0d 100644 --- a/handler-details.go +++ b/handler-details.go @@ -98,7 +98,7 @@ func (h Handler) detailsPost(w http.ResponseWriter, r *http.Request) { return } - country, err := ISO3CountryFromString(r.FormValue("country")) + country, err := ISO2CountryCodeFromString(r.FormValue("country")) if err != nil { httpError(w, "can't parse country", nil, http.StatusBadRequest) return diff --git a/vino.go b/vino.go index 3b0226c..d33f4f5 100644 --- a/vino.go +++ b/vino.go @@ -32,7 +32,7 @@ type ISO2CountryCode [2]byte var UnknownCountry = ISO2CountryCode{'X', 'X'} -func ISO3CountryFromString(s string) (ISO2CountryCode, error) { +func ISO2CountryCodeFromString(s string) (ISO2CountryCode, error) { if len(s) == 0 { return UnknownCountry, nil } @@ -45,6 +45,10 @@ func ISO3CountryFromString(s string) (ISO2CountryCode, error) { } func (i ISO2CountryCode) String() string { + if i[0] == 0 || i[1] == 0 { + return "XX" + } + return fmt.Sprintf("%c%c", i[0], i[1]) } @@ -59,7 +63,7 @@ func (i *ISO2CountryCode) UnmarshalBinary(d []byte) error { return nil } - *i = ISO2CountryCode{d[0], d[1]} + copy(i[:], d) return nil } -- GitLab