use fhir_types::codec::Value;
use crate::tree::Occurrence;
use crate::tree::element::Move;
use crate::tree::element::Resolved;
#[must_use]
pub fn locate(document: &Value, resolved: &Resolved, occurrence: &Occurrence) -> String {
let mut path = String::from(resolved.resource());
let mut indices = occurrence.indices().iter().copied();
let mut cursor = (document.get("resourceType").and_then(Value::as_str)
== Some(resolved.resource()))
.then_some(document);
for step in resolved.moves() {
match *step {
Move::Member(ref field) => {
path.push('.');
path.push_str(element_name(field.path()));
let held = cursor.and_then(|value| value.get(field.key()));
cursor = if field.repeats() {
index_into(&mut path, held, indices.next())
} else {
held
};
}
Move::Choice { ref field, schema } => {
path.push('.');
path.push_str(element_name(field.path()));
let held = cursor.and_then(|value| {
schema
.choices()
.find_map(|form| value.get(&format!("{}{}", field.key(), form.suffix)))
});
cursor = if field.repeats() {
index_into(&mut path, held, indices.next())
} else {
held
};
}
Move::Extension { ref field, ref url } => {
path.push('.');
path.push_str(element_name(field.path()));
let matched = indices.next().and_then(|position| {
cursor
.and_then(|value| value.get(field.key()))
.and_then(Value::as_array)?
.iter()
.enumerate()
.filter(|(_, entry)| {
entry.get("url").and_then(Value::as_str) == Some(url.as_str())
})
.nth(position)
});
cursor = matched.map(|(index, entry)| {
push_index(&mut path, index);
entry
});
}
Move::Resolve { .. } => break,
Move::Ordinal(_) | Move::Index(_) | Move::Predicate(_) => {}
}
}
path
}
fn index_into<'tree>(
path: &mut String,
held: Option<&'tree Value>,
index: Option<usize>,
) -> Option<&'tree Value> {
let index = index?;
push_index(path, index);
held.and_then(Value::as_array)
.and_then(|items| items.get(index))
}
fn push_index(path: &mut String, index: usize) {
path.push('[');
path.push_str(&index.to_string());
path.push(']');
}
fn element_name(definition: &str) -> &str {
let last = definition.rsplit('.').next().unwrap_or(definition);
last.strip_suffix("[x]").unwrap_or(last)
}
#[cfg(test)]
mod tests {
use core::str::FromStr;
use fhir_types::codec::Value;
use super::element_name;
use super::locate;
use crate::tree::Occurrence;
use crate::tree::element::resolve;
use crate::tree::path::FhirPath;
fn located(resource: &str, expression: &str, document: &Value, indices: &[usize]) -> String {
let path = FhirPath::from_str(expression).expect("the expression parses");
let resolved = resolve(&fhir_types::r4::schema::SCHEMAS, resource, &path)
.expect("the expression resolves");
locate(document, &resolved, &Occurrence::new(indices.to_vec()))
}
fn document(json: &str) -> Value {
serde_json::from_str(json).expect("the fixture is JSON")
}
#[test]
fn every_repeating_element_takes_its_index() {
assert_eq!(
located(
"Condition",
"$resource.code.coding.code",
&Value::Null,
&[2]
),
"Condition.code.coding[2].code"
);
}
#[test]
fn an_element_with_no_index_is_left_to_the_default_child_accessor() {
assert_eq!(
located("Condition", "$resource.code.coding.code", &Value::Null, &[]),
"Condition.code.coding.code"
);
}
#[test]
fn a_choice_is_named_without_its_type_suffix() {
assert_eq!(
located(
"Observation",
"$resource.value.ofType(Quantity).value",
&Value::Null,
&[]
),
"Observation.value.value"
);
assert_eq!(
located("Condition", "$resource.onset", &Value::Null, &[]),
"Condition.onset"
);
}
#[test]
fn an_extension_step_takes_the_index_its_url_matched_in_the_document() {
let patient = document(
r#"{"resourceType":"Patient","extension":[
{"url":"http://example.org/a","valueString":"x"},
{"url":"http://example.org/b","valueString":"y"},
{"url":"http://example.org/b","valueString":"z"}]}"#,
);
assert_eq!(
located(
"Patient",
"$resource.extension('http://example.org/b').value",
&patient,
&[1]
),
"Patient.extension[2].value"
);
assert_eq!(
located(
"Patient",
"$resource.extension('http://example.org/b').value",
&Value::Null,
&[1]
),
"Patient.extension.value",
"an entry the document does not hold is left unindexed"
);
}
#[test]
fn a_primitive_extension_is_reached_through_its_element() {
assert_eq!(
located(
"Patient",
"$resource.birthDate.extension('http://example.org/a')",
&Value::Null,
&[]
),
"Patient.birthDate.extension"
);
}
#[test]
fn a_resolved_reference_is_located_at_the_element_holding_it() {
assert_eq!(
located(
"Condition",
"$resource.subject.resolve().name.family",
&Value::Null,
&[]
),
"Condition.subject"
);
}
#[test]
fn an_element_name_drops_the_choice_marker_only() {
assert_eq!(element_name("Observation.value[x]"), "value");
assert_eq!(element_name("Patient.birthDate"), "birthDate");
assert_eq!(element_name("Patient"), "Patient");
}
}