Differentiate between IsEU being false and unknown

This commit is contained in:
Martin Polden 2018-08-31 22:41:16 +02:00
parent 7f538ca24b
commit 8433521b3b
3 changed files with 6 additions and 8 deletions

View File

@ -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"`

View File

@ -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},

View File

@ -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
}