Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarJsonPatch.cpp
diff options
context:
space:
mode:
authorJamesTheMaker <jamesthemaker2005@gmail.com>2024-03-07 12:31:48 -0500
committerJamesTheMaker <jamesthemaker2005@gmail.com>2024-03-07 12:31:48 -0500
commit9b4ca69e0e75e08c4b0587dad931654c9ac9ee2e (patch)
treec52e1479b5813c57cb0816bb659ae70cfefdc4ea /source/core/StarJsonPatch.cpp
parentc808d207c91e577d12907ea9723d500849e0d61f (diff)
Added the `search` operand to the `find`, `replace`, and `remove` operations
Diffstat (limited to 'source/core/StarJsonPatch.cpp')
-rw-r--r--source/core/StarJsonPatch.cpp99
1 files 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) {