mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 03:34:58 +02:00
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:
@ -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))
|
||||
|
Reference in New Issue
Block a user