1
0

Implement nested cross-type dot notation

This commit is contained in:
Jeff 2023-08-23 22:03:26 -04:00
parent fde899bf3e
commit f307b436f5

View File

@ -54,39 +54,35 @@ impl VariableMap {
} }
pub fn get_value(&self, identifier: &str) -> Result<Option<Value>> { pub fn get_value(&self, identifier: &str) -> Result<Option<Value>> {
let split = identifier.split_once('.'); let split = identifier.rsplit_once('.');
let (found_value, next_identifier) = if let Some((identifier, next_identifier)) = split {
if let Some((identifier, next_identifier)) = split { if identifier.contains('.') {
if let Some(value) = self.variables.get(identifier) { (self.get_value(identifier)?, next_identifier)
if let Value::Map(map) = value {
map.get_value(next_identifier)
} else if let Value::List(list) = value {
let index = if let Ok(index) = next_identifier.parse::<usize>() {
index
} else {
return Err(Error::ExpectedInt {
actual: Value::String(next_identifier.to_string()),
});
};
let value = list.get(index);
Ok(value.cloned())
} else {
Err(Error::ExpectedMap {
actual: value.clone(),
})
}
} else { } else {
Ok(None) (self.variables.get(identifier).cloned(), next_identifier)
} }
} else { } else {
let value = self.variables.get(identifier); return Ok(self.variables.get(identifier).cloned());
};
if let Some(value) = value { if let Some(value) = found_value {
Ok(Some(value.clone())) if let Value::List(list) = value {
let index = if let Ok(index) = next_identifier.parse::<usize>() {
index
} else {
return Err(Error::expected_int(Value::String(
next_identifier.to_string(),
)));
};
Ok(list.get(index).cloned())
} else if let Value::Map(map) = value {
map.get_value(next_identifier)
} else { } else {
Ok(None) Ok(Some(value))
} }
} else {
Ok(None)
} }
} }