From f7bdc4e52cb987687c11733dac1e4d14a6152223 Mon Sep 17 00:00:00 2001 From: Mrognor Date: Wed, 4 Dec 2024 13:50:15 +0300 Subject: [PATCH] Add properly AddUniqueKey result --- Source/Vault.h | 6 ++---- Source/Vault.hpp | 25 +++++++++++++++++++------ Source/VaultOperationResult.h | 1 + Tests/VaultUnitTest.cpp | 10 +++++----- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Source/Vault.h b/Source/Vault.h index a6a54be..6a3c02d 100644 --- a/Source/Vault.h +++ b/Source/Vault.h @@ -192,7 +192,7 @@ namespace mvlt \return Returns false if such a key already exists, otherwise it returns true */ template - bool AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey, + VaultOperationResult AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey, std::function uniqueKeyFunction) noexcept; /** @@ -267,7 +267,7 @@ namespace mvlt \return Returns false if such a key already exists, otherwise it returns true */ template - bool AddUniqueKey(const std::string& key, std::function uniqueKeyFunction) noexcept; + VaultOperationResult AddUniqueKey(const std::string& key, std::function uniqueKeyFunction) noexcept; /** \brief Template method to update default key value @@ -665,11 +665,9 @@ namespace mvlt \brief A method for saving data to a table file. The file format is csv \param [in] fileName The file name to save the data, the extension must be specified manually - \param [in] keys a vector with the keys to save. The keys will be stored in the same order as in the vector \param [in] separator The symbol that will be used to separate the record elements in the file \param [in] isSaveKey A variable that determines whether to save keys to a file - The validity of the keys is provided by the user. In case of a non-existent key, an empty column will be written to the file. \return It will return true if it was possible to open the file and write the data, otherwise it will return false */ bool SaveToFile(const std::string& fileName, const std::vector keys = {}, const std::string& separator = ",", const bool& isSaveKey = true) const noexcept; diff --git a/Source/Vault.hpp b/Source/Vault.hpp index bbf49d6..1d77a46 100644 --- a/Source/Vault.hpp +++ b/Source/Vault.hpp @@ -346,19 +346,27 @@ namespace mvlt } template - bool Vault::AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey, + VaultOperationResult Vault::AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey, std::function uniqueKeyFunction) noexcept { static_assert(!std::is_array::value, "It is not possible to use a c array as a key value. \n\ If you want to use a string as a key, you must specialize the function with a string. Like this: \n\ AddKey(\"Key\", \"Value\") or AddKey(\"Key\", std::string(\"Value\"))"); + VaultOperationResult res; + res.Key = key; + res.RequestedType = typeid(T); + // Lock Vault to write WriteLock writeLock(RecursiveReadWriteMtx); // If the key was added earlier if (KeysTypes.find(key) != KeysTypes.end()) - return false; + { + res.IsOperationSuccess = false; + res.ResultCode = VaultOperationResultCode::DuplicateKey; + return res; + } // Create new hash map to store data with template T key UnorderedMap* TtoVaultRecordHashMap = new UnorderedMap(!isUniqueKey); @@ -417,7 +425,10 @@ namespace mvlt { VaultHashMapStructure.EraseData(key); VaultMapStructure.EraseData(key); - return false; + + res.IsOperationSuccess = false; + res.ResultCode = VaultOperationResultCode::UniqueKeyValueAlredyInSet; + return res; } } else @@ -538,17 +549,19 @@ namespace mvlt } } - return true; + res.IsOperationSuccess = true; + res.ResultCode = VaultOperationResultCode::Success; + return res; } template bool Vault::AddKey(const std::string& key, const T& defaultKeyValue) noexcept { - return AddKey(key, defaultKeyValue, false, {[&](std::size_t counter, const VaultRecordRef&) -> T{ return defaultKeyValue; }}); + return AddKey(key, defaultKeyValue, false, {[&](std::size_t counter, const VaultRecordRef&) -> T{ return defaultKeyValue; }}).IsOperationSuccess; } template - bool Vault::AddUniqueKey(const std::string& key, std::function uniqueKeyFunction) noexcept + VaultOperationResult Vault::AddUniqueKey(const std::string& key, std::function uniqueKeyFunction) noexcept { return AddKey(key, T(), true, uniqueKeyFunction); } diff --git a/Source/VaultOperationResult.h b/Source/VaultOperationResult.h index eef5004..f17d5cb 100644 --- a/Source/VaultOperationResult.h +++ b/Source/VaultOperationResult.h @@ -16,6 +16,7 @@ namespace mvlt /// \brief Enum with all error handling codes enum class VaultOperationResultCode { + DuplicateKey = -8, ///< This code is returned when trying to add key which alredy in vault TryToUpdateUniqueKey = -7, ///< This code is returned when trying to update default value in an unique key RecordAlredyInSet = -6, ///< This code is returned when trying to add an record to the set when it is already in it ParentVaultNotMatch = -5, ///< This code is returned during operations on VaultRecordSet when objects depend on different Vaults diff --git a/Tests/VaultUnitTest.cpp b/Tests/VaultUnitTest.cpp index e984899..4628060 100644 --- a/Tests/VaultUnitTest.cpp +++ b/Tests/VaultUnitTest.cpp @@ -22,10 +22,10 @@ void Vault_AddUniqueKey_Test() // Add to empty Vault // Correct add new key - TEST_ASSERT(vlt.AddUniqueKey("A", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast(counter); }}) == true, "Failed to add new unique key"); + TEST_ASSERT(vlt.AddUniqueKey("A", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast(counter); }}).IsOperationSuccess == true, "Failed to add new unique key"); // Incorrect try to add exist key - TEST_ASSERT(vlt.AddUniqueKey("A", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast(counter); }}) == false, "Error when try to add key with existing name"); + TEST_ASSERT(vlt.AddUniqueKey("A", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast(counter); }}).IsOperationSuccess == false, "Error when try to add key with existing name"); // Fill vault for (int i = 0; i < 10; ++i) @@ -35,10 +35,10 @@ void Vault_AddUniqueKey_Test() // Add to filled vault // Correct add new key - TEST_ASSERT(vlt.AddUniqueKey("B", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast(counter * counter); }}) == true, "Failed to add new unique key"); + TEST_ASSERT(vlt.AddUniqueKey("B", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast(counter * counter); }}).IsOperationSuccess == true, "Failed to add new unique key"); // Incorrect try to add exist key - TEST_ASSERT(vlt.AddUniqueKey("C", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return 1; }}) == false, "Error when try to add key with existing name"); + TEST_ASSERT(vlt.AddUniqueKey("C", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return 1; }}).IsOperationSuccess == false, "Error when try to add key with existing name"); // Check lambda TEST_ASSERT(vlt.AddUniqueKey("C", {[](std::size_t counter, const VaultRecordRef& vrf) -> int @@ -46,7 +46,7 @@ void Vault_AddUniqueKey_Test() int i; vrf.GetData("B", i); return static_cast(2 * i); - }}) == true, "Failed to add new unique key"); + }}).IsOperationSuccess == true, "Failed to add new unique key"); VaultRecordRef vrf; for (int i = 0; i < 10; ++i)