| 56 | } |
| 57 | |
| 58 | Json applyTestOperation(Json const& base, Json const& op) { |
| 59 | String path = op.getString("path"); |
| 60 | auto pointer = JsonPath::Pointer(path); |
| 61 | auto inverseTest = op.getBool("inverse", false); |
| 62 | |
| 63 | try { |
| 64 | if (op.contains("search")) { |
| 65 | auto searchable = pointer.get(base); |
| 66 | auto searchValue = op.get("search"); |
| 67 | bool found = findJsonMatch(searchable, searchValue, pointer); |
| 68 | if (found && inverseTest) |
| 69 | throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", searchValue), false); |
| 70 | else if (!found && !inverseTest) |
| 71 | throw JsonPatchTestFail(strf("Test operation failure, could not find {}.", searchValue), false); |
| 72 | return base; |
| 73 | } else { |
| 74 | auto value = op.opt("value"); |
| 75 | auto testValue = pointer.get(base); |
| 76 | if (!value) { |
| 77 | if (inverseTest) |
| 78 | throw JsonPatchTestFail(strf("Test operation failure, expected {} to be missing.", path), false); |
| 79 | return base; |
| 80 | } |
| 81 | |
| 82 | if ((value && (testValue == *value)) ^ inverseTest) |
| 83 | return base; |
| 84 | else |
| 85 | throw JsonPatchTestFail(strf("Test operation failure, expected {} found {}.", value, testValue), false); |
| 86 | } |
| 87 | } catch (JsonPath::TraversalException& e) { |
| 88 | if (inverseTest) |
| 89 | return base; |
| 90 | throw JsonPatchTestFail(strf("Test operation failure: {}", e.what()), false); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | Json applyRemoveOperation(Json const& base, Json const& op) { |
| 95 | String path = op.getString("path"); |