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

@@ -4,10 +4,10 @@ use lscolors::Style;
use nu_engine::env_to_string;
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, PathMember},
ast::Call,
engine::{Command, EngineState, Stack},
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
SyntaxShape, Type, Value,
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape,
Type, Value,
};
use nu_term_grid::grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions};
use nu_utils::get_ls_colors;
@@ -77,7 +77,7 @@ prints out the list properly."#
match input {
PipelineData::Value(Value::List { vals, .. }, ..) => {
// dbg!("value::list");
let data = convert_to_list(vals, config, call.head)?;
let data = convert_to_list(vals, config)?;
if let Some(items) = data {
Ok(create_grid_output(
items,
@@ -94,7 +94,7 @@ prints out the list properly."#
}
PipelineData::ListStream(stream, ..) => {
// dbg!("value::stream");
let data = convert_to_list(stream, config, call.head)?;
let data = convert_to_list(stream, config)?;
if let Some(items) = data {
Ok(create_grid_output(
items,
@@ -253,7 +253,6 @@ fn create_grid_output(
fn convert_to_list(
iter: impl IntoIterator<Item = Value>,
config: &Config,
head: Span,
) -> Result<Option<Vec<(usize, String, String)>>, ShellError> {
let mut iter = iter.into_iter().peekable();
@@ -273,21 +272,14 @@ fn convert_to_list(
row.push(item.nonerror_into_string(", ", config)?)
} else {
for header in headers.iter().skip(1) {
let result = match item {
Value::Record { .. } => item.clone().follow_cell_path(
&[PathMember::String {
val: header.into(),
span: head,
optional: false,
}],
false,
),
_ => Ok(item.clone()),
let result = match &item {
Value::Record { val, .. } => val.get(header),
item => Some(item),
};
match result {
Ok(value) => row.push(value.nonerror_into_string(", ", config)?),
Err(_) => row.push(String::new()),
Some(value) => row.push(value.nonerror_into_string(", ", config)?),
None => row.push(String::new()),
}
}
}