Skip to content

Commit

Permalink
refactor airline data (cieslarmichal#552)
Browse files Browse the repository at this point in the history
* refactor airline data

* fix tests
  • Loading branch information
cieslarmichal authored and 00thirdeye00 committed Jun 20, 2024
1 parent 41a442f commit dc973bd
Show file tree
Hide file tree
Showing 21 changed files with 563 additions and 605 deletions.
40 changes: 21 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,46 @@ endif ()
set(LIBRARY_NAME faker-cxx)

set(FAKER_SOURCES
src/modules/airline/Airline.cpp
src/modules/airline/AirlineData.cpp
src/modules/animal/Animal.cpp
src/modules/book/Book.cpp
src/modules/color/Color.cpp
src/modules/commerce/Commerce.cpp
src/modules/company/Company.cpp
src/modules/computer/Computer.cpp
src/modules/crypto/Crypto.cpp
src/modules/database/Database.cpp
src/modules/datatype/Datatype.cpp
src/modules/date/Date.cpp
src/modules/finance/Finance.cpp
src/modules/food/Food.cpp
src/modules/git/Git.cpp
src/modules/hacker/Hacker.cpp
src/modules/helper/Helper.cpp
src/modules/image/Image.cpp
src/modules/internet/Internet.cpp
src/modules/location/Location.cpp
src/modules/lorem/Lorem.cpp
src/modules/medicine/Medicine.cpp
src/modules/movie/Movie.cpp
src/modules/music/Music.cpp
src/modules/number/Number.cpp
src/modules/person/Person.cpp
src/modules/string/String.cpp
src/modules/word/Word.cpp
src/modules/phone/Phone.cpp
src/common/StringHelper.cpp
src/common/FormatHelper.cpp
src/common/LuhnCheck.cpp
src/common/mappers/precisionMapper/PrecisionMapper.cpp
src/modules/system/System.cpp
src/modules/database/Database.cpp
src/modules/music/Music.cpp
src/modules/movie/Movie.cpp
src/modules/git/Git.cpp
src/modules/hacker/Hacker.cpp
src/modules/science/Science.cpp
src/modules/sport/Sport.cpp
src/modules/string/String.cpp
src/modules/system/System.cpp
src/modules/vehicle/Vehicle.cpp
src/modules/videoGame/VideoGame.cpp
src/modules/medicine/Medicine.cpp
src/modules/weather/Weather.cpp
src/modules/airline/Airline.cpp
src/modules/image/Image.cpp
src/modules/crypto/Crypto.cpp
src/modules/computer/Computer.cpp
src/modules/vehicle/Vehicle.cpp
src/modules/science/Science.cpp
src/modules/word/Word.cpp

src/common/FormatHelper.cpp
src/common/LuhnCheck.cpp
src/common/StringHelper.cpp
src/common/PrecisionMapper.cpp
)

add_library(${LIBRARY_NAME} ${FAKER_SOURCES})
Expand Down
16 changes: 8 additions & 8 deletions include/faker-cxx/Airline.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <string_view>

namespace faker
{
Expand All @@ -16,12 +16,12 @@ class Airline
* Airline::aircraftType // "narrowbody"
* @endcode
*/
static std::string aircraftType();
static std::string_view aircraftType();

struct Airplane
{
std::string name;
std::string iataTypeCode;
std::string_view name;
std::string_view iataTypeCode;
};

/**
Expand All @@ -37,8 +37,8 @@ class Airline

struct AirlineInfo
{
std::string name;
std::string iataCode;
std::string_view name;
std::string_view iataCode;
};

/**
Expand All @@ -54,8 +54,8 @@ class Airline

struct Airport
{
std::string name;
std::string iataCode;
std::string_view name;
std::string_view iataCode;
};

/**
Expand Down
14 changes: 14 additions & 0 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ class Helper
return data[index];
}

template <typename T, std::size_t N>
static T arrayElement(const std::array<T, N>& data)
{
if (data.empty())
{
throw std::invalid_argument{"Data is empty."};
}

const auto index = Number::integer<size_t>(data.size() - 1);

return data[index];
}

/**
* @brief Get a random element from a vector.
*
Expand Down Expand Up @@ -89,6 +102,7 @@ class Helper
{
throw std::invalid_argument{"Data is empty."};
}

T item;
std::sample(data.begin(), data.end(), &item, 1, pseudoRandomGenerator);
return item;
Expand Down
5 changes: 2 additions & 3 deletions src/common/FormatHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "FormatHelper.h"

#include "errors/TokenGeneratorNotFoundError.h"
#include <stdexcept>

namespace faker
{
Expand All @@ -26,8 +26,7 @@ std::string FormatHelper::fillTokenValues(const std::string& format,

if (foundTokenGenerator == tokenValueGenerators.end())
{
throw errors::TokenGeneratorNotFoundError{
FormatHelper::format("Generator not found for token {}.", token)};
throw std::runtime_error{FormatHelper::format("Generator not found for token {}.", token)};
}

filledFormat += foundTokenGenerator->second();
Expand Down
File renamed without changes.
File renamed without changes.
11 changes: 0 additions & 11 deletions src/common/errors/TokenGeneratorNotFoundError.h

This file was deleted.

16 changes: 6 additions & 10 deletions src/modules/airline/Airline.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
#include "faker-cxx/Airline.h"

#include "data/AircraftTypes.h"
#include "data/Airlines.h"
#include "data/Airplanes.h"
#include "data/Airports.h"
#include "data/Seat.h"
#include "AirlineData.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "faker-cxx/String.h"

namespace faker
{
std::string Airline::aircraftType()
std::string_view Airline::aircraftType()
{
return Helper::arrayElement<std::string>(aircraftTypes);
return Helper::arrayElement(aircraftTypes);
}

Airline::Airplane Airline::airplane()
{
return Helper::arrayElement<Airplane>(airplanes);
return Helper::arrayElement(airplanes);
}

Airline::AirlineInfo Airline::airline()
{
return Helper::arrayElement<Airline::AirlineInfo>(airlines);
return Helper::arrayElement(airlines);
}

Airline::Airport Airline::airport()
{
return Helper::arrayElement<Airline::Airport>(airports);
return Helper::arrayElement(airports);
}

std::string Airline::seat(Airline::AircraftType aircraftType)
Expand Down
Loading

0 comments on commit dc973bd

Please sign in to comment.