From 8433521b3b55b33600529ed0ccef748d875340b6 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Fri, 31 Aug 2018 22:41:16 +0200 Subject: [PATCH] Differentiate between IsEU being false and unknown --- http/http.go | 2 +- http/http_test.go | 4 ++-- iputil/geo/geo.go | 8 +++----- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/http/http.go b/http/http.go index eb15bcf..8245ce2 100644 --- a/http/http.go +++ b/http/http.go @@ -34,7 +34,7 @@ type Response struct { IP net.IP `json:"ip"` IPDecimal *big.Int `json:"ip_decimal"` Country string `json:"country,omitempty"` - CountryEU bool `json:"country_eu,omitempty"` + CountryEU *bool `json:"country_eu,omitempty"` CountryISO string `json:"country_iso,omitempty"` City string `json:"city,omitempty"` Hostname string `json:"hostname,omitempty"` diff --git a/http/http_test.go b/http/http_test.go index 6478325..52cb2bf 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -17,7 +17,7 @@ func lookupPort(net.IP, uint64) error { return nil } type testDb struct{} func (t *testDb) Country(net.IP) (geo.Country, error) { - return geo.Country{Name: "Elbonia", ISO: "EB", IsEU: true}, nil + return geo.Country{Name: "Elbonia", ISO: "EB", IsEU: new(bool)}, nil } func (t *testDb) City(net.IP) (geo.City, error) { @@ -129,7 +129,7 @@ func TestJSONHandlers(t *testing.T) { out string status int }{ - {s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","country_eu":true,"country_iso":"EB","city":"Bornyasherk","hostname":"localhost","latitude":63.416667,"longitude":10.416667}`, 200}, + {s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","country_eu":false,"country_iso":"EB","city":"Bornyasherk","hostname":"localhost","latitude":63.416667,"longitude":10.416667}`, 200}, {s.URL + "/port/foo", `{"error":"Invalid port: 0"}`, 400}, {s.URL + "/port/0", `{"error":"Invalid port: 0"}`, 400}, {s.URL + "/port/65356", `{"error":"Invalid port: 65356"}`, 400}, diff --git a/iputil/geo/geo.go b/iputil/geo/geo.go index 5509c2f..e87b42f 100644 --- a/iputil/geo/geo.go +++ b/iputil/geo/geo.go @@ -16,7 +16,7 @@ type Reader interface { type Country struct { Name string ISO string - IsEU bool + IsEU *bool } type City struct { @@ -70,10 +70,8 @@ func (g *geoip) Country(ip net.IP) (Country, error) { if record.RegisteredCountry.IsoCode != "" && country.ISO == "" { country.ISO = record.RegisteredCountry.IsoCode } - country.IsEU = record.Country.IsInEuropeanUnion - if record.RegisteredCountry.IsoCode != "" && country.ISO == "" { - country.IsEU = record.RegisteredCountry.IsInEuropeanUnion - } + isEU := record.Country.IsInEuropeanUnion || record.RegisteredCountry.IsInEuropeanUnion + country.IsEU = &isEU return country, nil }