mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 02:14:56 +02:00
Create Record
type (#10103)
# Description This PR creates a new `Record` type to reduce duplicate code and possibly bugs as well. (This is an edited version of #9648.) - `Record` implements `FromIterator` and `IntoIterator` and so can be iterated over or collected into. For example, this helps with conversions to and from (hash)maps. (Also, no more `cols.iter().zip(vals)`!) - `Record` has a `push(col, val)` function to help insure that the number of columns is equal to the number of values. I caught a few potential bugs thanks to this (e.g. in the `ls` command). - Finally, this PR also adds a `record!` macro that helps simplify record creation. It is used like so: ```rust record! { "key1" => some_value, "key2" => Value::string("text", span), "key3" => Value::int(optional_int.unwrap_or(0), span), "key4" => Value::bool(config.setting, span), } ``` Since macros hinder formatting, etc., the right hand side values should be relatively short and sweet like the examples above. Where possible, prefer `record!` or `.collect()` on an iterator instead of multiple `Record::push`s, since the first two automatically set the record capacity and do less work overall. # User-Facing Changes Besides the changes in `nu-protocol` the only other breaking changes are to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
This commit is contained in:
@ -2,7 +2,9 @@ use crate::math::reducers::{reducer_for, Reduce};
|
||||
use crate::math::utils::run_with_function;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Span, Type, Value};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SubCommand;
|
||||
@ -50,11 +52,10 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Find the maxima of the columns of a table",
|
||||
example: "[{a: 1 b: 3} {a: 2 b: -1}] | math max",
|
||||
result: Some(Value::Record {
|
||||
result: Some(Value::test_record(Record {
|
||||
cols: vec!["a".to_string(), "b".to_string()],
|
||||
vals: vec![Value::test_int(2), Value::test_int(3)],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
})),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -4,7 +4,9 @@ use crate::math::avg::average;
|
||||
use crate::math::utils::run_with_function;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Span, Type, Value};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SubCommand;
|
||||
@ -54,11 +56,10 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Compute the medians of the columns of a table",
|
||||
example: "[{a: 1 b: 3} {a: 2 b: -1} {a: -3 b: 5}] | math median",
|
||||
result: Some(Value::Record {
|
||||
result: Some(Value::test_record(Record {
|
||||
cols: vec!["a".to_string(), "b".to_string()],
|
||||
vals: vec![Value::test_int(1), Value::test_int(3)],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
})),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -2,7 +2,9 @@ use crate::math::reducers::{reducer_for, Reduce};
|
||||
use crate::math::utils::run_with_function;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Span, Type, Value};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SubCommand;
|
||||
@ -50,11 +52,10 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Compute the minima of the columns of a table",
|
||||
example: "[{a: 1 b: 3} {a: 2 b: -1}] | math min",
|
||||
result: Some(Value::Record {
|
||||
result: Some(Value::test_record(Record {
|
||||
cols: vec!["a".to_string(), "b".to_string()],
|
||||
vals: vec![Value::test_int(1), Value::test_int(-1)],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
})),
|
||||
},
|
||||
Example {
|
||||
description: "Find the minimum of a list of arbitrary values (Warning: Weird)",
|
||||
|
@ -1,7 +1,9 @@
|
||||
use crate::math::utils::run_with_function;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Span, Type, Value};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -88,7 +90,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Compute the mode(s) of the columns of a table",
|
||||
example: "[{a: 1 b: 3} {a: 2 b: -1} {a: 1 b: 5}] | math mode",
|
||||
result: Some(Value::Record {
|
||||
result: Some(Value::test_record(Record {
|
||||
cols: vec!["a".to_string(), "b".to_string()],
|
||||
vals: vec![
|
||||
Value::List {
|
||||
@ -100,8 +102,7 @@ impl Command for SubCommand {
|
||||
span: Span::test_data(),
|
||||
},
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
})),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use indexmap::map::IndexMap;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Span, Spanned, Value};
|
||||
use nu_protocol::{IntoPipelineData, PipelineData, Record, ShellError, Span, Value};
|
||||
|
||||
pub fn run_with_function(
|
||||
call: &Call,
|
||||
@ -26,8 +26,8 @@ fn helper_for_tables(
|
||||
let mut column_values = IndexMap::new();
|
||||
for val in values {
|
||||
match val {
|
||||
Value::Record { cols, vals, .. } => {
|
||||
for (key, value) in cols.iter().zip(vals.iter()) {
|
||||
Value::Record { val, .. } => {
|
||||
for (key, value) in val {
|
||||
column_values
|
||||
.entry(key.clone())
|
||||
.and_modify(|v: &mut Vec<Value>| v.push(value.clone()))
|
||||
@ -57,10 +57,7 @@ fn helper_for_tables(
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Value::from(Spanned {
|
||||
item: column_totals,
|
||||
span: name,
|
||||
}))
|
||||
Ok(Value::record(column_totals.into_iter().collect(), name))
|
||||
}
|
||||
|
||||
pub fn calculate(
|
||||
@ -83,15 +80,20 @@ pub fn calculate(
|
||||
),
|
||||
_ => mf(vals, span, name),
|
||||
},
|
||||
PipelineData::Value(Value::Record { vals, cols, span }, ..) => {
|
||||
let new_vals: Result<Vec<Value>, ShellError> =
|
||||
vals.into_iter().map(|val| mf(&[val], span, name)).collect();
|
||||
PipelineData::Value(Value::Record { val: record, span }, ..) => {
|
||||
let new_vals: Result<Vec<Value>, ShellError> = record
|
||||
.vals
|
||||
.into_iter()
|
||||
.map(|val| mf(&[val], span, name))
|
||||
.collect();
|
||||
match new_vals {
|
||||
Ok(vec) => Ok(Value::Record {
|
||||
cols,
|
||||
vals: vec,
|
||||
Ok(vec) => Ok(Value::record(
|
||||
Record {
|
||||
cols: record.cols,
|
||||
vals: vec,
|
||||
},
|
||||
span,
|
||||
}),
|
||||
)),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user