Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
f4f9b54d
Commit
f4f9b54d
authored
2 years ago
by
Francé Wilke
Browse files
Options
Downloads
Patches
Plain Diff
Add address utils function GetZoneDisplayName
parent
aa796974
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
address_utils/address_utils.go
+57
-38
57 additions, 38 deletions
address_utils/address_utils.go
address_utils/address_utils_test.go
+43
-0
43 additions, 0 deletions
address_utils/address_utils_test.go
with
100 additions
and
38 deletions
address_utils/address_utils.go
+
57
−
38
View file @
f4f9b54d
...
@@ -9,15 +9,25 @@ import (
...
@@ -9,15 +9,25 @@ import (
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils"
)
)
var
southAfricaVariations
=
[]
string
{
"ZA"
,
"South Africa"
,
"Suid-Afrika"
,
"Suid Afrika"
,
"Iningizimu Afrika"
,
"Mzantsi Afrika"
,
"Afrika Boroa"
,
"Africa Kusini"
,
}
type
Province
struct
{
type
Province
struct
{
Code
string
Names
[]
string
Names
[]
string
DisplayName
string
}
}
// Provinces largely follows the ISO standard: https://en.wikipedia.org/wiki/ISO_3166-2:ZA
var
Provinces
=
map
[
string
]
Province
{
var
Provinces
=
[]
Province
{
"EC"
:
{
{
DisplayName
:
"Eastern Cape"
,
Code
:
"EC"
,
Names
:
[]
string
{
Names
:
[]
string
{
"Eastern Cape"
,
"Eastern Cape"
,
"Eastern-Cape"
,
"Eastern-Cape"
,
...
@@ -33,8 +43,8 @@ var Provinces = []Province{
...
@@ -33,8 +43,8 @@ var Provinces = []Province{
"Mpumalanga-Koloni"
,
"Mpumalanga-Koloni"
,
},
},
},
},
{
"FS"
:
{
Code
:
"FS
"
,
DisplayName
:
"Free State
"
,
Names
:
[]
string
{
Names
:
[]
string
{
"Free State"
,
"Free State"
,
"Freestate"
,
"Freestate"
,
...
@@ -48,18 +58,21 @@ var Provinces = []Province{
...
@@ -48,18 +58,21 @@ var Provinces = []Province{
"Freyisitata"
,
"Freyisitata"
,
},
},
},
},
{
"GP"
:
{
Code
:
"GP
"
,
DisplayName
:
"Gauteng
"
,
Names
:
[]
string
{
Names
:
[]
string
{
"GT"
,
"Gauteng"
,
"Gauteng"
,
"iGauteng"
,
"iGauteng"
,
"Kgauteng"
,
"Kgauteng"
,
"Rhawuti"
,
"Rhawuti"
,
},
},
},
},
{
"KZN"
:
{
Code
:
"KZN
"
,
DisplayName
:
"KwaZulu-Natal
"
,
Names
:
[]
string
{
Names
:
[]
string
{
"NT"
,
"NL"
,
"KwaZulu-Natal"
,
"KwaZulu-Natal"
,
"KwaZulu Natal"
,
"KwaZulu Natal"
,
"iKwaZulu-Natal"
,
"iKwaZulu-Natal"
,
...
@@ -70,22 +83,23 @@ var Provinces = []Province{
...
@@ -70,22 +83,23 @@ var Provinces = []Province{
"KwaZulu-Natala"
,
"KwaZulu-Natala"
,
},
},
},
},
{
"LP"
:
{
Code
:
"LP
"
,
DisplayName
:
"Limpopo
"
,
Names
:
[]
string
{
Names
:
[]
string
{
"NP"
,
"Limpopo"
,
"Limpopo"
,
"Vhembe"
,
"Vhembe"
,
},
},
},
},
{
"MP"
:
{
Code
:
"MP
"
,
DisplayName
:
"Mpumalanga
"
,
Names
:
[]
string
{
Names
:
[]
string
{
"Mpumalanga"
,
"Mpumalanga"
,
"iMpumalanga"
,
"iMpumalanga"
,
},
},
},
},
{
"NC"
:
{
Code
:
"NC
"
,
DisplayName
:
"Northern Cape
"
,
Names
:
[]
string
{
Names
:
[]
string
{
"Northern Cape"
,
"Northern Cape"
,
"Northern-Cape"
,
"Northern-Cape"
,
...
@@ -102,8 +116,8 @@ var Provinces = []Province{
...
@@ -102,8 +116,8 @@ var Provinces = []Province{
"Nyakatho-Koloni"
,
"Nyakatho-Koloni"
,
},
},
},
},
{
"NW"
:
{
Code
:
"NW
"
,
DisplayName
:
"North West
"
,
Names
:
[]
string
{
Names
:
[]
string
{
"North West"
,
"North West"
,
"North-West"
,
"North-West"
,
...
@@ -119,8 +133,8 @@ var Provinces = []Province{
...
@@ -119,8 +133,8 @@ var Provinces = []Province{
"Nyakatho-Ntshonalanga"
,
"Nyakatho-Ntshonalanga"
,
},
},
},
},
{
"WC"
:
{
Code
:
"WC
"
,
DisplayName
:
"Western Cape
"
,
Names
:
[]
string
{
Names
:
[]
string
{
"Western Cape"
,
"Western Cape"
,
"Western-Cape"
,
"Western-Cape"
,
...
@@ -212,8 +226,6 @@ func stripUnwantedCharacters(s string) string {
...
@@ -212,8 +226,6 @@ func stripUnwantedCharacters(s string) string {
func
CleanZone
(
countryToClean
,
zoneToClean
*
string
)
(
newCountry
,
newZone
*
string
)
{
func
CleanZone
(
countryToClean
,
zoneToClean
*
string
)
(
newCountry
,
newZone
*
string
)
{
newCountry
=
countryToClean
newCountry
=
countryToClean
southAfricaVariations
:=
[]
string
{
"ZA"
,
"South Africa"
,
"Suid-Afrika"
,
"Suid Afrika"
,
"Iningizimu Afrika"
,
"Mzantsi Afrika"
,
"Afrika Boroa"
,
"Africa Kusini"
}
for
_
,
southAfricaVariation
:=
range
southAfricaVariations
{
for
_
,
southAfricaVariation
:=
range
southAfricaVariations
{
if
countryToClean
==
nil
||
len
(
*
countryToClean
)
==
0
||
strings
.
ToLower
(
*
countryToClean
)
==
strings
.
ToLower
(
southAfricaVariation
)
{
if
countryToClean
==
nil
||
len
(
*
countryToClean
)
==
0
||
strings
.
ToLower
(
*
countryToClean
)
==
strings
.
ToLower
(
southAfricaVariation
)
{
defaultCountry
:=
"South Africa"
defaultCountry
:=
"South Africa"
...
@@ -224,20 +236,10 @@ func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string
...
@@ -224,20 +236,10 @@ func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string
if
*
newCountry
==
"South Africa"
&&
zoneToClean
!=
nil
{
if
*
newCountry
==
"South Africa"
&&
zoneToClean
!=
nil
{
zone
:=
*
zoneToClean
zone
:=
*
zoneToClean
if
zone
==
"GT"
{
// Gauteng - GT should be GP for Google
zone
=
"GP"
}
else
if
zone
==
"NT"
||
zone
==
"NL"
{
// KZN - NT and NL should be KZN for Google
zone
=
"KZN"
}
else
if
zone
==
"NP"
{
// Limpopo - NP should be LP for Google
zone
=
"LP"
}
for
_
,
province
:=
range
Provinces
{
for
provinceCode
,
province
:=
range
Provinces
{
for
_
,
name
:=
range
province
.
Names
{
for
_
,
name
:=
range
province
.
Names
{
zone
=
string_utils
.
ReplaceCaseInsensitive
(
zone
,
name
,
province
.
Code
)
zone
=
string_utils
.
ReplaceCaseInsensitive
(
zone
,
name
,
provinceCode
)
}
}
}
}
...
@@ -248,11 +250,28 @@ func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string
...
@@ -248,11 +250,28 @@ func CleanZone(countryToClean, zoneToClean *string) (newCountry, newZone *string
}
}
func
IsProvince
(
address
string
)
bool
{
func
IsProvince
(
address
string
)
bool
{
for
_
,
province
:=
range
Provinces
{
for
provinceCode
,
province
:=
range
Provinces
{
if
strings
.
ToLower
(
address
)
==
strings
.
ToLower
(
fmt
.
Sprintf
(
"%v, South Africa"
,
province
))
{
if
strings
.
ToLower
(
address
)
==
strings
.
ToLower
(
fmt
.
Sprintf
(
"%v, South Africa"
,
provinceCode
))
{
return
true
}
if
strings
.
ToLower
(
address
)
==
strings
.
ToLower
(
fmt
.
Sprintf
(
"%v, South Africa"
,
province
.
DisplayName
))
{
return
true
}
for
_
,
provinceName
:=
range
province
.
Names
{
if
strings
.
ToLower
(
address
)
==
strings
.
ToLower
(
fmt
.
Sprintf
(
"%v, South Africa"
,
provinceName
))
{
return
true
return
true
}
}
}
}
}
return
false
return
false
}
}
func
GetZoneDisplayName
(
zone
string
)
string
{
if
province
,
ok
:=
Provinces
[
zone
];
ok
{
return
province
.
DisplayName
}
return
zone
}
This diff is collapsed.
Click to expand it.
address_utils/address_utils_test.go
+
43
−
0
View file @
f4f9b54d
...
@@ -25,6 +25,14 @@ func TestIsProvince(t *testing.T) {
...
@@ -25,6 +25,14 @@ func TestIsProvince(t *testing.T) {
name
:
"IsProvince"
,
name
:
"IsProvince"
,
args
:
args
{
address
:
"North West, South Africa"
},
args
:
args
{
address
:
"North West, South Africa"
},
want
:
true
,
want
:
true
,
},
{
name
:
"IsProvince2"
,
args
:
args
{
address
:
"KwaZulu Natal, South Africa"
},
want
:
true
,
},
{
name
:
"IsProvince3"
,
args
:
args
{
address
:
"KZN, South Africa"
},
want
:
true
,
},
{
},
{
name
:
"IsNotProvince"
,
name
:
"IsNotProvince"
,
args
:
args
{
address
:
"22 Kruis Street, Potchefstroom, Potchefstroom, 2531, GP, ZA"
},
args
:
args
{
address
:
"22 Kruis Street, Potchefstroom, Potchefstroom, 2531, GP, ZA"
},
...
@@ -39,3 +47,38 @@ func TestIsProvince(t *testing.T) {
...
@@ -39,3 +47,38 @@ func TestIsProvince(t *testing.T) {
})
})
}
}
}
}
func
TestZoneDisplayName
(
t
*
testing
.
T
)
{
type
args
struct
{
zone
string
}
tests
:=
[]
struct
{
name
string
args
args
want
string
}{{
name
:
"IsValidZone"
,
args
:
args
{
zone
:
"LP"
},
want
:
"Limpopo"
,
},
{
name
:
"IsValidZone2"
,
args
:
args
{
zone
:
"KZN"
},
want
:
"KwaZulu-Natal"
,
},
{
name
:
"IsNotValidZone"
,
args
:
args
{
zone
:
"invalidF"
},
want
:
"invalidF"
,
},
{
name
:
"IsNotValidZone 2"
,
args
:
args
{
zone
:
"NP"
},
want
:
"NP"
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
if
got
:=
GetZoneDisplayName
(
tt
.
args
.
zone
);
got
!=
tt
.
want
{
t
.
Errorf
(
"GetZoneDisplayName() = %v, want %v"
,
got
,
tt
.
want
)
}
})
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment