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

@ -3,7 +3,7 @@ use std::collections::HashMap;
use nu_color_config::{Alignment, StyleComputer, TextStyle};
use nu_engine::column::get_columns;
use nu_protocol::{ast::PathMember, Config, Record, ShellError, Span, TableIndexMode, Value};
use nu_protocol::{Config, Record, ShellError, Span, TableIndexMode, Value};
use tabled::grid::config::Position;
use crate::{
@ -116,10 +116,11 @@ fn expanded_table_list(input: &[Value], cfg: Cfg<'_>) -> TableResult {
}
let index = row + cfg.opts.row_offset;
let text = matches!(item, Value::Record { .. })
.then(|| {
lookup_index_value(item, cfg.opts.config).unwrap_or_else(|| index.to_string())
})
let text = item
.as_record()
.ok()
.and_then(|val| val.get(INDEX_COLUMN_NAME))
.map(|value| value.into_string("", cfg.opts.config))
.unwrap_or_else(|| index.to_string());
let row = row + with_header as usize;
@ -461,20 +462,10 @@ fn get_key_style(cfg: &Cfg<'_>) -> TextStyle {
fn expanded_table_entry(item: &Value, header: &str, cfg: Cfg<'_>) -> NuText {
match item {
Value::Record { .. } => {
let val = header.to_owned();
let path = PathMember::String {
val,
span: cfg.opts.span,
optional: false,
};
let val = item.clone().follow_cell_path(&[path], false);
match val {
Ok(val) => expanded_table_entry2(&val, cfg),
Err(_) => error_sign(cfg.opts.style_computer),
}
}
Value::Record { val, .. } => match val.get(header) {
Some(val) => expanded_table_entry2(val, cfg),
None => error_sign(cfg.opts.style_computer),
},
_ => expanded_table_entry2(item, cfg),
}
}
@ -564,11 +555,6 @@ fn dive_options<'b>(cfg: &Cfg<'b>, span: Span) -> Cfg<'b> {
cfg
}
fn lookup_index_value(item: &Value, config: &Config) -> Option<String> {
item.get_data_by_key(INDEX_COLUMN_NAME)
.map(|value| value.into_string("", config))
}
fn maybe_expand_table(out: TableOutput, term_width: usize, opts: &TableOpts<'_>) -> StringResult {
let mut table_config = create_nu_table_config(opts.config, opts.style_computer, &out, false);
let total_width = out.table.total_width(&table_config);

View File

@ -1,6 +1,6 @@
use nu_color_config::TextStyle;
use nu_engine::column::get_columns;
use nu_protocol::{ast::PathMember, Config, Record, ShellError, Span, TableIndexMode, Value};
use nu_protocol::{Config, Record, ShellError, TableIndexMode, Value};
use crate::{
clean_charset, colorize_space,
@ -185,19 +185,10 @@ fn to_table_with_no_header(
fn get_string_value_with_header(item: &Value, header: &str, opts: &TableOpts) -> NuText {
match item {
Value::Record { .. } => {
let path = PathMember::String {
val: header.to_owned(),
span: Span::unknown(),
optional: false,
};
let value = item.clone().follow_cell_path(&[path], false);
match value {
Ok(value) => get_string_value(&value, opts),
Err(_) => get_empty_style(opts.style_computer),
}
}
Value::Record { val, .. } => match val.get(header) {
Some(value) => get_string_value(value, opts),
None => get_empty_style(opts.style_computer),
},
value => get_string_value(value, opts),
}
}
@ -214,8 +205,8 @@ fn get_string_value(item: &Value, opts: &TableOpts) -> NuText {
fn get_table_row_index(item: &Value, config: &Config, row: usize, offset: usize) -> String {
match item {
Value::Record { .. } => item
.get_data_by_key(INDEX_COLUMN_NAME)
Value::Record { val, .. } => val
.get(INDEX_COLUMN_NAME)
.map(|value| value.into_string("", config))
.unwrap_or_else(|| (row + offset).to_string()),
_ => (row + offset).to_string(),