Skip to content

Commit

Permalink
Update to pickup change in zjsonpatch 0.4.2
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
dandoug committed Feb 6, 2018
1 parent aee23e3 commit 2611195
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ The code here was ported (copied, renamed, repackaged, modified) from the [zjson

### How to use:

### Current Version : 0.4.1
### Current Version : 0.4.2

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

```xml
<dependency>
<groupId>com.ebay.bsonpatch</groupId>
<artifactId>bsonpatch</artifactId>
<version>0.4.1</version>
<version>0.4.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.ebay.bsonpatch</groupId>
<artifactId>bsonpatch</artifactId>
<version>0.4.1-SNAPSHOT</version>
<version>0.4.2</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/com/ebay/bsonpatch/InPlaceApplyProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.List;

import org.bson.BsonArray;
import org.bson.BsonBinary;
import org.bson.BsonDocument;
import org.bson.BsonJavaScriptWithScope;
import org.bson.BsonValue;

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

@Override
Expand Down Expand Up @@ -238,4 +241,26 @@ private int arrayIndex(String s, int max, boolean allowNoneExisting) {
private boolean isNullOrEmpty(String string) {
return string == null || string.length() == 0;
}

private static BsonValue cloneBsonValue(BsonValue from) {
BsonValue to;
switch (from.getBsonType()) {
case DOCUMENT:
to = from.asDocument().clone();
break;
case ARRAY:
to = from.asArray().clone();
break;
case BINARY:
to = new BsonBinary(from.asBinary().getType(), from.asBinary().getData().clone());
break;
case JAVASCRIPT_WITH_SCOPE:
to = new BsonJavaScriptWithScope(from.asJavaScriptWithScope().getCode(), from.asJavaScriptWithScope().getScope().clone());
break;
default:
to = from; // assume that from is immutable
}
return to;
}

}
11 changes: 11 additions & 0 deletions src/test/java/com/ebay/bsonpatch/JsonDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,15 @@ public void testRenderedOperationsExceptMoveAndCopy() throws Exception {

}

@Test
public void testPath() throws Exception {
BsonValue source = BsonDocument.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world\"}]}}");
BsonArray patch = BsonArray.parse("[{\"op\":\"copy\",\"from\":\"/profiles/def/0\", \"path\":\"/profiles/def/0\"},{\"op\":\"replace\",\"path\":\"/profiles/def/0/hello\",\"value\":\"world2\"}]");

BsonValue target = BsonPatch.apply(patch, source);
//System.out.println(target);
BsonValue expected = BsonDocument.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world2\"},{\"hello\":\"world\"}]}}");
Assert.assertTrue(target.equals(expected));
}

}

0 comments on commit 2611195

Please sign in to comment.