Skip to content

Commit 9aed9d7

Browse files
committed
Extract an isInEuropeanUnion utility method
1 parent a27f5ac commit 9aed9d7

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ protected Map<String, Object> transform(final CityResponse response) {
209209
switch (property) {
210210
case IP -> data.put("ip", response.getTraits().getIpAddress());
211211
case COUNTRY_IN_EUROPEAN_UNION -> {
212-
if (country.getIsoCode() != null) {
213-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
214-
data.put("country_in_european_union", country.isInEuropeanUnion());
212+
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
213+
if (isInEuropeanUnion != null) {
214+
data.put("country_in_european_union", isInEuropeanUnion);
215215
}
216216
}
217217
case COUNTRY_ISO_CODE -> {
@@ -288,9 +288,9 @@ protected Map<String, Object> transform(final CityResponse response) {
288288
}
289289
}
290290
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
291-
if (registeredCountry.getIsoCode() != null) {
292-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
293-
data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion());
291+
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
292+
if (isInEuropeanUnion != null) {
293+
data.put("registered_country_in_european_union", isInEuropeanUnion);
294294
}
295295
}
296296
case REGISTERED_COUNTRY_ISO_CODE -> {
@@ -353,9 +353,9 @@ protected Map<String, Object> transform(final CountryResponse response) {
353353
switch (property) {
354354
case IP -> data.put("ip", response.getTraits().getIpAddress());
355355
case COUNTRY_IN_EUROPEAN_UNION -> {
356-
if (country.getIsoCode() != null) {
357-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
358-
data.put("country_in_european_union", country.isInEuropeanUnion());
356+
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
357+
if (isInEuropeanUnion != null) {
358+
data.put("country_in_european_union", isInEuropeanUnion);
359359
}
360360
}
361361
case COUNTRY_ISO_CODE -> {
@@ -383,9 +383,9 @@ protected Map<String, Object> transform(final CountryResponse response) {
383383
}
384384
}
385385
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
386-
if (registeredCountry.getIsoCode() != null) {
387-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
388-
data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion());
386+
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
387+
if (isInEuropeanUnion != null) {
388+
data.put("registered_country_in_european_union", isInEuropeanUnion);
389389
}
390390
}
391391
case REGISTERED_COUNTRY_ISO_CODE -> {
@@ -480,9 +480,9 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
480480
}
481481
}
482482
case COUNTRY_IN_EUROPEAN_UNION -> {
483-
if (country.getIsoCode() != null) {
484-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
485-
data.put("country_in_european_union", country.isInEuropeanUnion());
483+
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
484+
if (isInEuropeanUnion != null) {
485+
data.put("country_in_european_union", isInEuropeanUnion);
486486
}
487487
}
488488
case COUNTRY_ISO_CODE -> {
@@ -639,9 +639,9 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
639639
}
640640
}
641641
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
642-
if (registeredCountry.getIsoCode() != null) {
643-
// isInEuropeanUnion is a boolean so it can't be null. But it really only makes sense if we have a country
644-
data.put("registered_country_in_european_union", registeredCountry.isInEuropeanUnion());
642+
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
643+
if (isInEuropeanUnion != null) {
644+
data.put("registered_country_in_european_union", isInEuropeanUnion);
645645
}
646646
}
647647
case REGISTERED_COUNTRY_ISO_CODE -> {
@@ -776,4 +776,11 @@ private RESPONSE lookup(final Reader reader, final String ipAddress) throws IOEx
776776
*/
777777
protected abstract Map<String, Object> transform(RESPONSE response);
778778
}
779+
780+
@Nullable
781+
private static Boolean isInEuropeanUnion(com.maxmind.geoip2.record.Country country) {
782+
// isInEuropeanUnion is a lowercase-b boolean so it cannot be null, but it really only makes sense for us to return a value
783+
// for this if there's actually a real country here, as opposed to an empty null-object country, so we check for an iso code first
784+
return (country.getIsoCode() == null) ? null : country.isInEuropeanUnion();
785+
}
779786
}

0 commit comments

Comments
 (0)