Skip to content

Commit b1581f4

Browse files
committed
Add support for European Union flag
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.
1 parent 3ccda42 commit b1581f4

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

reader.go

+26-20
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ type City struct {
2727
Names map[string]string `maxminddb:"names"`
2828
} `maxminddb:"continent"`
2929
Country struct {
30-
GeoNameID uint `maxminddb:"geoname_id"`
31-
IsoCode string `maxminddb:"iso_code"`
32-
Names map[string]string `maxminddb:"names"`
30+
GeoNameID uint `maxminddb:"geoname_id"`
31+
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
32+
IsoCode string `maxminddb:"iso_code"`
33+
Names map[string]string `maxminddb:"names"`
3334
} `maxminddb:"country"`
3435
Location struct {
3536
AccuracyRadius uint16 `maxminddb:"accuracy_radius"`
@@ -42,15 +43,17 @@ type City struct {
4243
Code string `maxminddb:"code"`
4344
} `maxminddb:"postal"`
4445
RegisteredCountry struct {
45-
GeoNameID uint `maxminddb:"geoname_id"`
46-
IsoCode string `maxminddb:"iso_code"`
47-
Names map[string]string `maxminddb:"names"`
46+
GeoNameID uint `maxminddb:"geoname_id"`
47+
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
48+
IsoCode string `maxminddb:"iso_code"`
49+
Names map[string]string `maxminddb:"names"`
4850
} `maxminddb:"registered_country"`
4951
RepresentedCountry struct {
50-
GeoNameID uint `maxminddb:"geoname_id"`
51-
IsoCode string `maxminddb:"iso_code"`
52-
Names map[string]string `maxminddb:"names"`
53-
Type string `maxminddb:"type"`
52+
GeoNameID uint `maxminddb:"geoname_id"`
53+
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
54+
IsoCode string `maxminddb:"iso_code"`
55+
Names map[string]string `maxminddb:"names"`
56+
Type string `maxminddb:"type"`
5457
} `maxminddb:"represented_country"`
5558
Subdivisions []struct {
5659
GeoNameID uint `maxminddb:"geoname_id"`
@@ -72,20 +75,23 @@ type Country struct {
7275
Names map[string]string `maxminddb:"names"`
7376
} `maxminddb:"continent"`
7477
Country struct {
75-
GeoNameID uint `maxminddb:"geoname_id"`
76-
IsoCode string `maxminddb:"iso_code"`
77-
Names map[string]string `maxminddb:"names"`
78+
GeoNameID uint `maxminddb:"geoname_id"`
79+
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
80+
IsoCode string `maxminddb:"iso_code"`
81+
Names map[string]string `maxminddb:"names"`
7882
} `maxminddb:"country"`
7983
RegisteredCountry struct {
80-
GeoNameID uint `maxminddb:"geoname_id"`
81-
IsoCode string `maxminddb:"iso_code"`
82-
Names map[string]string `maxminddb:"names"`
84+
GeoNameID uint `maxminddb:"geoname_id"`
85+
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
86+
IsoCode string `maxminddb:"iso_code"`
87+
Names map[string]string `maxminddb:"names"`
8388
} `maxminddb:"registered_country"`
8489
RepresentedCountry struct {
85-
GeoNameID uint `maxminddb:"geoname_id"`
86-
IsoCode string `maxminddb:"iso_code"`
87-
Names map[string]string `maxminddb:"names"`
88-
Type string `maxminddb:"type"`
90+
GeoNameID uint `maxminddb:"geoname_id"`
91+
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
92+
IsoCode string `maxminddb:"iso_code"`
93+
Names map[string]string `maxminddb:"names"`
94+
Type string `maxminddb:"type"`
8995
} `maxminddb:"represented_country"`
9096
Traits struct {
9197
IsAnonymousProxy bool `maxminddb:"is_anonymous_proxy"`

reader_test.go

+29-10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func TestReader(t *testing.T) {
6464
)
6565

6666
assert.Equal(t, uint(2635167), record.Country.GeoNameID)
67+
assert.True(t, record.Country.IsInEuropeanUnion)
6768
assert.Equal(t, "GB", record.Country.IsoCode)
6869
assert.Equal(t,
6970
map[string]string{
@@ -97,19 +98,23 @@ func TestReader(t *testing.T) {
9798
)
9899

99100
assert.Equal(t, uint(6252001), record.RegisteredCountry.GeoNameID)
101+
assert.False(t, record.RegisteredCountry.IsInEuropeanUnion)
100102
assert.Equal(t, "US", record.RegisteredCountry.IsoCode)
101-
assert.Equal(t, map[string]string{
102-
"de": "USA",
103-
"en": "United States",
104-
"es": "Estados Unidos",
105-
"fr": "États-Unis",
106-
"ja": "アメリカ合衆国",
107-
"pt-BR": "Estados Unidos",
108-
"ru": "США",
109-
"zh-CN": "美国",
110-
},
103+
assert.Equal(t,
104+
map[string]string{
105+
"de": "USA",
106+
"en": "United States",
107+
"es": "Estados Unidos",
108+
"fr": "États-Unis",
109+
"ja": "アメリカ合衆国",
110+
"pt-BR": "Estados Unidos",
111+
"ru": "США",
112+
"zh-CN": "美国",
113+
},
111114
record.RegisteredCountry.Names,
112115
)
116+
117+
assert.False(t, record.RepresentedCountry.IsInEuropeanUnion)
113118
}
114119

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

172+
func TestCountry(t *testing.T) {
173+
reader, err := Open("test-data/test-data/GeoIP2-Country-Test.mmdb")
174+
assert.Nil(t, err)
175+
176+
defer reader.Close()
177+
178+
record, err := reader.Country(net.ParseIP("81.2.69.160"))
179+
assert.Nil(t, err)
180+
181+
assert.True(t, record.Country.IsInEuropeanUnion)
182+
assert.False(t, record.RegisteredCountry.IsInEuropeanUnion)
183+
assert.False(t, record.RepresentedCountry.IsInEuropeanUnion)
184+
}
185+
167186
func TestDomain(t *testing.T) {
168187
reader, err := Open("test-data/test-data/GeoIP2-Domain-Test.mmdb")
169188
assert.Nil(t, err)

0 commit comments

Comments
 (0)