Unify and improve country info

This commit is contained in:
Daniel
2023-08-28 15:26:12 +02:00
parent a2c24f131a
commit 28a4ea4709
8 changed files with 857 additions and 1290 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,7 @@
package geoip
import (
"strings"
"testing"
)
@@ -8,21 +9,31 @@ func TestCountryInfo(t *testing.T) {
t.Parallel()
for key, country := range countries {
if key != country.ID {
t.Errorf("%s has a wrong ID of %q", key, country.ID)
if key != country.Code {
t.Errorf("%s has a wrong country code of %q", key, country.Code)
}
if country.Name == "" {
t.Errorf("%s is missing name", key)
}
if country.Region == "" {
t.Errorf("%s is missing region", key)
}
if country.ContinentCode == "" {
if country.Continent.Code == "" {
t.Errorf("%s is missing continent", key)
}
if country.Continent.Region == "" {
t.Errorf("%s is missing continent region", key)
}
if country.Continent.Name == "" {
t.Errorf("%s is missing continent name", key)
}
generatedContinentCode, _, _ := strings.Cut(country.Continent.Region, "-")
if country.Continent.Code != generatedContinentCode {
t.Errorf("%s is has wrong continent code or region", key)
}
if country.Center.Latitude == 0 && country.Center.Longitude == 0 {
t.Errorf("%s is missing coords", key)
}
if country.Center.AccuracyRadius == 0 {
t.Errorf("%s is missing accuracy radius", key)
}
// Generate map source from data:
// fmt.Printf(

View File

@@ -18,13 +18,7 @@ const (
// Location holds information regarding the geographical and network location of an IP address.
// TODO: We are currently re-using the Continent-Code for the region. Update this and all dependencies.
type Location struct {
Continent struct {
Code string `maxminddb:"code"`
} `maxminddb:"continent"`
Country struct {
Name string
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
Country CountryInfo `maxminddb:"country"`
Coordinates Coordinates `maxminddb:"location"`
AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
@@ -96,10 +90,9 @@ const (
// EstimateNetworkProximity aims to calculate the distance between two network locations. Returns a proximity value between 0 (far away) and 100 (nearby).
func (l *Location) EstimateNetworkProximity(to *Location) (proximity float32) {
switch {
case l.Country.ISOCode != "" && l.Country.ISOCode == to.Country.ISOCode:
case l.Country.Code != "" && l.Country.Code == to.Country.Code:
proximity += weightCountryMatch + weightRegionMatch + weightRegionalNeighborMatch
case l.Continent.Code != "" && l.Continent.Code == to.Continent.Code:
// FYI: This is the region code!
case l.Country.Continent.Region != "" && l.Country.Continent.Region == to.Country.Continent.Region:
proximity += weightRegionMatch + weightRegionalNeighborMatch
case l.IsRegionalNeighbor(to):
proximity += weightRegionalNeighborMatch

View File

@@ -6,11 +6,11 @@ import (
// IsRegionalNeighbor returns whether the supplied location is a regional neighbor.
func (l *Location) IsRegionalNeighbor(other *Location) bool {
if l.Continent.Code == "" || other.Continent.Code == "" {
if l.Country.Continent.Region == "" || other.Country.Continent.Region == "" {
return false
}
if region, ok := regions[l.Continent.Code]; ok {
return utils.StringInSlice(region.Neighbors, other.Continent.Code)
if region, ok := regions[l.Country.Continent.Region]; ok {
return utils.StringInSlice(region.Neighbors, other.Country.Continent.Region)
}
return false
}