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

BUG: ReadCSVFileFilter-Replace illegal characters in headers. #977

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ enum class IssueCodes
IGNORED_TUPLE_DIMS = -200
};

StringVector RemoveIllegalCharacters(StringVector& headers)
{
for(int i = 0; i < headers.size(); i++)
{
auto& headerName = headers[i];
// Replace all illegal characters with '_' character. The header names become array names which is the issue.
// This should have been taken care of in the GUI, but if someone is trying this from Python they will not have done that
// or if they are just reading it in through nxrunner.
headerName = StringUtilities::replace(headerName, "&", "_");
headerName = StringUtilities::replace(headerName, ":", "_");
headerName = StringUtilities::replace(headerName, "/", "_");
headerName = StringUtilities::replace(headerName, "\\", "_");
headerName = StringUtilities::replace(headerName, "\"", "");
imikejackson marked this conversation as resolved.
Show resolved Hide resolved
}
return headers;
}
// -----------------------------------------------------------------------------
Result<OutputActions> validateExistingGroup(const DataPath& groupPath, const DataStructure& dataStructure, const std::vector<std::string>& headers)
{
Expand Down Expand Up @@ -530,9 +546,11 @@ IFilter::PreflightResult ReadCSVFileFilter::preflightImpl(const DataStructure& d
return {MakeErrorResult<OutputActions>(to_underlying(IssueCodes::INCORRECT_MASK_COUNT), errMsg), {}};
}

headers = RemoveIllegalCharacters(headers);

for(int i = 0; i < headers.size(); i++)
{
const auto& headerName = headers[i];
auto& headerName = headers[i];
if(headerName.empty())
{
std::string errMsg = fmt::format("The header for column #{} is empty. Please fill in a header for column #{}.", i + 1, i + 1);
Expand Down Expand Up @@ -651,6 +669,8 @@ Result<> ReadCSVFileFilter::executeImpl(DataStructure& dataStructure, const Argu
headers = readCSVData.customHeaders;
}

headers = RemoveIllegalCharacters(headers);

DataPath groupPath = createdDataGroup;
if(useExistingGroup)
{
Expand Down
29 changes: 18 additions & 11 deletions src/Plugins/SimplnxCore/test/ReadCSVFileTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,16 @@ void TestCase_TestImporterData_Error(const std::string& inputFilePath, usize sta

// Execute the filter and check the result
auto executeResult = filter.execute(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result);
REQUIRE(executeResult.result.errors().size() == 1);
REQUIRE(executeResult.result.errors()[0].code == expectedErrorCode);
if(expectedErrorCode == 0)
{
SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result);
}
else
{
SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result);
REQUIRE(executeResult.result.errors().size() == 1);
REQUIRE(executeResult.result.errors()[0].code == expectedErrorCode);
}
}

TEST_CASE("SimplnxCore::ReadCSVFileFilter (Case 1): Valid filter execution")
Expand Down Expand Up @@ -456,23 +463,23 @@ TEST_CASE("SimplnxCore::ReadCSVFileFilter (Case 5): Invalid filter execution - I

std::vector<std::string> illegalHeaders = {"Illegal/Header"};
CreateTestDataFile(tmp_file, v, illegalHeaders);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {}, {DataType::int8}, {false}, tupleDims, v, k_IllegalNames);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::CUSTOM, 1, {','}, illegalHeaders, {DataType::int8}, {false}, tupleDims, v, k_IllegalNames);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {}, {DataType::int8}, {false}, tupleDims, v, 0);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::CUSTOM, 1, {','}, illegalHeaders, {DataType::int8}, {false}, tupleDims, v, 0);

illegalHeaders = {"Illegal\\Header"};
CreateTestDataFile(tmp_file, v, illegalHeaders);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {}, {DataType::int8}, {false}, tupleDims, v, k_IllegalNames);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::CUSTOM, 1, {','}, illegalHeaders, {DataType::int8}, {false}, tupleDims, v, k_IllegalNames);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {}, {DataType::int8}, {false}, tupleDims, v, 0);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::CUSTOM, 1, {','}, illegalHeaders, {DataType::int8}, {false}, tupleDims, v, 0);

illegalHeaders = {"Illegal&Header"};
CreateTestDataFile(tmp_file, v, illegalHeaders);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {}, {DataType::int8}, {false}, tupleDims, v, k_IllegalNames);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::CUSTOM, 1, {','}, illegalHeaders, {DataType::int8}, {false}, tupleDims, v, k_IllegalNames);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {}, {DataType::int8}, {false}, tupleDims, v, 0);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::CUSTOM, 1, {','}, illegalHeaders, {DataType::int8}, {false}, tupleDims, v, 0);

illegalHeaders = {"Illegal:Header"};
CreateTestDataFile(tmp_file, v, illegalHeaders);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {}, {DataType::int8}, {false}, tupleDims, v, k_IllegalNames);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::CUSTOM, 1, {','}, illegalHeaders, {DataType::int8}, {false}, tupleDims, v, k_IllegalNames);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {}, {DataType::int8}, {false}, tupleDims, v, 0);
TestCase_TestImporterData_Error(tmp_file.string(), 2, ReadCSVData::HeaderMode::CUSTOM, 1, {','}, illegalHeaders, {DataType::int8}, {false}, tupleDims, v, 0);

fs::remove(tmp_file);

Expand Down
2 changes: 1 addition & 1 deletion src/simplnx/DataStructure/DataPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ DataPath::DataPath(std::vector<std::string> path)
{
if(!DataObject::IsValidName(item) && !item.empty())
{
throw std::invalid_argument("DataPath: Invalid DataObject name");
throw std::invalid_argument(fmt::format("DataPath: Invalid DataObject name - [{}]. One of the DataObject names contains the '/' character.", fmt::join(m_Path, ",")));
imikejackson marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/simplnx/Utilities/StringUtilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,24 @@ inline constexpr StringLiteral k_Whitespaces = " \t\f\v\n\r";

/**
* @brief Replace characters in a string. If 'from' is empty, the origin string is returned.
* @param str Input String
* @param inputString Input String
* @param from Characters to replace (These characters are being replaced)
* @param to The characters to be used as the replacement
* @return The modified string
*/
inline std::string replace(std::string str, std::string_view from, std::string_view to)
inline std::string replace(std::string inputString, std::string_view from, std::string_view to)
{
usize startPos = 0;
if(from.empty())
{
return str;
return inputString;
}
while((startPos = str.find(from, startPos)) != std::string::npos)
while((startPos = inputString.find(from, startPos)) != std::string::npos)
{
str.replace(startPos, from.length(), to);
inputString.replace(startPos, from.length(), to);
startPos += to.length();
}
return str;
return inputString;
imikejackson marked this conversation as resolved.
Show resolved Hide resolved
}

inline std::string ltrim(std::string_view str)
Expand Down
Loading