From f1e3f6791d18f82e662a54ea407b5e5495e18a30 Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Wed, 6 Mar 2024 12:40:38 -0500 Subject: [PATCH 1/5] Multiple patch files --- source/base/StarAssets.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index 2c3d04b..422ea22 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -86,10 +86,21 @@ Assets::Assets(Settings settings, StringList assetSources) { m_assetSourcePaths.add(sourcePath, source); for (auto const& filename : source->assetPaths()) { - if (filename.endsWith(AssetsPatchSuffix, String::CaseInsensitive)) { - auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix)); - if (auto p = m_files.ptr(targetPatchFile)) - p->patchSources.append({filename, source}); + if (filename.contains(AssetsPatchSuffix, String::CaseInsensitive)) { + if (filename.endsWith(AssetsPatchSuffix, String::CaseInsensitive)) { + auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix)); + if (auto p = m_files.ptr(targetPatchFile)) + p->patchSources.append({filename, source}); + } else { + for (int i = 0; i < 10; i++) { + if (filename.endsWith(AssetsPatchSuffix + toString(i), String::CaseInsensitive)) { + auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix) + 1); + if (auto p = m_files.ptr(targetPatchFile)) + p->patchSources.append({filename, source}); + break; + } + } + } } auto& descriptor = m_files[filename]; descriptor.sourceName = filename; From 14ec64ace75c83238ee9f81fe4ea4c3c9eed09fb Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Thu, 7 Mar 2024 09:43:36 -0500 Subject: [PATCH 2/5] Added `jsonCompare` function --- source/core/StarJson.cpp | 30 ++++++++++++++++++++++++++++++ source/core/StarJson.hpp | 16 ++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/source/core/StarJson.cpp b/source/core/StarJson.cpp index 3feaf37..ed0e48f 100644 --- a/source/core/StarJson.cpp +++ b/source/core/StarJson.cpp @@ -1035,4 +1035,34 @@ Json jsonMerge(Json const& base, Json const& merger) { } } +bool jsonCompare(Json const& base, Json const& compare) { + if (base == compare) { + return true; + } else { + if (base.type() == Json::Type::Object && compare.type() == Json::Type::Object) { + for (auto const& c : compare.toObject()) { + if (!base.contains(c.first) || !jsonCompare(base.get(c.first), c.second)) + return false; + } + return true; + } + if (base.type() == Json::Type::Array && compare.type() == Json::Type::Array) { + for (auto const& c : compare.toArray()) { + bool similar = false; + for (auto const& b : base.toArray()) { + if (jsonCompare(c, b)) { + similar = true; + break; + } + } + if (!similar) + return false; + } + return true; + } + + return false; + } +} + } \ No newline at end of file diff --git a/source/core/StarJson.hpp b/source/core/StarJson.hpp index 4aa6f7b..51e0be7 100644 --- a/source/core/StarJson.hpp +++ b/source/core/StarJson.hpp @@ -355,6 +355,22 @@ Json jsonMergeQueryDef(String const& key, Json def, Json const& first, T const&. return def; } +// Compares the two given json values and returns a boolean, by the following +// rules (applied in order): If both values are identical, return true. If both +// values are not equal, check if they are objects. If they are objects, +// iterate through every pair in the comparing object and check if the key is +// in the base object. If the key is in the base object, then jsonCompare is +// called recursively on both values. If the base object does not contain the +// key, or the recursion fails, return false. Otherwise, return true. If they +// are not objects, check if they are arrays. If they are arrays, iterate +// through every value in the comparing object and then recursively call +// jsonCompare on every value in the base object until a match is found. If a +// match is found, break and move on to the next value in the comparing array. +// If a match is found for every value in the comparing array, return true. +// Otherwise, return false. If both values are not identical, and are not +// objects or arrays, return false. +bool jsonCompare(Json const& base, Json const& compare); + } template <> struct fmt::formatter : ostream_formatter {}; From c808d207c91e577d12907ea9723d500849e0d61f Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Thu, 7 Mar 2024 11:01:29 -0500 Subject: [PATCH 3/5] Added `search` operator to the `remove` operation --- source/core/StarJsonPatch.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/source/core/StarJsonPatch.cpp b/source/core/StarJsonPatch.cpp index 34c6a82..9ef11ff 100644 --- a/source/core/StarJsonPatch.cpp +++ b/source/core/StarJsonPatch.cpp @@ -66,7 +66,30 @@ namespace JsonPatching { } Json applyRemoveOperation(Json const& base, Json const& op) { - return JsonPath::Pointer(op.getString("path")).remove(base); + if (op.contains("search")) { + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(path); + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& e : searchArray.toArray()) { + if (jsonCompare(e, searchValue)) { + found = true; + break; + } + index++; + } + if (found) + searchArray = searchArray.eraseIndex(index); + return pointer.add(pointer.remove(base), searchArray); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + return JsonPath::Pointer(op.getString("path")).remove(base); + } } Json applyAddOperation(Json const& base, Json const& op) { From 9b4ca69e0e75e08c4b0587dad931654c9ac9ee2e Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Thu, 7 Mar 2024 12:31:48 -0500 Subject: [PATCH 4/5] Added the `search` operand to the `find`, `replace`, and `remove` operations --- source/core/StarJsonPatch.cpp | 99 ++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 14 deletions(-) diff --git a/source/core/StarJsonPatch.cpp b/source/core/StarJsonPatch.cpp index 9ef11ff..6ed425e 100644 --- a/source/core/StarJsonPatch.cpp +++ b/source/core/StarJsonPatch.cpp @@ -40,24 +40,49 @@ namespace JsonPatching { Json applyTestOperation(Json const& base, Json const& op) { auto path = op.getString("path"); - auto value = op.opt("value"); auto inverseTest = op.getBool("inverse", false); auto pointer = JsonPath::Pointer(path); try { - auto testValue = pointer.get(base); - if (!value) { - if (inverseTest) - throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", op.getString("path"))); - return base; - } + if (op.contains("search")) { + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + bool found = false; + for(auto& e : searchArray.toArray()) { + if (jsonCompare(e, searchValue)) { + found = true; + break; + } + } + if (found) { + if (inverseTest) + throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", searchValue)); + return base; + } else { + if (!inverseTest) + throw JsonPatchTestFail(strf("Test operation failure, could not find {}.", searchValue)); + return base; + } + } else { + throw JsonPatchException(strf("Search operation failure, value at '{}' is not an array.", path)); + } + } else { + auto value = op.opt("value"); + auto testValue = pointer.get(base); + if (!value) { + if (inverseTest) + throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", op.getString("path"))); + return base; + } - if ((value && (testValue == *value)) ^ inverseTest) { - return base; - } + if ((value && (testValue == *value)) ^ inverseTest) { + return base; + } - throw JsonPatchTestFail(strf("Test operation failure, expected {} found {}.", value, testValue)); + throw JsonPatchTestFail(strf("Test operation failure, expected {} found {}.", value, testValue)); + } } catch (JsonPath::TraversalException& e) { if (inverseTest) return base; @@ -93,12 +118,58 @@ namespace JsonPatching { } Json applyAddOperation(Json const& base, Json const& op) { - return JsonPath::Pointer(op.getString("path")).add(base, op.get("value")); + if (op.contains("search")) { + Json value = op.get("value"); + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(path); + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + bool found = false; + for (auto& e : searchArray.toArray()) { + if (jsonCompare(e, searchValue)) { + found = true; + break; + } + } + if (found) + searchArray = searchArray.append(value); + return pointer.add(pointer.remove(base), searchArray); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + return JsonPath::Pointer(op.getString("path")).add(base, op.get("value")); + } } Json applyReplaceOperation(Json const& base, Json const& op) { - auto pointer = JsonPath::Pointer(op.getString("path")); - return pointer.add(pointer.remove(base), op.get("value")); + if (op.contains("search")) { + Json value = op.get("value"); + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(path); + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& e : searchArray.toArray()) { + if (jsonCompare(e, searchValue)) { + found = true; + break; + } + index++; + } + if (found) + searchArray = searchArray.set(index, value); + return pointer.add(pointer.remove(base), searchArray); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + auto pointer = JsonPath::Pointer(op.getString("path")); + return pointer.add(pointer.remove(base), op.get("value")); + } } Json applyMoveOperation(Json const& base, Json const& op) { From 15a12c06a66adca4c953be8af6fc434cb6d2b156 Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Thu, 7 Mar 2024 17:28:34 -0500 Subject: [PATCH 5/5] Added the `search` operand to the `move` and `copy` operations, and added the new `merge` operation --- source/core/StarJsonPatch.cpp | 112 +++++++++++++++++++++++++++++----- source/core/StarJsonPatch.hpp | 5 +- 2 files changed, 100 insertions(+), 17 deletions(-) diff --git a/source/core/StarJsonPatch.cpp b/source/core/StarJsonPatch.cpp index 6ed425e..f5e7877 100644 --- a/source/core/StarJsonPatch.cpp +++ b/source/core/StarJsonPatch.cpp @@ -25,6 +25,7 @@ namespace JsonPatching { {"replace", std::bind(applyReplaceOperation, _1, _2)}, {"move", std::bind(applyMoveOperation, _1, _2)}, {"copy", std::bind(applyCopyOperation, _1, _2)}, + {"merge", std::bind(applyMergeOperation, _1, _2)}, }; Json applyOperation(Json const& base, Json const& op) { @@ -50,8 +51,8 @@ namespace JsonPatching { Json searchValue = op.get("search"); if (searchArray.type() == Json::Type::Array) { bool found = false; - for(auto& e : searchArray.toArray()) { - if (jsonCompare(e, searchValue)) { + for(auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { found = true; break; } @@ -99,8 +100,8 @@ namespace JsonPatching { if (searchArray.type() == Json::Type::Array) { size_t index = 0; bool found = false; - for (auto& e : searchArray.toArray()) { - if (jsonCompare(e, searchValue)) { + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { found = true; break; } @@ -126,8 +127,8 @@ namespace JsonPatching { Json searchValue = op.get("search"); if (searchArray.type() == Json::Type::Array) { bool found = false; - for (auto& e : searchArray.toArray()) { - if (jsonCompare(e, searchValue)) { + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { found = true; break; } @@ -144,17 +145,18 @@ namespace JsonPatching { } Json applyReplaceOperation(Json const& base, Json const& op) { + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(op.getString("path")); + if (op.contains("search")) { Json value = op.get("value"); - String path = op.getString("path"); - auto pointer = JsonPath::Pointer(path); Json searchArray = pointer.get(base); Json searchValue = op.get("search"); if (searchArray.type() == Json::Type::Array) { size_t index = 0; bool found = false; - for (auto& e : searchArray.toArray()) { - if (jsonCompare(e, searchValue)) { + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { found = true; break; } @@ -167,24 +169,102 @@ namespace JsonPatching { throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); } } else { - auto pointer = JsonPath::Pointer(op.getString("path")); return pointer.add(pointer.remove(base), op.get("value")); } } Json applyMoveOperation(Json const& base, Json const& op) { + String path = op.getString("path"); + auto toPointer = JsonPath::Pointer(path); auto fromPointer = JsonPath::Pointer(op.getString("from")); - auto toPointer = JsonPath::Pointer(op.getString("path")); - Json value = fromPointer.get(base); - return toPointer.add(fromPointer.remove(base), value); + if (op.contains("search")) { + Json value = op.get("value"); + Json searchArray = fromPointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { + found = true; + break; + } + index++; + } + if (found) { + toPointer.add(toPointer.remove(base), searchArray.get(index)); + searchArray = searchArray.eraseIndex(index); + fromPointer.add(fromPointer.remove(base), searchArray); + } + return toPointer.get(base); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + Json value = fromPointer.get(base); + return toPointer.add(fromPointer.remove(base), value); + } } Json applyCopyOperation(Json const& base, Json const& op) { + String path = op.getString("path"); + auto toPointer = JsonPath::Pointer(path); auto fromPointer = JsonPath::Pointer(op.getString("from")); - auto toPointer = JsonPath::Pointer(op.getString("path")); - return toPointer.add(base, fromPointer.get(base)); + if (op.contains("search")) { + Json value = op.get("value"); + Json searchArray = fromPointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { + found = true; + break; + } + index++; + } + if (found) + toPointer.add(base, searchArray.get(index)); + return toPointer.get(base); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + Json value = fromPointer.get(base); + return toPointer.add(base, fromPointer.get(base)); + } + } + + Json applyMergeOperation(Json const& base, Json const& op) { + String path = op.getString("path"); + auto pointer = JsonPath::Pointer(op.getString("path")); + + if (op.contains("search")) { + Json value = op.get("value"); + Json searchArray = pointer.get(base); + Json searchValue = op.get("search"); + if (searchArray.type() == Json::Type::Array) { + size_t index = 0; + bool found = false; + for (auto& v : searchArray.toArray()) { + if (jsonCompare(v, searchValue)) { + found = true; + break; + } + index++; + } + if (found) + searchArray = searchArray.set(index, jsonMerge(searchArray.get(index), op.get("value"))); + return pointer.add(pointer.remove(base), searchArray); + } else { + throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path)); + } + } else { + return pointer.add(pointer.remove(base), jsonMerge(pointer.get(base), op.get("value"))); + } } } diff --git a/source/core/StarJsonPatch.hpp b/source/core/StarJsonPatch.hpp index bd331c4..44bf482 100644 --- a/source/core/StarJsonPatch.hpp +++ b/source/core/StarJsonPatch.hpp @@ -33,6 +33,9 @@ namespace JsonPatching { // Copies "from" to "path" Json applyCopyOperation(Json const& base, Json const& op); -} + + // Merges "value" at "path" + Json applyMergeOperation(Json const& base, Json const& op); + } }