Skip to content

Commit

Permalink
feat: get all custom metadata keys, closes #277
Browse files Browse the repository at this point in the history
  • Loading branch information
decahedron1 committed Aug 28, 2024
1 parent d9661ed commit 9f4527c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ impl<'s> ModelMetadata<'s> {
Ok(None)
}
}

pub fn custom_keys(&self) -> Result<Vec<String>> {
let mut keys: *mut *mut c_char = std::ptr::null_mut();
let mut key_len = 0;
ortsys![unsafe ModelMetadataGetCustomMetadataMapKeys(self.metadata_ptr.as_ptr(), self.allocator.ptr.as_ptr(), &mut keys, &mut key_len) -> Error::GetModelMetadata];
if key_len != 0 && !keys.is_null() {
let res = unsafe { std::slice::from_raw_parts(keys, key_len as usize) }
.iter()
.map(|c| {
let res = char_p_to_string(*c);
unsafe { self.allocator.free(*c) };
res
})
.collect();
unsafe { self.allocator.free(keys) };
res
} else {
Ok(vec![])
}
}
}

impl<'s> Drop for ModelMetadata<'s> {
Expand Down
1 change: 1 addition & 0 deletions tests/vectorizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn vectorizer() -> ort::Result<()> {
let metadata = session.metadata()?;
assert_eq!(metadata.producer()?, "skl2onnx");
assert_eq!(metadata.description()?, "test description");
assert_eq!(metadata.custom_keys()?, ["custom_key"]);
assert_eq!(metadata.custom("custom_key")?.as_deref(), Some("custom_value"));

let array = ndarray::CowArray::from(ndarray::Array::from_shape_vec((1,), vec!["document".to_owned()]).unwrap());
Expand Down

0 comments on commit 9f4527c

Please sign in to comment.