From ffb79c747c313051ce96f24ec0372569c9ce4822 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Fri, 6 Aug 2021 15:41:18 +0200 Subject: [PATCH] Add method "IsValidContinent". --- geoip.go | 28 ++++++++++++++++++++++++++++ geoip_test.go | 10 ++++++++++ 2 files changed, 38 insertions(+) diff --git a/geoip.go b/geoip.go index 824d62d..1f1457a 100644 --- a/geoip.go +++ b/geoip.go @@ -239,3 +239,31 @@ func LookupContinents(country string) []string { return continents } } + +func IsValidContinent(continent string) bool { + switch continent { + case "AF": + // Africa + fallthrough + case "AN": + // Antartica + fallthrough + case "AS": + // Asia + fallthrough + case "EU": + // Europe + fallthrough + case "NA": + // North America + fallthrough + case "SA": + // South America + fallthrough + case "OC": + // Oceania + return true + default: + return false + } +} diff --git a/geoip_test.go b/geoip_test.go index 8413f29..a0ef7b6 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -200,3 +200,13 @@ func TestGeoLookupFromFile(t *testing.T) { testGeoLookupReader(t, reader) } + +func TestIsValidContinent(t *testing.T) { + for country, continents := range ContinentMap { + for _, continent := range continents { + if !IsValidContinent(continent) { + t.Errorf("Continent %s of country %s is not valid", continent, country) + } + } + } +}