Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add value to move and copy operations #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/com/flipkart/zjsonpatch/Diff.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class Diff {
this.value = null;
this.srcValue = null;
}

Diff(Operation operation, JsonPointer fromPath, JsonPointer toPath, JsonNode value) {
this.operation = operation;
this.path = fromPath;
this.toPath = toPath;
this.value = value;
this.srcValue = null;
}

Diff(Operation operation, JsonPointer path, JsonNode srcValue, JsonNode value) {
this.operation = operation;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/flipkart/zjsonpatch/JsonDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void introduceCopyOperation(JsonNode source, JsonNode target) {
diffs.add(i, new Diff(Operation.TEST, matchingValuePath, diff.getValue()));
i++;
}
diffs.set(i, new Diff(Operation.COPY, matchingValuePath, diff.getPath()));
diffs.set(i, new Diff(Operation.COPY, matchingValuePath, diff.getPath(), diff.getValue()));
}
}
}
Expand Down Expand Up @@ -209,12 +209,12 @@ private void introduceMoveOperation() {
if (Operation.REMOVE == diff1.getOperation() &&
Operation.ADD == diff2.getOperation()) {
JsonPointer relativePath = computeRelativePath(diff2.getPath(), i + 1, j - 1, diffs);
moveDiff = new Diff(Operation.MOVE, diff1.getPath(), relativePath);
moveDiff = new Diff(Operation.MOVE, diff1.getPath(), relativePath, diff2.getValue());

} else if (Operation.ADD == diff1.getOperation() &&
Operation.REMOVE == diff2.getOperation()) {
JsonPointer relativePath = computeRelativePath(diff2.getPath(), i, j - 1, diffs); // diff1's add should also be considered
moveDiff = new Diff(Operation.MOVE, relativePath, diff1.getPath());
moveDiff = new Diff(Operation.MOVE, relativePath, diff1.getPath(), diff1.getValue());
}
if (moveDiff != null) {
diffs.remove(j);
Expand Down Expand Up @@ -322,6 +322,7 @@ private static ObjectNode getJsonNode(JsonNodeFactory FACTORY, Diff diff, EnumSe
switch (diff.getOperation()) {
case MOVE:
case COPY:
jsonNode.set(Constants.VALUE, diff.getValue());
jsonNode.put(Constants.FROM, diff.getPath().toString()); // required {from} only in case of Move Operation
jsonNode.put(Constants.PATH, diff.getToPath().toString()); // destination Path
break;
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/com/flipkart/zjsonpatch/MoveOperationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public static Collection<PatchTestCase> data() throws IOException {
public void testMoveValueGeneratedHasNoValue() throws IOException {
JsonNode jsonNode1 = MAPPER.readTree("{ \"foo\": { \"bar\": \"baz\", \"waldo\": \"fred\" }, \"qux\": { \"corge\": \"grault\" } }");
JsonNode jsonNode2 = MAPPER.readTree("{ \"foo\": { \"bar\": \"baz\" }, \"qux\": { \"corge\": \"grault\", \"thud\": \"fred\" } }");
JsonNode patch = MAPPER.readTree("[{\"op\":\"move\",\"from\":\"/foo/waldo\",\"path\":\"/qux/thud\"}]");
JsonNode patch = MAPPER.readTree(
"[{\"op\":\"move\",\"value\":\"fred\",\"from\":\"/foo/waldo\",\"path\":\"/qux/thud\"}]"
);

JsonNode diff = JsonDiff.asJson(jsonNode1, jsonNode2);

Expand All @@ -54,7 +56,9 @@ public void testMoveValueGeneratedHasNoValue() throws IOException {
public void testMoveArrayGeneratedHasNoValue() throws IOException {
JsonNode jsonNode1 = MAPPER.readTree("{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }");
JsonNode jsonNode2 = MAPPER.readTree("{ \"foo\": [ \"all\", \"cows\", \"eat\", \"grass\" ] }");
JsonNode patch = MAPPER.readTree("[{\"op\":\"move\",\"from\":\"/foo/1\",\"path\":\"/foo/3\"}]");
JsonNode patch = MAPPER.readTree(
"[{\"op\":\"move\",\"value\":\"grass\",\"from\":\"/foo/1\",\"path\":\"/foo/3\"}]"
);

JsonNode diff = JsonDiff.asJson(jsonNode1, jsonNode2);

Expand Down