diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2024-03-09 06:25:55 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-09 06:25:55 +1100 |
commit | 35dc974a8f87057c998f44687586a16147dba654 (patch) | |
tree | 7b8ce0977981bcba32c1c4088f0d26389a3cb99c /source/core | |
parent | 1cf7baa317e341a3fbca77454aa21a13440470ed (diff) | |
parent | 15a12c06a66adca4c953be8af6fc434cb6d2b156 (diff) |
Merge pull request #28 from JamesTheMaker/main
Added many new patch features
Diffstat (limited to 'source/core')
-rw-r--r-- | source/core/StarJson.cpp | 30 | ||||
-rw-r--r-- | source/core/StarJson.hpp | 16 | ||||
-rw-r--r-- | source/core/StarJsonPatch.cpp | 212 | ||||
-rw-r--r-- | source/core/StarJsonPatch.hpp | 5 |
4 files changed, 243 insertions, 20 deletions
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<Star::Json> : ostream_formatter {}; diff --git a/source/core/StarJsonPatch.cpp b/source/core/StarJsonPatch.cpp index 34c6a82..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) { @@ -40,24 +41,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& v : searchArray.toArray()) { + if (jsonCompare(v, 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; @@ -66,31 +92,179 @@ 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& v : searchArray.toArray()) { + if (jsonCompare(v, 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) { - 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& v : searchArray.toArray()) { + if (jsonCompare(v, 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) { + String path = op.getString("path"); 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"); + 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, 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), 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); + } } |