Skip to content

Commit

Permalink
Add support for European Union flag
Browse files Browse the repository at this point in the history
The country structs for `geoip2.City` and `geoip2.Country` now have an
`IsInEuropeanUnion` boolean field. This is true when the associated
country is a member state of the European Union. This requires a
database built on or after Februrary 13, 2018.
  • Loading branch information
oschwald committed Feb 19, 2018
1 parent 3ccda42 commit b1581f4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
46 changes: 26 additions & 20 deletions reader.go
Expand Up @@ -27,9 +27,10 @@ type City struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"continent"`
Country struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"country"`
Location struct {
AccuracyRadius uint16 `maxminddb:"accuracy_radius"`
Expand All @@ -42,15 +43,17 @@ type City struct {
Code string `maxminddb:"code"`
} `maxminddb:"postal"`
RegisteredCountry struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"registered_country"`
RepresentedCountry struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
Type string `maxminddb:"type"`
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
Type string `maxminddb:"type"`
} `maxminddb:"represented_country"`
Subdivisions []struct {
GeoNameID uint `maxminddb:"geoname_id"`
Expand All @@ -72,20 +75,23 @@ type Country struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"continent"`
Country struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"country"`
RegisteredCountry struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"registered_country"`
RepresentedCountry struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
Type string `maxminddb:"type"`
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
Type string `maxminddb:"type"`
} `maxminddb:"represented_country"`
Traits struct {
IsAnonymousProxy bool `maxminddb:"is_anonymous_proxy"`
Expand Down
39 changes: 29 additions & 10 deletions reader_test.go
Expand Up @@ -64,6 +64,7 @@ func TestReader(t *testing.T) {
)

assert.Equal(t, uint(2635167), record.Country.GeoNameID)
assert.True(t, record.Country.IsInEuropeanUnion)
assert.Equal(t, "GB", record.Country.IsoCode)
assert.Equal(t,
map[string]string{
Expand Down Expand Up @@ -97,19 +98,23 @@ func TestReader(t *testing.T) {
)

assert.Equal(t, uint(6252001), record.RegisteredCountry.GeoNameID)
assert.False(t, record.RegisteredCountry.IsInEuropeanUnion)
assert.Equal(t, "US", record.RegisteredCountry.IsoCode)
assert.Equal(t, map[string]string{
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "États-Unis",
"ja": "アメリカ合衆国",
"pt-BR": "Estados Unidos",
"ru": "США",
"zh-CN": "美国",
},
assert.Equal(t,
map[string]string{
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "États-Unis",
"ja": "アメリカ合衆国",
"pt-BR": "Estados Unidos",
"ru": "США",
"zh-CN": "美国",
},
record.RegisteredCountry.Names,
)

assert.False(t, record.RepresentedCountry.IsInEuropeanUnion)
}

func TestMetroCode(t *testing.T) {
Expand Down Expand Up @@ -164,6 +169,20 @@ func TestConnectionType(t *testing.T) {
assert.Equal(t, "Cable/DSL", record.ConnectionType)
}

func TestCountry(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-Country-Test.mmdb")
assert.Nil(t, err)

defer reader.Close()

record, err := reader.Country(net.ParseIP("81.2.69.160"))
assert.Nil(t, err)

assert.True(t, record.Country.IsInEuropeanUnion)
assert.False(t, record.RegisteredCountry.IsInEuropeanUnion)
assert.False(t, record.RepresentedCountry.IsInEuropeanUnion)
}

func TestDomain(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-Domain-Test.mmdb")
assert.Nil(t, err)
Expand Down

0 comments on commit b1581f4

Please sign in to comment.