feat: construct about value, use title for name, error handling

This commit is contained in:
rzmk 2026-05-13 18:31:45 -04:00
parent 1c280b8fd4
commit 78b30fc4ba
2 changed files with 59 additions and 9 deletions

View file

@ -1,10 +1,11 @@
use anyhow::{Result, bail};
use serde_json::json; use serde_json::json;
pub fn construct_dataset_jsonld_from_metadata( pub fn construct_dataset_jsonld_from_metadata(
dataset_metadata: serde_json::Value, dataset_metadata: serde_json::Value,
) -> serde_json::Value { ) -> Result<serde_json::Value> {
let dataset_id = dataset_metadata.get("id").unwrap().as_str().unwrap(); let dataset_id = dataset_metadata.get("id").unwrap().as_str().unwrap();
let dataset_name = dataset_metadata.get("name").unwrap().as_str().unwrap(); let dataset_title = dataset_metadata.get("title").unwrap().as_str().unwrap();
let organization_name = dataset_metadata let organization_name = dataset_metadata
.get("organization") .get("organization")
.unwrap() .unwrap()
@ -13,8 +14,45 @@ pub fn construct_dataset_jsonld_from_metadata(
// TODO: Align and include Geoconnex PIDs for reference feature categories to extract PIDs from them // TODO: Align and include Geoconnex PIDs for reference feature categories to extract PIDs from them
// Then also convert spatial_full FeatureCollection to Multipolygon if needed for gsp:hasGeometry when there are // Then also convert spatial_full FeatureCollection to Multipolygon if needed for gsp:hasGeometry when there are
// also non-reference feature polygons // also non-reference feature polygons
// if let Some(spatial_full) = dataset_metadata.get("spatial_full") {} let mut about = vec![];
let jsonld = json!({ if let Some(spatial_full) = dataset_metadata.get("spatial_full") {
let Some(spatial_full_str) = spatial_full.as_str() else {
bail!("Could not parse spatial_full as string.");
};
if !spatial_full_str.is_empty() {
let Ok(spatial_full_json) = serde_json::from_str::<serde_json::Value>(spatial_full_str)
else {
bail!(
"Error while attempting to deserialize spatial_full string to serde_json::Value."
);
};
let Some(features_value) = spatial_full_json.get("features") else {
bail!("Error while attempting to get value of features from spatial_full GeoJSON.");
};
let Some(features) = features_value.as_array() else {
bail!(
"Eerror while attempting to take features value as array from spatial_full GeoJSON."
);
};
for feature in features {
let Some(properties) = feature.get("properties") else {
bail!(
"Error while attempting to get properties from features from spatial_full GeoJSON."
);
};
if let Some(pid) = properties.get("pid") {
let Some(pid_string) = pid.as_str() else {
bail!("Error while attempting to convert PID as str from &Value.");
};
about.push(json!({
"@id": pid_string,
"@type": "Place"
}));
}
}
}
}
let mut jsonld = json!({
"@context": { "@context": {
"@vocab": "https://schema.org/", "@vocab": "https://schema.org/",
"gsp": "http://www.opengis.net/ont/geosparql#", "gsp": "http://www.opengis.net/ont/geosparql#",
@ -22,11 +60,17 @@ pub fn construct_dataset_jsonld_from_metadata(
"@type": "Dataset", "@type": "Dataset",
// TODO: Customize namespace based on CKAN instance being used // TODO: Customize namespace based on CKAN instance being used
"@id": format!("https://geoconnex.us/nmwdh/ckan-datasets/{dataset_id}"), "@id": format!("https://geoconnex.us/nmwdh/ckan-datasets/{dataset_id}"),
"name": dataset_name, "name": dataset_title,
"provider": { "provider": {
"@type": "Organization", "@type": "Organization",
"name": organization_name "name": organization_name
} },
// TODO: Customize CKAN instance URL based on CKAN instance being used
"url": format!("http://localhost:5000/dataset/{dataset_id}")
}); });
jsonld let jsonld_map = jsonld.as_object_mut().unwrap();
if about.len() > 0 {
jsonld_map.insert("about".to_string(), serde_json::to_value(about).unwrap());
}
Ok(serde_json::to_value(jsonld_map).unwrap())
} }

View file

@ -61,8 +61,14 @@ async fn main() -> Result<()> {
); );
}; };
// 2. Construct JSON-LD based on the data from /package_show // 2. Construct JSON-LD based on the data from /package_show
let jsonld = let jsonld = match construct_dataset_jsonld_from_metadata(
construct_dataset_jsonld_from_metadata(dataset_metadata.to_owned()); dataset_metadata.to_owned(),
) {
Ok(j) => j,
Err(e) => bail!(
"Error while attempting to construct JSON-LD from dataset's metadata: {e}"
),
};
// 3. Validate the JSON-LD against the dataset JSON schema // 3. Validate the JSON-LD against the dataset JSON schema
if jsonschema::validate(&get_dataset_schema(), &jsonld).is_ok() { if jsonschema::validate(&get_dataset_schema(), &jsonld).is_ok() {
// 4. Print the JSON-LD on a new line to stdout // 4. Print the JSON-LD on a new line to stdout