Skip to content

Commit 2611195

Browse files
committed
Update to pickup change in zjsonpatch 0.4.2
Closes #2
1 parent aee23e3 commit 2611195

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ The code here was ported (copied, renamed, repackaged, modified) from the [zjson
2020

2121
### How to use:
2222

23-
### Current Version : 0.4.1
23+
### Current Version : 0.4.2
2424

2525
Add following to `<dependencies/>` section of your pom.xml -
2626

2727
```xml
2828
<dependency>
2929
<groupId>com.ebay.bsonpatch</groupId>
3030
<artifactId>bsonpatch</artifactId>
31-
<version>0.4.1</version>
31+
<version>0.4.2</version>
3232
</dependency>
3333
```
3434

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.ebay.bsonpatch</groupId>
88
<artifactId>bsonpatch</artifactId>
9-
<version>0.4.1-SNAPSHOT</version>
9+
<version>0.4.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>${project.groupId}:${project.artifactId}</name>

src/main/java/com/ebay/bsonpatch/InPlaceApplyProcessor.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import java.util.List;
2424

2525
import org.bson.BsonArray;
26+
import org.bson.BsonBinary;
2627
import org.bson.BsonDocument;
28+
import org.bson.BsonJavaScriptWithScope;
2729
import org.bson.BsonValue;
2830

2931
class InPlaceApplyProcessor implements BsonPatchProcessor {
@@ -58,7 +60,8 @@ public void copy(List<String> fromPath, List<String> toPath) {
5860
BsonValue parentNode = getParentNode(fromPath, Operation.COPY);
5961
String field = fromPath.get(fromPath.size() - 1).replaceAll("\"", "");
6062
BsonValue valueNode = parentNode.isArray() ? parentNode.asArray().get(Integer.parseInt(field)) : parentNode.asDocument().get(field);
61-
add(toPath, valueNode);
63+
BsonValue valueToCopy = valueNode != null ? cloneBsonValue(valueNode) : null;
64+
add(toPath, valueToCopy);
6265
}
6366

6467
@Override
@@ -238,4 +241,26 @@ private int arrayIndex(String s, int max, boolean allowNoneExisting) {
238241
private boolean isNullOrEmpty(String string) {
239242
return string == null || string.length() == 0;
240243
}
244+
245+
private static BsonValue cloneBsonValue(BsonValue from) {
246+
BsonValue to;
247+
switch (from.getBsonType()) {
248+
case DOCUMENT:
249+
to = from.asDocument().clone();
250+
break;
251+
case ARRAY:
252+
to = from.asArray().clone();
253+
break;
254+
case BINARY:
255+
to = new BsonBinary(from.asBinary().getType(), from.asBinary().getData().clone());
256+
break;
257+
case JAVASCRIPT_WITH_SCOPE:
258+
to = new BsonJavaScriptWithScope(from.asJavaScriptWithScope().getCode(), from.asJavaScriptWithScope().getScope().clone());
259+
break;
260+
default:
261+
to = from; // assume that from is immutable
262+
}
263+
return to;
264+
}
265+
241266
}

src/test/java/com/ebay/bsonpatch/JsonDiffTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,15 @@ public void testRenderedOperationsExceptMoveAndCopy() throws Exception {
140140

141141
}
142142

143+
@Test
144+
public void testPath() throws Exception {
145+
BsonValue source = BsonDocument.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world\"}]}}");
146+
BsonArray patch = BsonArray.parse("[{\"op\":\"copy\",\"from\":\"/profiles/def/0\", \"path\":\"/profiles/def/0\"},{\"op\":\"replace\",\"path\":\"/profiles/def/0/hello\",\"value\":\"world2\"}]");
147+
148+
BsonValue target = BsonPatch.apply(patch, source);
149+
//System.out.println(target);
150+
BsonValue expected = BsonDocument.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world2\"},{\"hello\":\"world\"}]}}");
151+
Assert.assertTrue(target.equals(expected));
152+
}
153+
143154
}

0 commit comments

Comments
 (0)