Skip to content

Commit

Permalink
RO-Crate should now handle null values gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
mburridge96 committed Nov 6, 2024
1 parent a5cf4f8 commit 8babe6e
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ro-crate-rs"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
repository = "https://github.com/intbio-ncl/ro-crate-rs"
authors = ["Matt Burridge <[email protected]>"]
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rocraters"
version = "0.3.0"
version = "0.4.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
6 changes: 4 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,10 @@ fn create_default_crate(mut rocrate: RoCrate) -> RoCrate {
let root_data_entity = root::RootDataEntity {
id: "./".to_string(),
type_: DataType::Term("Dataset".to_string()),
date_published: Option::Some(Utc::now().to_rfc3339()),
license: Option::Some(License::Description(String::from("Private"))),
date_published: Utc::now().to_rfc3339(),
name: "Default Crate Name".to_string(),
description: "Default crate description".to_string(),
license: License::Description(String::from("Private")),
dynamic_entity: None,
};

Expand Down
2 changes: 1 addition & 1 deletion python/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rocraters-python"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["Matt Burridge <[email protected]>"]
description = "Lightweight Python library for RO-Crate manipulation implemented in Rust"
Expand Down
10 changes: 8 additions & 2 deletions python/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,15 @@ pub fn convert_dynamic_entity_to_pyobject(py: Python, value: &DynamicEntity) ->
DynamicEntity::NestedDynamicEntity(boxed_entity) => {
convert_dynamic_entity_to_pyobject(py, boxed_entity)
}
DynamicEntity::Fallback(value) => {
DynamicEntity::Fallback(value_option) => {
// Convert serde_json::Value to PyObject
convert_serde_json_value_to_pyobject(py, value)
if let Some(value) = value_option {
// Convert serde_json::Value to PyObject when there's a value
convert_serde_json_value_to_pyobject(py, value)
} else {
// Handle the case where Fallback contains None (i.e., represents null)
convert_serde_json_value_to_pyobject(py, &serde_json::Value::Null)
}
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/ro_crate/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub enum DynamicEntity {
EntityObject(HashMap<String, DynamicEntity>),
EntityVecObject(Vec<HashMap<String, DynamicEntity>>),
NestedDynamicEntity(Box<DynamicEntity>),
Fallback(serde_json::Value),
Fallback(Option<serde_json::Value>),
}

/// Tests
Expand Down Expand Up @@ -138,10 +138,11 @@ mod tests {
#[test]
fn test_dynamic_entity_fallback() {
let json_value = json!({"unexpected": "data"});
let entity = DynamicEntity::Fallback(json_value.clone());
let entity = DynamicEntity::Fallback(Some(json_value.clone()));

match entity {
DynamicEntity::Fallback(value) => assert_eq!(value, json_value),
_ => panic!("Fallback variant expected"),
DynamicEntity::Fallback(Some(value)) => assert_eq!(value, json_value),
_ => panic!("Fallback variant with expected value was not found"),
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/ro_crate/modify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ pub trait DynamicEntityManipulation: Serialize {
for key in fallback_keys_to_modify {
if let Some(DynamicEntity::Fallback(fallback_value)) = dynamic_entity.get_mut(&key)
{
remove_matching_value_from_json(fallback_value, target_id);
if let Some(fallback_value) = fallback_value.as_mut() {
remove_matching_value_from_json(fallback_value, target_id);
}
}
}

Expand Down

0 comments on commit 8babe6e

Please sign in to comment.