Skip to content

Commit c93131a

Browse files
committed
Implemented object.patch
1 parent c602765 commit c93131a

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/main/java/org/ipfs/api/IPFS.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public class IPFS {
99
public enum PinType {all, direct, indirect, recursive}
1010
public List<String> ObjectTemplates = Arrays.asList("unixfs-dir");
11+
public List<String> ObjectPatchTypes = Arrays.asList("add-link", "rm-link", "set-data", "append-data");
1112

1213
public final String host;
1314
public final int port;
@@ -205,7 +206,38 @@ public MerkleNode _new(Optional<String> template) throws IOException {
205206
return MerkleNode.fromJSON(json);
206207
}
207208

208-
// TODO patch
209+
public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, Optional<String> name, Optional<Multihash> target) throws IOException {
210+
if (!ObjectPatchTypes.contains(command))
211+
throw new IllegalStateException("Illegal Object.patch command type: "+command);
212+
String targetPath = "object/patch?arg=" + base.toBase58() + "&arg=" + command;
213+
if (name.isPresent())
214+
targetPath += "&arg=" + name.get();
215+
if (target.isPresent())
216+
targetPath += "&arg=" + target.get().toBase58();
217+
218+
switch (command) {
219+
case "add-link":
220+
if (!name.isPresent() || !target.isPresent())
221+
throw new IllegalStateException("add-link requires name and target!");
222+
return MerkleNode.fromJSON(retrieveMap(targetPath));
223+
case "rm-link":
224+
if (!name.isPresent())
225+
throw new IllegalStateException("rm-link requires name!");
226+
return MerkleNode.fromJSON(retrieveMap(targetPath));
227+
case "set-data":
228+
if (!data.isPresent())
229+
throw new IllegalStateException("set-data requires data!");
230+
targetPath += "&arg="+new String(data.get());
231+
return MerkleNode.fromJSON(retrieveMap(targetPath));
232+
case "append-data":
233+
if (!data.isPresent())
234+
throw new IllegalStateException("append-data requires data!");
235+
targetPath += "&arg="+new String(data.get());
236+
return MerkleNode.fromJSON(retrieveMap(targetPath));
237+
default:
238+
throw new IllegalStateException("Unimplemented");
239+
}
240+
}
209241
}
210242

211243
public class Name {

src/test/java/org/ipfs/api/Test.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,41 @@ public void pinTest() {
8282
}
8383
}
8484

85+
@org.junit.Test
86+
public void objectPatch() {
87+
try {
88+
MerkleNode obj = ipfs.object._new(Optional.empty());
89+
Multihash base = obj.hash;
90+
// link tests
91+
String linkName = "alink";
92+
MerkleNode addLink = ipfs.object.patch(base, "add-link", Optional.empty(), Optional.of(linkName), Optional.of(base));
93+
MerkleNode withLink = ipfs.object.get(addLink.hash);
94+
if (withLink.links.size() != 1 || !withLink.links.get(0).hash.equals(base) || !withLink.links.get(0).name.get().equals(linkName))
95+
throw new RuntimeException("Added link not correct!");
96+
MerkleNode rmLink = ipfs.object.patch(addLink.hash, "rm-link", Optional.empty(), Optional.of(linkName), Optional.empty());
97+
if (!rmLink.hash.equals(base))
98+
throw new RuntimeException("Adding not inverse of removing link!");
99+
100+
// data tests
101+
byte[] data = "somerandomdata".getBytes();
102+
MerkleNode patched = ipfs.object.patch(base, "set-data", Optional.of(data), Optional.empty(), Optional.empty());
103+
MerkleNode patchedResult = ipfs.object.get(patched.hash);
104+
if (!Arrays.equals(patchedResult.data.get(), data))
105+
throw new RuntimeException("object.patch: returned data != stored data!");
106+
107+
MerkleNode twicePatched = ipfs.object.patch(patched.hash, "append-data", Optional.of(data), Optional.empty(), Optional.empty());
108+
MerkleNode twicePatchedResult = ipfs.object.get(twicePatched.hash);
109+
byte[] twice = new byte[2*data.length];
110+
for (int i=0; i < 2; i++)
111+
System.arraycopy(data, 0, twice, i*data.length, data.length);
112+
if (!Arrays.equals(twicePatchedResult.data.get(), twice))
113+
throw new RuntimeException("object.patch: returned data after append != stored data!");
114+
115+
} catch (IOException e) {
116+
throw new RuntimeException(e);
117+
}
118+
}
119+
85120
@org.junit.Test
86121
public void refsTest() {
87122
try {

0 commit comments

Comments
 (0)