Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Add method to get the ValueType of a TypedValue as a keyword to Android API #820

Open
wants to merge 2 commits 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
17 changes: 16 additions & 1 deletion ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,6 @@ pub unsafe extern "C" fn typed_value_into_long(typed_value: *mut Binding) -> c_l
pub unsafe extern "C" fn typed_value_into_entid(typed_value: *mut Binding) -> Entid {
assert_not_null!(typed_value);
let typed_value = Box::from_raw(typed_value);
println!("typed value as entid {:?}", typed_value);
unwrap_conversion(typed_value.into_entid(), ValueType::Ref)
}

Expand Down Expand Up @@ -1659,6 +1658,22 @@ pub unsafe extern "C" fn typed_value_value_type(typed_value: *mut Binding) -> Va
typed_value.value_type().unwrap_or_else(|| panic!("Binding is not Scalar and has no ValueType"))
}

/// Returns the [ValueType](mentat::ValueType) of this [Binding](mentat::Binding).
#[no_mangle]
pub unsafe extern "C" fn typed_value_value_type_kw(typed_value: *mut Binding) -> *mut c_char {
let typed_value = &*typed_value;
let value = typed_value.value_type().unwrap_or_else(|| panic!("Binding is not Scalar and has no ValueType")).into_edn_value().to_string();
string_to_c_char(value.clone())
}

/// Returns the size of a `row` as usize
#[no_mangle]
pub unsafe extern "C" fn typed_value_list_size(values: *mut Vec<Binding>) -> c_int {
assert_not_null!(values);
let values = &*values;
values.len () as c_int
}

/// Returns the value at the provided `index` as a `Vec<ValueType>`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no space between function and arguments, so: .len().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting is done. I am happy to try and implement your suggestion but I don't understand. Please could you walk me through it? What should all the return types be? Should the java class size method return a String?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, reading this again, I am guessing that you mean rust to write to some memory owned by the java class (which just happens to be a static string)
Are there any good examples or bits of docs that would help me?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, my current idea would be to do this by passing a direct allocated static final byteBuffer from java. To do this it seems easiest if I have access to JNIEnv, is there a reason why you don't seem to use this style?
https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-21-rust-on-android.html

Would need to use synchronise(this) around the call to rust within the constructor.

/// If there is no value present at the `index`, a null pointer is returned.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ class EntityBuilder extends PointerType {}
double typed_value_into_double(TypedValue value);
long typed_value_into_timestamp(TypedValue value);
int typed_value_value_type(TypedValue value);
Pointer typed_value_value_type_kw(TypedValue value);

int typed_value_list_size(TypedValueList rows);
TypedValueList row_at_index(RelResult rows, int index);
RelResultIter typed_value_result_set_into_iter(RelResult rows);
TypedValueList typed_value_result_set_iter_next(RelResultIter iter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
*/
public class TupleResult extends RustObject<JNA.TypedValueList> {

private int size;

public TupleResult(JNA.TypedValueList pointer) {
super(pointer);
this.size = JNA.INSTANCE.typed_value_list_size(super.validPointer());
}

/**
Expand Down Expand Up @@ -145,6 +148,14 @@ public UUID asUUID(Integer index) {
JNA.INSTANCE.value_at_index_into_uuid(this.validPointer(), index));
}

/**
* Return the size of the tuple.
* @return The number of items in this tuple
*/
public int size () {
return this.size;
}

@Override
protected void destroyPointer(JNA.TypedValueList p) {
JNA.INSTANCE.typed_value_list_destroy(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@
public class TypedValue extends RustObject<JNA.TypedValue> {

private Object value;
private String type = null;

public TypedValue(JNA.TypedValue pointer) {
super(pointer);
}
this.type = getAndConsumeMentatString(JNA.INSTANCE.typed_value_value_type_kw(super.validPointer()));
if ( this.type == null ) {
super.close ();
throw new IllegalArgumentException ("Failed to obtain Keyword for the valueType of this TypedValue");
}
}

/**
* This value as a {@link Long}. This function will panic if the `ValueType` of this
Expand Down Expand Up @@ -131,6 +137,14 @@ public UUID asUUID() {
return (UUID)this.value;
}

/**
* The :db/valueType of this value as a keyword {@link String}.
* @return the value type of this {@link TypedValue} as a Keyword
*/
public String valueTypeKeyword() {
return this.type;
}

@Override
protected void destroyPointer(JNA.TypedValue p) {
JNA.INSTANCE.typed_value_destroy(p);
Expand Down