Skip to content

Commit

Permalink
feat!: Add passthrough param to the compress_list and expand_list fun…
Browse files Browse the repository at this point in the history
…ctions (default to true for python and JS bindings)
  • Loading branch information
vemonet committed Aug 22, 2024
1 parent df2a833 commit b7fae77
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 21 deletions.
22 changes: 18 additions & 4 deletions js/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,19 @@ impl ConverterJs {
// TODO: Use Vec<String> instead of JsValue possible?
/// Expand a list of CURIEs to URIs
#[wasm_bindgen(js_name = expandList)]
pub fn expand_list(&self, curies: JsValue) -> Result<JsValue, JsValue> {
pub fn expand_list(
&self,
curies: JsValue,
passthrough: Option<bool>,
) -> Result<JsValue, JsValue> {
let curies_vec: Vec<String> = serde_wasm_bindgen::from_value(curies)
.map_err(|e| JsValue::from_str(&format!("Error converting CURIEs list: {}", e)))?;
let js_array = self
.converter
.expand_list(curies_vec.iter().map(String::as_str).collect())
.expand_list(
curies_vec.iter().map(String::as_str).collect(),
passthrough.unwrap_or(true),
)
.into_iter()
.map(JsValue::from)
.collect::<Array>();
Expand All @@ -170,12 +177,19 @@ impl ConverterJs {

/// Compress a list of URIs to CURIEs
#[wasm_bindgen(js_name = compressList)]
pub fn compress_list(&self, curies: JsValue) -> Result<JsValue, JsValue> {
pub fn compress_list(
&self,
curies: JsValue,
passthrough: Option<bool>,
) -> Result<JsValue, JsValue> {
let curies_vec: Vec<String> = serde_wasm_bindgen::from_value(curies)
.map_err(|e| JsValue::from_str(&format!("Error converting URIs list: {}", e)))?;
let js_array = self
.converter
.compress_list(curies_vec.iter().map(String::as_str).collect())
.compress_list(
curies_vec.iter().map(String::as_str).collect(),
passthrough.unwrap_or(true),
)
.into_iter()
.map(JsValue::from)
.collect::<Array>();
Expand Down
8 changes: 4 additions & 4 deletions js/tests/curies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ describe('Tests for the curies npm package', () => {
expect(converter.expandList(["OBO:1234", "DOID:1234", "Wrong:1"])).toEqual([
"http://purl.obolibrary.org/obo/1234",
"http://purl.obolibrary.org/obo/DOID_1234",
undefined
"Wrong:1"
]);
expect(converter.compress("http://purl.obolibrary.org/obo/1234")).toBe("OBO:1234");
expect(converter.compressList([
"http://purl.obolibrary.org/obo/1234",
"http://purl.obolibrary.org/obo/DOID_1234",
"http://identifiers.org/DOID:1234"
])).toEqual(["OBO:1234", "DOID:1234", undefined]);
])).toEqual(["OBO:1234", "DOID:1234", "http://identifiers.org/DOID:1234"]);
expect(converter.getPrefixes().length).toBe(2)
expect(converter.getUriPrefixes().length).toBe(2)

Expand All @@ -43,13 +43,13 @@ describe('Tests for the curies npm package', () => {
expect(converter.expandList(["OBO:1234", "DOID:1234", "Wrong:1"])).toEqual([
"http://purl.obolibrary.org/obo/1234",
"http://purl.obolibrary.org/obo/DOID_1234",
undefined
"Wrong:1",
]);
expect(converter.compressList([
"http://purl.obolibrary.org/obo/1234",
"http://purl.obolibrary.org/obo/DOID_1234",
"http://identifiers.org/DOID:1234"
])).toEqual(["OBO:1234", "DOID:1234", undefined]);
])).toEqual(["OBO:1234", "DOID:1234", "http://identifiers.org/DOID:1234"]);
});

test('from JSON-LD', async () => {
Expand Down
23 changes: 19 additions & 4 deletions lib/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ impl Converter {
/// assert_eq!(curie, "DOID:1234");
/// ```
pub async fn from_prefix_map<T: PrefixMapSource>(prefix_map: T) -> Result<Self, CuriesError> {
// TODO: better error handling. We should show all the errors at once instead of stopping at the first one
// cf. original python implementation
let prefix_map: HashMap<String, Value> = prefix_map.fetch().await?;
let mut converter = Converter::default();
for (prefix, uri_prefix) in prefix_map {
Expand Down Expand Up @@ -598,22 +600,35 @@ impl Converter {
}

/// Compresses a list of URIs to CURIEs
pub fn compress_list(&self, uris: Vec<&str>) -> Vec<Option<String>> {
pub fn compress_list(&self, uris: Vec<&str>, passthrough: bool) -> Vec<Option<String>> {
// TODO: fixed
uris.into_iter()
.map(|uri| match self.compress(uri) {
Ok(curie) => Some(curie),
Err(_) => None,
Err(_) => {
if passthrough {
Some(uri.to_string()) // Return the full URI if passthrough is true
} else {
None
}
}
})
.collect()
}

/// Expands a list of CURIESs to URIs
pub fn expand_list(&self, curies: Vec<&str>) -> Vec<Option<String>> {
pub fn expand_list(&self, curies: Vec<&str>, passthrough: bool) -> Vec<Option<String>> {
curies
.into_iter()
.map(|curie| match self.expand(curie) {
Ok(uri) => Some(uri),
Err(_) => None,
Err(_) => {
if passthrough {
Some(curie.to_string()) // Return the full URI if passthrough is true
} else {
None
}
}
})
.collect()
}
Expand Down
28 changes: 25 additions & 3 deletions lib/tests/curies_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,41 @@ fn new_empty_converter() -> Result<(), Box<dyn std::error::Error>> {
);

assert_eq!(
converter
.compress_list(["http://wrong/1234", "https://identifiers.org/DOID/1234"].to_vec()),
converter.compress_list(
["http://wrong/1234", "https://identifiers.org/DOID/1234"].to_vec(),
false
),
[None, Some("doid:1234".to_string())].to_vec()
);
assert_eq!(
converter.expand_list(["doid:1234", "wrong:1234"].to_vec()),
converter.expand_list(["doid:1234", "wrong:1234"].to_vec(), false),
[
Some("http://purl.obolibrary.org/obo/DOID_1234".to_string()),
None
]
.to_vec()
);

assert_eq!(
converter.compress_list(
["http://wrong/1234", "https://identifiers.org/DOID/1234"].to_vec(),
true
),
[
Some("http://wrong/1234".to_string()),
Some("doid:1234".to_string())
]
.to_vec()
);
assert_eq!(
converter.expand_list(["doid:1234", "wrong:1234"].to_vec(), true),
[
Some("http://purl.obolibrary.org/obo/DOID_1234".to_string()),
Some("wrong:1234".to_string()),
]
.to_vec()
);

// Test wrong calls
assert!(converter
.add_prefix("doid", "http://purl.obolibrary.org/obo/DOID_")
Expand Down
16 changes: 10 additions & 6 deletions python/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,20 @@ impl ConverterPy {

/// Expand a list of CURIEs
#[pyo3(text_signature = "(curies)")]
fn expand_list(&self, curies: Vec<String>) -> Vec<Option<String>> {
self.converter
.expand_list(curies.iter().map(|s| s.as_str()).collect())
fn expand_list(&self, curies: Vec<String>, passthrough: Option<bool>) -> Vec<Option<String>> {
self.converter.expand_list(
curies.iter().map(|s| s.as_str()).collect(),
passthrough.unwrap_or(true),
)
}

/// Compress a list of URIs
#[pyo3(text_signature = "(uris)")]
fn compress_list(&self, uris: Vec<String>) -> Vec<Option<String>> {
self.converter
.compress_list(uris.iter().map(|s| s.as_str()).collect())
fn compress_list(&self, uris: Vec<String>, passthrough: Option<bool>) -> Vec<Option<String>> {
self.converter.compress_list(
uris.iter().map(|s| s.as_str()).collect(),
passthrough.unwrap_or(true),
)
}

/// Standardize prefix
Expand Down

0 comments on commit b7fae77

Please sign in to comment.