Use Record::get instead of Value functions (#10925)

# Description
Where appropriate, this PR replaces instances of
`Value::get_data_by_key` and `Value::follow_cell_path` with
`Record::get`. This avoids some unnecessary clones and simplifies the
code in some places.
This commit is contained in:
Ian Manske
2023-11-08 20:47:37 +00:00
committed by GitHub
parent 435abadd8a
commit 59ea28cf06
17 changed files with 230 additions and 289 deletions

View File

@ -628,21 +628,17 @@ impl Value {
pub fn get_data_by_key(&self, name: &str) -> Option<Value> {
let span = self.span();
match self {
Value::Record { val, .. } => val
.iter()
.find(|(col, _)| col == &name)
.map(|(_, val)| val.clone()),
Value::Record { val, .. } => val.get(name).cloned(),
Value::List { vals, .. } => {
let mut out = vec![];
for item in vals {
match item {
Value::Record { .. } => match item.get_data_by_key(name) {
Some(v) => out.push(v),
None => out.push(Value::nothing(span)),
},
_ => out.push(Value::nothing(span)),
}
}
let out = vals
.iter()
.map(|item| {
item.as_record()
.ok()
.and_then(|val| val.get(name).cloned())
.unwrap_or(Value::nothing(span))
})
.collect::<Vec<_>>();
if !out.is_empty() {
Some(Value::list(out, span))