From 1d008016ae5f868909b01bd2a53dd6f8e6558ba5 Mon Sep 17 00:00:00 2001 From: Ishan Tyagi Date: Thu, 13 Jun 2024 17:44:24 -0400 Subject: [PATCH 1/2] refactored data --- src/modules/system/System.cpp | 48 +++--- src/modules/system/SystemData.cpp | 175 ++++++++++++++++++++++ src/modules/system/SystemData.h | 18 +++ src/modules/system/data/CommonInterface.h | 13 -- src/modules/system/data/CronDayOfWeek.h | 9 -- src/modules/system/data/DirectoryPath.h | 58 ------- src/modules/system/data/MimeTypes.h | 116 -------------- tests/modules/system/SystemTest.cpp | 24 +-- 8 files changed, 228 insertions(+), 233 deletions(-) create mode 100644 src/modules/system/SystemData.cpp create mode 100644 src/modules/system/SystemData.h delete mode 100644 src/modules/system/data/CommonInterface.h delete mode 100644 src/modules/system/data/CronDayOfWeek.h delete mode 100644 src/modules/system/data/DirectoryPath.h delete mode 100644 src/modules/system/data/MimeTypes.h diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 347f0b620..961b7b6a2 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -10,10 +10,7 @@ #include #include "../src/common/StringHelper.h" -#include "data/CommonInterface.h" -#include "data/CronDayOfWeek.h" -#include "data/DirectoryPath.h" -#include "data/MimeTypes.h" +#include "SystemData.h" #include "faker-cxx/Datatype.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Internet.h" @@ -25,13 +22,13 @@ namespace faker { namespace { -const std::unordered_map fileTypeToStringMapping{{FileType::Video, "video"}, - {FileType::Audio, "audio"}, - {FileType::Image, "image"}, - {FileType::Text, "text"}, - {FileType::Application, "application"}}; +const std::unordered_map fileTypeToStringMapping{{FileType::Video, "video"}, + {FileType::Audio, "audio"}, + {FileType::Image, "image"}, + {FileType::Text, "text"}, + {FileType::Application, "application"}}; -std::string extension(const std::string& mimeType) +std::string_view extension(std::string_view mimeType) { const auto it = mimeTypesExtensions.find(mimeType); if (it == mimeTypesExtensions.end()) @@ -95,7 +92,7 @@ std::string System::fileExtension(const std::optional& mimeType) const auto mt = mime.substr(0, pos); if (mimeTypeName == mt) { - extensions.push_back(mime.substr(pos + 1)); + extensions.push_back(std::string(mime.substr(pos + 1))); } } @@ -103,7 +100,7 @@ std::string System::fileExtension(const std::optional& mimeType) } else { - std::set extensionSet; + std::set extensionSet; for (const auto& mimeTypeName : mimeTypes) { @@ -136,14 +133,14 @@ std::string System::commonFileName(const std::optional& ext) std::string System::commonFileExtension() { - auto mimeType = Helper::arrayElement(commonMimeTypes); + auto mimeType = Helper::arrayElement(commonMimeTypes); - return extension(mimeType); + return std::string(extension(mimeType)); } -std::string System::mimeType() +std::string System::mimeType() { - std::vector mimeTypeKeys; + std::vector mimeTypeKeys; mimeTypeKeys.reserve(mimeTypes.size()); @@ -152,12 +149,12 @@ std::string System::mimeType() mimeTypeKeys.push_back(entry); } - return Helper::arrayElement(mimeTypeKeys); + return std::string(Helper::arrayElement(mimeTypeKeys)); } std::string System::commonFileType() { - return Helper::arrayElement(commonFileTypes); + return std::string(Helper::arrayElement(commonFileTypes)); } std::string System::fileType() @@ -168,7 +165,7 @@ std::string System::fileType() for (const auto& entry : localMimeTypes) { - const std::string& m = entry; + const std::string& m = std::string(entry); size_t pos = m.find('/'); @@ -186,7 +183,7 @@ std::string System::fileType() std::string System::directoryPath() { - return Helper::arrayElement(directoryPaths); + return std::string(Helper::arrayElement(directoryPaths)); } std::string System::filePath() @@ -209,10 +206,10 @@ std::string System::semver() std::string System::networkInterface(const std::optional& options) { - const auto defaultInterfaceType = Helper::arrayElement(commonInterfaceTypes); - const std::string defaultInterfaceSchema = Helper::objectKey(commonInterfaceSchemas); + const auto defaultInterfaceType = Helper::arrayElement(commonInterfaceTypes); + const std::string defaultInterfaceSchema = std::string(Helper::objectKey(commonInterfaceSchemas)); - std::string interfaceType = defaultInterfaceType; + std::string interfaceType = std::string(defaultInterfaceType); std::string interfaceSchema = defaultInterfaceSchema; if (options.has_value()) @@ -253,7 +250,7 @@ std::string System::networkInterface(const std::optional([&]() { return "d" + digit(); }); } - return prefix + interfaceType + commonInterfaceSchemas.at(interfaceSchema) + suffix; + return prefix + interfaceType + std::string(commonInterfaceSchemas.at(interfaceSchema)) + suffix; } std::string System::cron(const CronOptions& options) @@ -266,7 +263,8 @@ std::string System::cron(const CronOptions& options) std::vector months = {std::to_string(Number::integer(1, 12)), "*"}; std::vector daysOfWeek = { std::to_string(Number::integer(6)), - cronDayOfWeek[static_cast(Number::integer(0, static_cast(cronDayOfWeek.size() - 1)))], "*", + std::string(cronDayOfWeek[static_cast(Number::integer(0, static_cast(cronDayOfWeek.size() - 1)))]), + "*", "?"}; std::vector years; diff --git a/src/modules/system/SystemData.cpp b/src/modules/system/SystemData.cpp new file mode 100644 index 000000000..1564b02fb --- /dev/null +++ b/src/modules/system/SystemData.cpp @@ -0,0 +1,175 @@ +#include "SystemData.h" + +#include +#include +#include + +#include "faker-cxx/System.h" + +namespace faker +{ +const std::array commonInterfaceTypes = {"en", "wl", "ww"}; + +const std::unordered_map commonInterfaceSchemas = { + {"index", "o"}, {"slot", "s"}, {"mac", "x"}, {"pci", "p"}}; + +const std::array cronDayOfWeek = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; + +const std::array directoryPaths = {"/etc/mail", + "/var/log", + "/usr/bin", + "/tmp", + "/usr/local/lib", + "/var/www/html", + "/opt/app", + "/home/user/documents", + "/usr/share/fonts", + "/var/cache/apt", + "/etc/nginx", + "/usr/local/bin", + "/var/tmp", + "/opt/data", + "/home/user/pictures", + "/usr/local/include", + "/var/www/cgi-bin", + "/etc/ssh", + "/usr/local/share", + "/var/spool/mail", + "/opt/logs", + "/home/user/music", + "/usr/local/libexec", + "/var/www/cgi-bin", + "/etc/ssl", + "/usr/local/var", + "/var/spool/cron", + "/opt/config", + "/home/user/videos", + "/usr/local/sbin", + "/var/www/docs", + "/etc/apache2", + "/usr/local/games", + "/var/run", + "/opt/bin", + "/home/user/downloads", + "/usr/local/man", + "/var/www/icons", + "/etc/mysql", + "/usr/local/src", + "/var/lock", + "/opt/scripts", + "/home/user/public_html", + "/usr/local/etc", + "/var/www/logs", + "/etc/httpd", + "/usr/local/share/man", + "/var/log/apache2", + "/opt/files", + "/home/user/backups"}; + +const std::array mimeTypes = {"application/atom+xml", + "application/font-woff", + "application/gzip", + "application/java-archive", + "application/javascript", + "application/json", + "application/ld+json", + "application/msword", + "application/octet-stream", + "application/ogg", + "application/pdf", + "application/rdf+xml", + "application/rtf", + "application/vnd.apple.mpegurl", + "application/vnd.ms-excel", + "application/vnd.ms-fontobject", + "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/x-7z-compressed", + "application/x-font-ttf", + "application/x-javascript", + "application/x-mpegURL", + "application/x-rar-compressed", + "application/x-shockwave-flash", + "application/x-tar", + "application/x-www-form-urlencoded", + "application/xhtml+xml", + "application/xml", + "application/zip", + "audio/aac", + "audio/midi", + "audio/ogg", + "audio/wav", + "audio/webm", + "audio/mpeg", + "font/woff", + "font/woff2", + "image/apng", + "image/bmp", + "image/gif", + "image/jpeg", + "image/png", + "image/svg+xml", + "image/tiff", + "image/webp", + "image/x-icon", + "multipart/form-data", + "text/calendar", + "text/css", + "text/csv", + "text/html", + "text/javascript", + "text/plain", + "text/xml", + "video/mp4", + "video/3gpp", + "video/3gpp2", + "video/mp2t", + "video/mpeg", + "video/ogg", + "video/webm", + "video/x-msvideo", + "video/x-flv"}; + +const std::unordered_map mimeTypesExtensions{ + {"application/atom+xml", "xml"}, + {"application/font-woff", "woff"}, + {"application/gzip", "gz"}, + {"application/java-archive", "jar"}, + {"application/javascript", "js"}, + {"application/ld+json", "jsonld"}, + {"application/msword", "doc"}, + {"application/octet-stream", "bin"}, + {"application/ogg", "ogx"}, + {"application/vnd.ms-excel", "xls"}, + {"application/vnd.ms-fontobject", "eot"}, + {"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx"}, + {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"}, + {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx"}, + {"application/x-7z-compressed", "7z"}, + {"application/x-tar", "tart"}, + {"application/xhtml+xml", "xhtml"}, + + {"audio/ogg", "oga"}, + {"audio/webm", "weba"}, + {"audio/mpeg", "mp3"}, + + {"image/svg+xml", "svg"}, + + {"text/calendar", "ics"}, + {"text/javascript", "js"}, + {"text/plain", "txt"}, + + {"video/3gpp", "3gp"}, + {"video/3gpp2", "3g2"}, + {"video/mp2t", "ts"}, + {"video/ogg", "ogv"}, + {"video/x-msvideo", "avi"}}; + +const std::array commonMimeTypes = {"application/pdf", "audio/mpeg", "audio/wav", + "image/png", "image/jpeg", "image/gif", + "video/mp4", "video/mpeg", "text/html"}; + +const std::array commonFileTypes = {"video", "audio", "image", "text", "application"}; + +} diff --git a/src/modules/system/SystemData.h b/src/modules/system/SystemData.h new file mode 100644 index 000000000..cb6165471 --- /dev/null +++ b/src/modules/system/SystemData.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +#include "faker-cxx/System.h" + +namespace faker +{ +extern const std::array commonInterfaceTypes; +extern const std::unordered_map commonInterfaceSchemas; +extern const std::array cronDayOfWeek; +extern const std::array directoryPaths; +extern const std::array mimeTypes; +extern const std::unordered_map mimeTypesExtensions; +extern const std::array commonMimeTypes; +extern const std::array commonFileTypes; +} diff --git a/src/modules/system/data/CommonInterface.h b/src/modules/system/data/CommonInterface.h deleted file mode 100644 index e5ce1fed2..000000000 --- a/src/modules/system/data/CommonInterface.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace faker -{ -const std::vector commonInterfaceTypes = {"en", "wl", "ww"}; - -const std::unordered_map commonInterfaceSchemas = { - {"index", "o"}, {"slot", "s"}, {"mac", "x"}, {"pci", "p"}}; -} diff --git a/src/modules/system/data/CronDayOfWeek.h b/src/modules/system/data/CronDayOfWeek.h deleted file mode 100644 index b6509a275..000000000 --- a/src/modules/system/data/CronDayOfWeek.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector cronDayOfWeek = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; -} \ No newline at end of file diff --git a/src/modules/system/data/DirectoryPath.h b/src/modules/system/data/DirectoryPath.h deleted file mode 100644 index 335566513..000000000 --- a/src/modules/system/data/DirectoryPath.h +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include -#include - -namespace faker -{ -const std::vector directoryPaths = {"/etc/mail", - "/var/log", - "/usr/bin", - "/tmp", - "/usr/local/lib", - "/var/www/html", - "/opt/app", - "/home/user/documents", - "/usr/share/fonts", - "/var/cache/apt", - "/etc/nginx", - "/usr/local/bin", - "/var/tmp", - "/opt/data", - "/home/user/pictures", - "/usr/local/include", - "/var/www/cgi-bin", - "/etc/ssh", - "/usr/local/share", - "/var/spool/mail", - "/opt/logs", - "/home/user/music", - "/usr/local/libexec", - "/var/www/cgi-bin", - "/etc/ssl", - "/usr/local/var", - "/var/spool/cron", - "/opt/config", - "/home/user/videos", - "/usr/local/sbin", - "/var/www/docs", - "/etc/apache2", - "/usr/local/games", - "/var/run", - "/opt/bin", - "/home/user/downloads", - "/usr/local/man", - "/var/www/icons", - "/etc/mysql", - "/usr/local/src", - "/var/lock", - "/opt/scripts", - "/home/user/public_html", - "/usr/local/etc", - "/var/www/logs", - "/etc/httpd", - "/usr/local/share/man", - "/var/log/apache2", - "/opt/files", - "/home/user/backups"}; -} diff --git a/src/modules/system/data/MimeTypes.h b/src/modules/system/data/MimeTypes.h deleted file mode 100644 index 02689c789..000000000 --- a/src/modules/system/data/MimeTypes.h +++ /dev/null @@ -1,116 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace faker -{ -const std::vector mimeTypes = {"application/atom+xml", - "application/font-woff", - "application/gzip", - "application/java-archive", - "application/javascript", - "application/json", - "application/ld+json", - "application/msword", - "application/octet-stream", - "application/ogg", - "application/pdf", - "application/rdf+xml", - "application/rtf", - "application/vnd.apple.mpegurl", - "application/vnd.ms-excel", - "application/vnd.ms-fontobject", - "application/vnd.openxmlformats-officedocument.presentationml.presentation", - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - "application/x-7z-compressed", - "application/x-font-ttf", - "application/x-javascript", - "application/x-mpegURL", - "application/x-rar-compressed", - "application/x-shockwave-flash", - "application/x-tar", - "application/x-www-form-urlencoded", - "application/xhtml+xml", - "application/xml", - "application/zip", - "audio/aac", - "audio/midi", - "audio/ogg", - "audio/wav", - "audio/webm", - "audio/mpeg", - "font/woff", - "font/woff2", - "image/apng", - "image/bmp", - "image/gif", - "image/jpeg", - "image/png", - "image/svg+xml", - "image/tiff", - "image/webp", - "image/x-icon", - "multipart/form-data", - "text/calendar", - "text/css", - "text/csv", - "text/html", - "text/javascript", - "text/plain", - "text/xml", - "video/mp4", - "video/3gpp", - "video/3gpp2", - "video/mp2t", - "video/mpeg", - "video/ogg", - "video/webm", - "video/x-msvideo", - "video/x-flv"}; - -// Only contains non obvious extensions. -const std::unordered_map mimeTypesExtensions{ - {"application/atom+xml", "xml"}, - {"application/font-woff", "woff"}, - {"application/gzip", "gz"}, - {"application/java-archive", "jar"}, - {"application/javascript", "js"}, - {"application/ld+json", "jsonld"}, - {"application/msword", "doc"}, - {"application/octet-stream", "bin"}, - {"application/ogg", "ogx"}, - {"application/vnd.ms-excel", "xls"}, - {"application/vnd.ms-fontobject", "eot"}, - {"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx"}, - {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"}, - {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx"}, - {"application/x-7z-compressed", "7z"}, - {"application/x-tar", "tart"}, - {"application/xhtml+xml", "xhtml"}, - - {"audio/ogg", "oga"}, - {"audio/webm", "weba"}, - {"audio/mpeg", "mp3"}, - - {"image/svg+xml", "svg"}, - - {"text/calendar", "ics"}, - {"text/javascript", "js"}, - {"text/plain", "txt"}, - - {"video/3gpp", "3gp"}, - {"video/3gpp2", "3g2"}, - {"video/mp2t", "ts"}, - {"video/ogg", "ogv"}, - {"video/x-msvideo", "avi"}}; - -const std::vector commonMimeTypes = {"application/pdf", "audio/mpeg", "audio/wav", - "image/png", "image/jpeg", "image/gif", - "video/mp4", "video/mpeg", "text/html"}; - -const std::vector commonFileTypes = {"video", "audio", "image", "text", "application"}; - -} diff --git a/tests/modules/system/SystemTest.cpp b/tests/modules/system/SystemTest.cpp index bfa2e836e..199cd62c6 100644 --- a/tests/modules/system/SystemTest.cpp +++ b/tests/modules/system/SystemTest.cpp @@ -11,16 +11,16 @@ #include "gtest/gtest.h" -#include "system/data/MimeTypes.h" +#include "system/SystemData.h" using namespace ::testing; using namespace faker; -const std::unordered_map fileTypeToStringMapping{{FileType::Video, "video"}, - {FileType::Audio, "audio"}, - {FileType::Image, "image"}, - {FileType::Text, "text"}, - {FileType::Application, "application"}}; +const std::unordered_map fileTypeToStringMapping{{FileType::Video, "video"}, + {FileType::Audio, "audio"}, + {FileType::Image, "image"}, + {FileType::Text, "text"}, + {FileType::Application, "application"}}; class SystemTest : public Test { @@ -67,7 +67,7 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) auto text = FileType::Text; auto application = FileType::Application; - std::vector imageExtensions; + std::vector imageExtensions; for (const auto& mimeType : mimeTypes) { @@ -79,7 +79,7 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) } } - std::vector audioExtensions; + std::vector audioExtensions; for (const auto& mimeType : mimeTypes) { @@ -91,7 +91,7 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) } } - std::vector videoExtensions; + std::vector videoExtensions; for (const auto& mimeType : mimeTypes) { @@ -102,7 +102,7 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) videoExtensions.push_back(mimeType.substr(pos + 1)); } } - std::vector textExtensions; + std::vector textExtensions; for (const auto& mimeType : mimeTypes) { @@ -114,7 +114,7 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) } } - std::vector applicationExtensions; + std::vector applicationExtensions; for (const auto& mimeType : mimeTypes) { @@ -174,7 +174,7 @@ TEST_F(SystemTest, FileTypeTest) std::set typeSet; for (const auto& entry : mimeTypes) { - const std::string& m = entry; + const std::string& m = std::string(entry); size_t pos = m.find('/'); if (pos != std::string::npos) { From e12334844081f112f907045345b4244ffe5a8295 Mon Sep 17 00:00:00 2001 From: Ishan Tyagi Date: Fri, 14 Jun 2024 18:44:52 -0400 Subject: [PATCH 2/2] fixed cmake build (no pre-commit) --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index da0dcb2a6..7469f22ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,7 @@ set(FAKER_SOURCES src/modules/sport/SportData.cpp src/modules/string/String.cpp src/modules/system/System.cpp + src/modules/system/SystemData.cpp src/modules/vehicle/Vehicle.cpp src/modules/vehicle/VehicleData.cpp src/modules/videoGame/VideoGame.cpp