Added the search
operand to the find
, replace
, and remove
operations
This commit is contained in:
parent
c808d207c9
commit
9b4ca69e0e
@ -40,12 +40,36 @@ 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 {
|
||||
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)
|
||||
@ -58,6 +82,7 @@ namespace JsonPatching {
|
||||
}
|
||||
|
||||
throw JsonPatchTestFail(strf("Test operation failure, expected {} found {}.", value, testValue));
|
||||
}
|
||||
} catch (JsonPath::TraversalException& e) {
|
||||
if (inverseTest)
|
||||
return base;
|
||||
@ -93,13 +118,59 @@ namespace JsonPatching {
|
||||
}
|
||||
|
||||
Json applyAddOperation(Json const& base, Json const& op) {
|
||||
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) {
|
||||
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) {
|
||||
auto fromPointer = JsonPath::Pointer(op.getString("from"));
|
||||
|
Loading…
Reference in New Issue
Block a user