-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: sometimes you have an object ID but need to know what type of object the ID points at in order to determine what to do with it but there is no way of getting this information from automerge. Solution: add Read.getObjectType which returns the type of the object ID, or `Optional.empty` if the object is not in the document.
- Loading branch information
Showing
9 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.automerge; | ||
|
||
import java.util.Optional; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class TestGetObjType { | ||
|
||
@Test | ||
public void testGetObjType() { | ||
Document doc = new Document(); | ||
ObjectId map; | ||
ObjectId list; | ||
ObjectId text; | ||
try (Transaction tx = doc.startTransaction()) { | ||
map = tx.set(ObjectId.ROOT, "map", ObjectType.MAP); | ||
list = tx.set(ObjectId.ROOT, "list", ObjectType.LIST); | ||
text = tx.set(ObjectId.ROOT, "text", ObjectType.TEXT); | ||
tx.commit(); | ||
} | ||
|
||
// make an object ID from a different document | ||
Document otherDoc = new Document(); | ||
ObjectId missingObj; | ||
try (Transaction tx = otherDoc.startTransaction()) { | ||
missingObj = tx.set(ObjectId.ROOT, "other", ObjectType.MAP); | ||
tx.commit(); | ||
} | ||
|
||
Assertions.assertEquals(Optional.of(ObjectType.MAP), doc.getObjectType(map)); | ||
Assertions.assertEquals(Optional.of(ObjectType.LIST), doc.getObjectType(list)); | ||
Assertions.assertEquals(Optional.of(ObjectType.TEXT), doc.getObjectType(text)); | ||
Assertions.assertEquals(Optional.empty(), doc.getObjectType(missingObj)); | ||
|
||
// now the same tests but in a transaction | ||
try (Transaction tx = doc.startTransaction()) { | ||
Assertions.assertEquals(Optional.of(ObjectType.MAP), tx.getObjectType(map)); | ||
Assertions.assertEquals(Optional.of(ObjectType.LIST), tx.getObjectType(list)); | ||
Assertions.assertEquals(Optional.of(ObjectType.TEXT), tx.getObjectType(text)); | ||
Assertions.assertEquals(Optional.empty(), tx.getObjectType(missingObj)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use automerge_jni_macros::jni_fn; | ||
use jni::sys::jobject; | ||
|
||
use super::SomeReadPointer; | ||
|
||
#[no_mangle] | ||
#[jni_fn] | ||
pub unsafe extern "C" fn getObjectTypeInDoc( | ||
env: jni::JNIEnv, | ||
_class: jni::objects::JClass, | ||
doc_pointer: jni::sys::jobject, | ||
obj_pointer: jni::sys::jobject, | ||
) -> jobject { | ||
SomeReadPointer::doc(doc_pointer).get_object_type(env, obj_pointer) | ||
} | ||
|
||
#[no_mangle] | ||
#[jni_fn] | ||
pub unsafe extern "C" fn getObjectTypeInTx( | ||
env: jni::JNIEnv, | ||
_class: jni::objects::JClass, | ||
tx_pointer: jni::sys::jobject, | ||
obj_pointer: jni::sys::jobject, | ||
) -> jobject { | ||
SomeReadPointer::tx(tx_pointer).get_object_type(env, obj_pointer) | ||
} |