Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor generating street names #261

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 15 additions & 56 deletions src/modules/location/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <map>

#include "../../common/FormatHelper.h"
#include "../../common/mappers/precisionMapper/PrecisionMapper.h"
#include "../../common/StringHelper.h"
#include "data/Countries.h"
Expand Down Expand Up @@ -104,71 +105,29 @@ std::string Location::streetAddress(Country country)
{
const auto& addressFormat = countryToAddressFormatMapping.at(country);

const auto addressFormatElements = StringHelper::split(addressFormat, " ");
const auto dataGeneratorsMapping = std::map<std::string, std::function<std::string()>>{
{"buildingNumber", [&country]() { return buildingNumber(country); }},
{"street", [&country]() { return street(country); }}};

std::vector<std::string> addressElements;

for (const auto& addressFormatElement : addressFormatElements)
{
if (addressFormatElement == "{buildingNumber}")
{
addressElements.push_back(buildingNumber(country));
}
else if (addressFormatElement == "{street}")
{
addressElements.push_back(street(country));
}
}

return StringHelper::join(addressElements, " ");
return FormatHelper::fillTokenValues(addressFormat, dataGeneratorsMapping);
}

std::string Location::street(Country country)
{
// TODO: add internalization

const auto& streetFormats = countryToStreetFormatsMapping.at(country);

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

const auto streetFormatElements = StringHelper::split(streetFormat, " ");

std::vector<std::string> streetNameElements;

for (const auto& streetFormatElement : streetFormatElements)
{
if (streetFormatElement == "{firstName}")
{
streetNameElements.push_back(Person::firstName());
}
else if (streetFormatElement == "{lastName}")
{
streetNameElements.push_back(Person::lastName());
}
else if (streetFormatElement == "{streetSuffix}")
{
const auto& streetSuffixes = countryToStreetSuffixesMapping.at(country);

const auto streetSuffix = Helper::arrayElement<std::string>(streetSuffixes);

streetNameElements.push_back(streetSuffix);
}
else if (streetFormatElement == "{streetPrefix}")
{
const auto& streetPrefixes = countryToStreetPrefixesMapping.at(country);

const auto streetPrefix = Helper::arrayElement<std::string>(streetPrefixes);

streetNameElements.push_back(streetPrefix);
}
else if (streetFormatElement == "{streetName}")
{
const auto& streets = countryToStreetsMapping.at(country);
streetNameElements.push_back(Helper::arrayElement<std::string>(streets));
}
}

return StringHelper::join(streetNameElements, " ");
const auto dataGeneratorsMapping = std::map<std::string, std::function<std::string()>>{
{"firstName", [&country]() { return Person::firstName(country); }},
{"lastName", [&country]() { return Person::lastName(country); }},
{"streetName", [&country]() { return Helper::arrayElement<std::string>(countryToStreetsMapping.at(country)); }},
{"streetPrefix",
[&country]() { return Helper::arrayElement<std::string>(countryToStreetPrefixesMapping.at(country)); }},
{"streetSuffix",
[&country]() { return Helper::arrayElement<std::string>(countryToStreetSuffixesMapping.at(country)); }}};

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

std::string Location::buildingNumber(Country country)
Expand Down
Loading