Skip to content

Commit

Permalink
Initial overwrite updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq committed Apr 12, 2024
1 parent a139211 commit ea70dbe
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class IOHandler
// rename grayscale array to reflect original
{
auto& gray = m_DataStructure.getDataRefAs<IDataArray>(imageDataPath.getParent().createChildPath("gray" + imageDataPath.getTargetName()));
if(!gray.canRename(imageDataPath.getTargetName()))
if(gray.canRename(imageDataPath.getTargetName()) != 1)
{
return MakeErrorResult(-18543, fmt::format("Unable to rename the grayscale array to {}", imageDataPath.getTargetName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Result<> ReadImageStack(DataStructure& dataStructure, const DataPath& imageGeomP
// rename grayscale array to reflect original
{
auto& gray = importedDataStructure.getDataRefAs<IDataArray>(imageDataPath.getParent().createChildPath("gray" + imageDataPath.getTargetName()));
if(!gray.canRename(imageDataPath.getTargetName()))
if(gray.canRename(imageDataPath.getTargetName()) != 1)
{
return MakeErrorResult(-64543, fmt::format("Unable to rename the internal grayscale array to {}", imageDataPath.getTargetName()));
}
Expand Down
14 changes: 7 additions & 7 deletions src/simplnx/DataStructure/DataObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,38 +162,38 @@ std::string DataObject::getName() const
return m_Name;
}

bool DataObject::canRename(const std::string& name) const
int DataObject::canRename(const std::string& name) const
{

if(name == getName())
{
return true;
return 1;
}

if(!IsValidName(name))
{
return false;
return 0;
}

const auto* dataStructPtr = getDataStructure();
if(dataStructPtr == nullptr)
{
return false;
return 0;
}

return !std::any_of(m_ParentList.cbegin(), m_ParentList.cend(), [dataStructPtr, name](IdType parentId) {
return (std::any_of(m_ParentList.cbegin(), m_ParentList.cend(), [dataStructPtr, name](IdType parentId) {
const auto* baseGroupPtr = dataStructPtr->getDataAs<BaseGroup>(parentId);
if(baseGroupPtr == nullptr)
{
std::cout << "DataObject::canRename(name=" << name << ") cannot get baseGroup from parentId = " << parentId << std::endl;
}
return baseGroupPtr != nullptr && baseGroupPtr->contains(name);
});
})) ? 2 : 0;
}

bool DataObject::rename(const std::string& name)
{
if(!canRename(name))
if(canRename(name) != 1)
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/simplnx/DataStructure/DataObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ class SIMPLNX_EXPORT DataObject
* @brief Checks and returns if the DataObject can be renamed to the provided
* value.
* @param name
* @return bool
* @return int: 0 = false, 1 = true, 2 = duplicate object found
*/
bool canRename(const std::string& name) const;
int canRename(const std::string& name) const;

/**
* @brief Attempts to rename the DataObject to the provided value.
Expand Down
28 changes: 26 additions & 2 deletions src/simplnx/Filter/Actions/RenameDataAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ using namespace nx::core;

namespace nx::core
{
RenameDataAction::RenameDataAction(const DataPath& path, const std::string& newName)
RenameDataAction::RenameDataAction(const DataPath& path, const std::string& newName, bool overwrite)
: m_NewName(newName)
, m_Path(path)
, m_Overwrite(overwrite)
{
}

Expand All @@ -27,11 +28,28 @@ Result<> RenameDataAction::apply(DataStructure& dataStructure, Mode mode) const
return MakeErrorResult(-6601, ss);
}

if(!dataObject->canRename(m_NewName))
int validRename = dataObject->canRename(m_NewName);
if(validRename == 0)
{
std::string ss = fmt::format("{}Could not rename DataObject at '{}' to '{}'", prefix, m_Path.toString(), m_NewName);
return MakeErrorResult(-6602, ss);
}
if(validRename == 2)
{
if(m_Overwrite)
{
if(mode == Mode::Preflight)
{
std::string ss = fmt::format("{}Another object exists with that name, will overwrite destroying other DataObject at '{}' and replacing it with '{}'", prefix, m_NewName, m_Path.toString());
Result<>{}.warnings().emplace_back(Warning{-6603,ss});
}
}
else
{
std::string ss = fmt::format("{}Another object exists with that name, will not rename DataObject at '{}' to '{}'", prefix, m_Path.toString(), m_NewName);
return MakeErrorResult(-6604, ss);
}
}

dataObject->rename(m_NewName);
return {};
Expand All @@ -51,4 +69,10 @@ const DataPath& RenameDataAction::path() const
{
return m_Path;
}

bool RenameDataAction::overwrite() const
{
return m_Overwrite;
}

} // namespace nx::core
9 changes: 8 additions & 1 deletion src/simplnx/Filter/Actions/RenameDataAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SIMPLNX_EXPORT RenameDataAction : public IDataAction
public:
RenameDataAction() = delete;

RenameDataAction(const DataPath& path, const std::string& newName);
RenameDataAction(const DataPath& path, const std::string& newName, bool overwrite = false);

~RenameDataAction() noexcept override;

Expand Down Expand Up @@ -48,8 +48,15 @@ class SIMPLNX_EXPORT RenameDataAction : public IDataAction
*/
const DataPath& path() const;

/**
* @brief Returns the overwrite value
* @return
*/
bool overwrite() const;

private:
std::string m_NewName;
DataPath m_Path;
bool m_Overwrite;
};
} // namespace nx::core

0 comments on commit ea70dbe

Please sign in to comment.