Skip to content

Commit

Permalink
refactor location module data (#616)
Browse files Browse the repository at this point in the history
* refactor location module data

* fix mappings
  • Loading branch information
cieslarmichal authored May 31, 2024
1 parent c477e64 commit dc9ff1c
Show file tree
Hide file tree
Showing 104 changed files with 6,483 additions and 27,085 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ set(FAKER_SOURCES
src/modules/internet/Internet.cpp
src/modules/internet/InternetData.cpp
src/modules/location/Location.cpp
src/modules/location/LocationData.cpp
src/modules/lorem/Lorem.cpp
src/modules/medicine/Medicine.cpp
src/modules/medicine/MedicineData.cpp
Expand Down
20 changes: 7 additions & 13 deletions include/faker-cxx/Location.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <string_view>

#include "types/Precision.h"

Expand All @@ -23,12 +23,6 @@ enum class AddressCountry
Brazil,
Finland,
Estonia,
Romania,
Latvia,
Nepal,
Belgium,
Serbia,
Argentina
};

class Location
Expand All @@ -43,7 +37,7 @@ class Location
* Location::country() // "Poland"
* @endcode
*/
static std::string country();
static std::string_view country();

/**
* @brief Returns a random country code.
Expand All @@ -54,7 +48,7 @@ class Location
* Location::countryCode() // "PL"
* @endcode
*/
static std::string countryCode();
static std::string_view countryCode();

/**
* @brief Returns a random state for a given country.
Expand All @@ -67,7 +61,7 @@ class Location
* Location::state() // "Arizona"
* @endcode
*/
static std::string state(AddressCountry country = AddressCountry::Usa);
static std::string_view state(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random county for a given country.
Expand All @@ -80,7 +74,7 @@ class Location
* Location::county() // "Adams County"
* @endcode
*/
static std::string county(AddressCountry country = AddressCountry::Usa);
static std::string_view county(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random city for given country.
Expand Down Expand Up @@ -197,7 +191,7 @@ class Location
* Location::direction() // "North"
* @endcode
*/
static std::string direction();
static std::string_view direction();

/**
* @brief Generates a random time zone.
Expand All @@ -208,6 +202,6 @@ class Location
* Location::timeZone() // "Europe/Warsaw"
* @endcode
*/
static std::string timeZone();
static std::string_view timeZone();
};
}
1 change: 1 addition & 0 deletions src/common/FormatHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace faker
{

// TODO: change to std::function<std::string_view()>
std::string
FormatHelper::fillTokenValues(const std::string& format,
std::unordered_map<std::string, std::function<std::string()>> tokenValueGenerators)
Expand Down
212 changes: 121 additions & 91 deletions src/modules/location/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,172 +5,202 @@

#include "../../common/FormatHelper.h"
#include "../../common/PrecisionMapper.h"
#include "data/argentina/ArgentinaAddresses.h"
#include "data/australia/AustraliaAddresses.h"
#include "data/belgium/BelgiumAddresses.h"
#include "data/brazil/BrazilAddresses.h"
#include "data/Countries.h"
#include "data/czech/CzechAddresses.h"
#include "data/denmark/DenmarkAddresses.h"
#include "data/Directions.h"
#include "data/estonia/EstoniaAddresses.h"
#include "data/finland/FinlandAddresses.h"
#include "data/france/FranceAddresses.h"
#include "data/germany/GermanyAddresses.h"
#include "data/india/IndiaAddresses.h"
#include "data/italy/ItalyAddresses.h"
#include "data/latvia/LatviaAddresses.h"
#include "data/nepal/NepalAddresses.h"
#include "data/poland/PolandAddresses.h"
#include "data/romania/RomaniaAddresses.h"
#include "data/russia/RussiaAddresses.h"
#include "data/serbia/SerbiaAddresses.h"
#include "data/spain/SpainAddresses.h"
#include "data/TimeZones.h"
#include "data/ukraine/UkraineAddresses.h"
#include "data/usa/UsaAddresses.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Person.h"
#include "LocationData.h"

namespace faker
{
namespace
{
const std::unordered_map<AddressCountry, CountryAddresses> countryToCountryAddressesMapping{
{AddressCountry::Argentina, argentinaAddresses}, {AddressCountry::Usa, usaAddresses},
{AddressCountry::Poland, polandAddresses}, {AddressCountry::Russia, russiaAddresses},
{AddressCountry::France, franceAddresses}, {AddressCountry::Ukraine, ukraineAddresses},
{AddressCountry::Italy, italyAddresses}, {AddressCountry::Germany, germanyAddresses},
{AddressCountry::Czech, czechAddresses}, {AddressCountry::Australia, australiaAddresses},
{AddressCountry::India, indiaAddresses}, {AddressCountry::Denmark, denmarkAddresses},
{AddressCountry::Spain, spainAddresses}, {AddressCountry::Brazil, brazilAddresses},
{AddressCountry::Finland, finlandAddresses}, {AddressCountry::Estonia, estoniaAddresses},
{AddressCountry::Romania, romaniaAddresses}, {AddressCountry::Latvia, latviaAddresses},
{AddressCountry::Nepal, nepalAddresses}, {AddressCountry::Belgium, belgiumAddresses},
{AddressCountry::Serbia, serbiaAddresses},
};
CountryAddressesInfo getAddresses(const AddressCountry& country)
{
switch (country)
{
case AddressCountry::Usa:
return usaAddresses;
case AddressCountry::Poland:
return polandAddresses;
case AddressCountry::Russia:
return russiaAddresses;
case AddressCountry::France:
return franceAddresses;
case AddressCountry::Ukraine:
return ukraineAddresses;
case AddressCountry::Italy:
return italyAddresses;
case AddressCountry::Germany:
return germanyAddresses;
case AddressCountry::Czech:
return czechAddresses;
case AddressCountry::Australia:
return australiaAddresses;
case AddressCountry::India:
return indiaAddresses;
case AddressCountry::Denmark:
return denmarkAddresses;
case AddressCountry::Spain:
return spainAddresses;
case AddressCountry::Brazil:
return brazilAddresses;
case AddressCountry::Finland:
return finlandAddresses;
case AddressCountry::Estonia:
return estoniaAddresses;
default:
return usaAddresses;
}
}

const std::unordered_map<AddressCountry, Country> countryAddressToCountryMapping{
{AddressCountry::Argentina, Country::Argentina}, {AddressCountry::Usa, Country::Usa},
{AddressCountry::Poland, Country::Poland}, {AddressCountry::Russia, Country::Russia},
{AddressCountry::France, Country::France}, {AddressCountry::Ukraine, Country::Ukraine},
{AddressCountry::Italy, Country::Italy}, {AddressCountry::Germany, Country::Germany},
{AddressCountry::Czech, Country::Czech}, {AddressCountry::Australia, Country::Australia},
{AddressCountry::India, Country::India}, {AddressCountry::Denmark, Country::Denmark},
{AddressCountry::Spain, Country::Spain}, {AddressCountry::Brazil, Country::Brazil},
{AddressCountry::Finland, Country::Finland}, {AddressCountry::Estonia, Country::Estonia},
{AddressCountry::Romania, Country::Romania}, {AddressCountry::Latvia, Country::Latvia},
{AddressCountry::Nepal, Country::Nepal}, {AddressCountry::Belgium, Country::Belgium},
{AddressCountry::Serbia, Country::Serbia},
};
Country getCountry(const AddressCountry& addressCountry)
{
switch (addressCountry)
{
case AddressCountry::Usa:
return Country::Usa;
case AddressCountry::Poland:
return Country::Poland;
case AddressCountry::Russia:
return Country::Russia;
case AddressCountry::France:
return Country::France;
case AddressCountry::Ukraine:
return Country::Ukraine;
case AddressCountry::Italy:
return Country::Italy;
case AddressCountry::Germany:
return Country::Germany;
case AddressCountry::Czech:
return Country::Czech;
case AddressCountry::Australia:
return Country::Australia;
case AddressCountry::India:
return Country::India;
case AddressCountry::Denmark:
return Country::Denmark;
case AddressCountry::Spain:
return Country::Spain;
case AddressCountry::Brazil:
return Country::Brazil;
case AddressCountry::Finland:
return Country::Finland;
case AddressCountry::Estonia:
return Country::Estonia;
default:
return Country::Usa;
}
}
}

std::string Location::country()
std::string_view Location::country()
{
return Helper::arrayElement<std::string>(allCountries);
return Helper::arrayElement(allCountries);
}

std::string Location::countryCode()
std::string_view Location::countryCode()
{
return Helper::arrayElement<std::string>(countryCodes);
return Helper::arrayElement(countryCodes);
}

std::string Location::county(AddressCountry country)
std::string_view Location::county(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
const auto& countryAddresses = getAddresses(country);

if (countryAddresses.counties.empty())
{
return "";
}

return Helper::arrayElement<std::string>(countryAddresses.counties);
return Helper::arrayElement(countryAddresses.counties);
}

std::string Location::state(AddressCountry country)
std::string_view Location::state(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
const auto& countryAddresses = getAddresses(country);

return Helper::arrayElement<std::string>(countryAddresses.states);
return Helper::arrayElement(countryAddresses.states);
}

std::string Location::city(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
const auto& countryAddresses = getAddresses(country);

const auto cityFormat = Helper::arrayElement<std::string>(countryAddresses.cityFormats);
const auto cityFormat = static_cast<std::string>(Helper::arrayElement(countryAddresses.cityFormats));

const auto dataGeneratorsMapping = std::unordered_map<std::string, std::function<std::string()>>{
{"firstName", [&country]() { return Person::firstName(countryAddressToCountryMapping.at(country)); }},
{"lastName", [&country]() { return Person::lastName(countryAddressToCountryMapping.at(country)); }},
{"cityName", [&countryAddresses]() { return Helper::arrayElement<std::string>(countryAddresses.cities); }},
{"cityPrefix",
[&countryAddresses]() { return Helper::arrayElement<std::string>(countryAddresses.cityPrefixes); }},
{"citySuffix",
[&countryAddresses]() { return Helper::arrayElement<std::string>(countryAddresses.citySuffixes); }}};
{"firstName", [&country]() { return static_cast<std::string>(Person::firstName(getCountry(country))); }},
{"lastName", [&country]() { return static_cast<std::string>(Person::lastName(getCountry(country))); }},
{"cityName",
[&countryAddresses]() { return static_cast<std::string>(Helper::arrayElement(countryAddresses.cities)); }},
{"cityPrefix", [&countryAddresses]()
{ return static_cast<std::string>(Helper::arrayElement(countryAddresses.cityPrefixes)); }},
{"citySuffix", [&countryAddresses]()
{ return static_cast<std::string>(Helper::arrayElement(countryAddresses.citySuffixes)); }}};

return FormatHelper::fillTokenValues(cityFormat, dataGeneratorsMapping);
}

std::string Location::zipCode(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
const auto& countryAddresses = getAddresses(country);

return Helper::replaceSymbolWithNumber(countryAddresses.zipCodeFormat);
return Helper::replaceSymbolWithNumber(static_cast<std::string>(countryAddresses.zipCodeFormat));
}

std::string Location::streetAddress(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
const auto& countryAddresses = getAddresses(country);

const auto dataGeneratorsMapping = std::unordered_map<std::string, std::function<std::string()>>{
{"buildingNumber", [&country]() { return buildingNumber(country); }},
{"street", [&country]() { return street(country); }},
{"secondaryAddress", [&country]() { return secondaryAddress(country); }}};

const auto addressFormat = Helper::arrayElement<std::string>(countryAddresses.addressFormats);
const auto addressFormat = static_cast<std::string>(Helper::arrayElement(countryAddresses.addressFormats));

return FormatHelper::fillTokenValues(addressFormat, dataGeneratorsMapping);
}

std::string Location::street(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
const auto& countryAddresses = getAddresses(country);

const auto streetFormat = Helper::arrayElement<std::string>(countryAddresses.streetFormats);
const auto streetFormat = static_cast<std::string>(Helper::arrayElement(countryAddresses.streetFormats));

const auto dataGeneratorsMapping = std::unordered_map<std::string, std::function<std::string()>>{
{"firstName", [&country]() { return Person::firstName(countryAddressToCountryMapping.at(country)); }},
{"lastName", [&country]() { return Person::lastName(countryAddressToCountryMapping.at(country)); }},
{"streetName",
[&countryAddresses]() { return Helper::arrayElement<std::string>(countryAddresses.streetNames); }},
{"streetPrefix",
[&countryAddresses]() { return Helper::arrayElement<std::string>(countryAddresses.streetPrefixes); }},
{"streetSuffix",
[&countryAddresses]() { return Helper::arrayElement<std::string>(countryAddresses.streetSuffixes); }}};
{"firstName", [&country]() { return static_cast<std::string>(Person::firstName(getCountry(country))); }},
{"lastName", [&country]() { return static_cast<std::string>(Person::lastName(getCountry(country))); }},
{"streetName", [&countryAddresses]()
{ return static_cast<std::string>(Helper::arrayElement(countryAddresses.streetNames)); }},
{"streetPrefix", [&countryAddresses]()
{ return static_cast<std::string>(Helper::arrayElement(countryAddresses.streetPrefixes)); }},
{"streetSuffix", [&countryAddresses]()
{ return static_cast<std::string>(Helper::arrayElement(countryAddresses.streetSuffixes)); }}};

return FormatHelper::fillTokenValues(streetFormat, dataGeneratorsMapping);
}

std::string Location::buildingNumber(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
const auto& countryAddresses = getAddresses(country);

const auto buildingNumberFormat = Helper::arrayElement<std::string>(countryAddresses.buildingNumberFormats);
const auto buildingNumberFormat =
static_cast<std::string>(Helper::arrayElement(countryAddresses.buildingNumberFormats));

return Helper::replaceSymbolWithNumber(buildingNumberFormat);
}

std::string Location::secondaryAddress(AddressCountry country)
{
const auto& countryAddresses = countryToCountryAddressesMapping.at(country);
const auto& countryAddresses = getAddresses(country);

if (countryAddresses.secondaryAddressFormats.empty())
{
return "";
}

const auto secondaryAddressFormat = Helper::arrayElement<std::string>(countryAddresses.secondaryAddressFormats);
const auto secondaryAddressFormat =
static_cast<std::string>(Helper::arrayElement(countryAddresses.secondaryAddressFormats));

return Helper::replaceSymbolWithNumber(secondaryAddressFormat);
}
Expand Down Expand Up @@ -205,14 +235,14 @@ std::string Location::longitude(Precision precision)
return ss.str();
}

std::string Location::direction()
std::string_view Location::direction()
{
return Helper::arrayElement<std::string>(directions);
return Helper::arrayElement(directions);
}

std::string Location::timeZone()
std::string_view Location::timeZone()
{
return Helper::arrayElement<std::string>(timeZones);
return Helper::arrayElement(timeZones);
}

}
Loading

0 comments on commit dc9ff1c

Please sign in to comment.