More compact search patch parsing
This commit is contained in:
parent
7bc9eaa432
commit
7b556b33f8
@ -1035,13 +1035,13 @@ Json jsonMerge(Json const& base, Json const& merger) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonCompare(Json const& base, Json const& compare) {
|
bool jsonPartialMatch(Json const& base, Json const& compare) {
|
||||||
if (base == compare) {
|
if (base == compare) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
if (base.type() == Json::Type::Object && compare.type() == Json::Type::Object) {
|
if (base.type() == Json::Type::Object && compare.type() == Json::Type::Object) {
|
||||||
for (auto const& c : compare.toObject()) {
|
for (auto const& c : compare.toObject()) {
|
||||||
if (!base.contains(c.first) || !jsonCompare(base.get(c.first), c.second))
|
if (!base.contains(c.first) || !jsonPartialMatch(base.get(c.first), c.second))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -1050,7 +1050,7 @@ bool jsonCompare(Json const& base, Json const& compare) {
|
|||||||
for (auto const& c : compare.toArray()) {
|
for (auto const& c : compare.toArray()) {
|
||||||
bool similar = false;
|
bool similar = false;
|
||||||
for (auto const& b : base.toArray()) {
|
for (auto const& b : base.toArray()) {
|
||||||
if (jsonCompare(c, b)) {
|
if (jsonPartialMatch(c, b)) {
|
||||||
similar = true;
|
similar = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -355,21 +355,13 @@ Json jsonMergeQueryDef(String const& key, Json def, Json const& first, T const&.
|
|||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compares the two given json values and returns a boolean, by the following
|
// Compares two JSON values to see if the second is a subset of the first.
|
||||||
// rules (applied in order): If both values are identical, return true. If both
|
// For objects, each key in the second object must exist in the first
|
||||||
// values are not equal, check if they are objects. If they are objects,
|
// object and the values are recursively compared the same way. For arrays,
|
||||||
// iterate through every pair in the comparing object and check if the key is
|
// each element in the second array must successfully compare with some
|
||||||
// in the base object. If the key is in the base object, then jsonCompare is
|
// element of the first array, regardless of order or duplication.
|
||||||
// called recursively on both values. If the base object does not contain the
|
// For all other types, the values must be equal.
|
||||||
// key, or the recursion fails, return false. Otherwise, return true. If they
|
bool jsonPartialMatch(Json const& base, Json const& compare);
|
||||||
// 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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,22 @@ Json jsonPatch(Json const& base, JsonArray const& patch) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Returns 0 if not found, index + 1 if found.
|
||||||
|
size_t findJsonMatch(Json const& searchable, Json const& value, JsonPath::Pointer& pointer) {
|
||||||
|
if (searchable.isType(Json::Type::Array)) {
|
||||||
|
auto array = searchable.toArray();
|
||||||
|
for (size_t i = 0; i != array.size(); ++i) {
|
||||||
|
if (jsonPartialMatch(array[i], value))
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw JsonPatchException(strf("Search operation failure, value at '{}' is not an array.", pointer.path()));
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
namespace JsonPatching {
|
namespace JsonPatching {
|
||||||
|
|
||||||
static const StringMap<std::function<Json(Json, Json)>> functionMap = StringMap<std::function<Json(Json, Json)>>{
|
static const StringMap<std::function<Json(Json, Json)>> functionMap = StringMap<std::function<Json(Json, Json)>>{
|
||||||
@ -40,49 +56,33 @@ namespace JsonPatching {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Json applyTestOperation(Json const& base, Json const& op) {
|
Json applyTestOperation(Json const& base, Json const& op) {
|
||||||
auto path = op.getString("path");
|
String path = op.getString("path");
|
||||||
auto inverseTest = op.getBool("inverse", false);
|
|
||||||
|
|
||||||
auto pointer = JsonPath::Pointer(path);
|
auto pointer = JsonPath::Pointer(path);
|
||||||
|
auto inverseTest = op.getBool("inverse", false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (op.contains("search")) {
|
if (op.contains("search")) {
|
||||||
Json searchArray = pointer.get(base);
|
auto searchable = pointer.get(base);
|
||||||
Json searchValue = op.get("search");
|
auto searchValue = op.get("search");
|
||||||
if (searchArray.type() == Json::Type::Array) {
|
bool found = findJsonMatch(searchable, searchValue, pointer);
|
||||||
bool found = false;
|
if (found && inverseTest)
|
||||||
for(auto& v : searchArray.toArray()) {
|
throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", searchValue));
|
||||||
if (jsonCompare(v, searchValue)) {
|
else if (!found && !inverseTest)
|
||||||
found = true;
|
throw JsonPatchTestFail(strf("Test operation failure, could not find {}.", searchValue));
|
||||||
break;
|
return base;
|
||||||
}
|
|
||||||
}
|
|
||||||
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 {
|
} else {
|
||||||
auto value = op.opt("value");
|
auto value = op.opt("value");
|
||||||
auto testValue = pointer.get(base);
|
auto testValue = pointer.get(base);
|
||||||
if (!value) {
|
if (!value) {
|
||||||
if (inverseTest)
|
if (inverseTest)
|
||||||
throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", op.getString("path")));
|
throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", path));
|
||||||
return base;
|
return base;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((value && (testValue == *value)) ^ inverseTest) {
|
if ((value && (testValue == *value)) ^ inverseTest)
|
||||||
return base;
|
return base;
|
||||||
}
|
else
|
||||||
|
throw JsonPatchTestFail(strf("Test operation failure, expected {} found {}.", value, testValue));
|
||||||
throw JsonPatchTestFail(strf("Test operation failure, expected {} found {}.", value, testValue));
|
|
||||||
}
|
}
|
||||||
} catch (JsonPath::TraversalException& e) {
|
} catch (JsonPath::TraversalException& e) {
|
||||||
if (inverseTest)
|
if (inverseTest)
|
||||||
@ -92,84 +92,52 @@ namespace JsonPatching {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Json applyRemoveOperation(Json const& base, Json const& op) {
|
Json applyRemoveOperation(Json const& base, Json const& op) {
|
||||||
|
String path = op.getString("path");
|
||||||
|
auto pointer = JsonPath::Pointer(path);
|
||||||
|
|
||||||
if (op.contains("search")) {
|
if (op.contains("search")) {
|
||||||
String path = op.getString("path");
|
auto searchable = pointer.get(base);
|
||||||
auto pointer = JsonPath::Pointer(path);
|
auto searchValue = op.get("search");
|
||||||
Json searchArray = pointer.get(base);
|
if (size_t index = findJsonMatch(searchable, searchValue, pointer))
|
||||||
Json searchValue = op.get("search");
|
return pointer.add(pointer.remove(base), searchable.eraseIndex(index - 1));
|
||||||
if (searchArray.type() == Json::Type::Array) {
|
else
|
||||||
size_t index = 0;
|
return base;
|
||||||
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 {
|
} else {
|
||||||
return JsonPath::Pointer(op.getString("path")).remove(base);
|
return pointer.remove(base);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Json applyAddOperation(Json const& base, Json const& op) {
|
Json applyAddOperation(Json const& base, Json const& op) {
|
||||||
|
String path = op.getString("path");
|
||||||
|
auto value = op.get("value");
|
||||||
|
auto pointer = JsonPath::Pointer(path);
|
||||||
|
|
||||||
if (op.contains("search")) {
|
if (op.contains("search")) {
|
||||||
Json value = op.get("value");
|
auto searchable = pointer.get(base);
|
||||||
String path = op.getString("path");
|
auto searchValue = op.get("search");
|
||||||
auto pointer = JsonPath::Pointer(path);
|
if (size_t index = findJsonMatch(searchable, searchValue, pointer))
|
||||||
Json searchArray = pointer.get(base);
|
return pointer.add(pointer.remove(base), searchable.insert(index - 1, value));
|
||||||
Json searchValue = op.get("search");
|
else
|
||||||
if (searchArray.type() == Json::Type::Array) {
|
return base;
|
||||||
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 {
|
} else {
|
||||||
return JsonPath::Pointer(op.getString("path")).add(base, op.get("value"));
|
return pointer.add(base, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Json applyReplaceOperation(Json const& base, Json const& op) {
|
Json applyReplaceOperation(Json const& base, Json const& op) {
|
||||||
String path = op.getString("path");
|
String path = op.getString("path");
|
||||||
|
auto value = op.get("value");
|
||||||
auto pointer = JsonPath::Pointer(op.getString("path"));
|
auto pointer = JsonPath::Pointer(op.getString("path"));
|
||||||
|
|
||||||
if (op.contains("search")) {
|
if (op.contains("search")) {
|
||||||
Json value = op.get("value");
|
auto searchable = pointer.get(base);
|
||||||
Json searchArray = pointer.get(base);
|
auto searchValue = op.get("search");
|
||||||
Json searchValue = op.get("search");
|
if (size_t index = findJsonMatch(searchable, searchValue, pointer))
|
||||||
if (searchArray.type() == Json::Type::Array) {
|
return pointer.add(pointer.remove(base), searchable.set(index - 1, value));
|
||||||
size_t index = 0;
|
else
|
||||||
bool found = false;
|
return base;
|
||||||
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 {
|
} else {
|
||||||
return pointer.add(pointer.remove(base), op.get("value"));
|
return pointer.add(pointer.remove(base), value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,28 +147,14 @@ namespace JsonPatching {
|
|||||||
auto fromPointer = JsonPath::Pointer(op.getString("from"));
|
auto fromPointer = JsonPath::Pointer(op.getString("from"));
|
||||||
|
|
||||||
if (op.contains("search")) {
|
if (op.contains("search")) {
|
||||||
Json value = op.get("value");
|
auto searchable = fromPointer.get(base);
|
||||||
Json searchArray = fromPointer.get(base);
|
auto searchValue = op.get("search");
|
||||||
Json searchValue = op.get("search");
|
if (size_t index = findJsonMatch(searchable, searchValue, fromPointer)) {
|
||||||
if (searchArray.type() == Json::Type::Array) {
|
auto result = toPointer.add(base, searchable.get(index - 1));
|
||||||
size_t index = 0;
|
return fromPointer.add(result, searchable.eraseIndex(index - 1));
|
||||||
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
|
||||||
|
return base;
|
||||||
} else {
|
} else {
|
||||||
Json value = fromPointer.get(base);
|
Json value = fromPointer.get(base);
|
||||||
return toPointer.add(fromPointer.remove(base), value);
|
return toPointer.add(fromPointer.remove(base), value);
|
||||||
@ -213,55 +167,29 @@ namespace JsonPatching {
|
|||||||
auto fromPointer = JsonPath::Pointer(op.getString("from"));
|
auto fromPointer = JsonPath::Pointer(op.getString("from"));
|
||||||
|
|
||||||
if (op.contains("search")) {
|
if (op.contains("search")) {
|
||||||
Json value = op.get("value");
|
auto searchable = fromPointer.get(base);
|
||||||
Json searchArray = fromPointer.get(base);
|
auto searchValue = op.get("search");
|
||||||
Json searchValue = op.get("search");
|
if (size_t index = findJsonMatch(searchable, searchValue, fromPointer))
|
||||||
if (searchArray.type() == Json::Type::Array) {
|
return toPointer.add(base, searchable.get(index - 1));
|
||||||
size_t index = 0;
|
else
|
||||||
bool found = false;
|
return base;
|
||||||
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 {
|
} else {
|
||||||
Json value = fromPointer.get(base);
|
Json value = fromPointer.get(base);
|
||||||
return toPointer.add(base, fromPointer.get(base));
|
return toPointer.add(base, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Json applyMergeOperation(Json const& base, Json const& op) {
|
Json applyMergeOperation(Json const& base, Json const& op) {
|
||||||
String path = op.getString("path");
|
String path = op.getString("path");
|
||||||
auto pointer = JsonPath::Pointer(op.getString("path"));
|
auto pointer = JsonPath::Pointer(path);
|
||||||
|
|
||||||
if (op.contains("search")) {
|
if (op.contains("search")) {
|
||||||
Json value = op.get("value");
|
auto searchable = pointer.get(base);
|
||||||
Json searchArray = pointer.get(base);
|
auto searchValue = op.get("search");
|
||||||
Json searchValue = op.get("search");
|
if (size_t index = findJsonMatch(searchable, searchValue, pointer))
|
||||||
if (searchArray.type() == Json::Type::Array) {
|
return pointer.add(pointer.remove(base), searchable.set(index - 1, jsonMerge(searchable.get(index - 1), op.get("value"))));
|
||||||
size_t index = 0;
|
else
|
||||||
bool found = false;
|
return base;
|
||||||
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 {
|
} else {
|
||||||
return pointer.add(pointer.remove(base), jsonMerge(pointer.get(base), op.get("value")));
|
return pointer.add(pointer.remove(base), jsonMerge(pointer.get(base), op.get("value")));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user