mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +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:
@ -3,7 +3,7 @@ use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::{Call, CellPath},
|
||||
engine::{Command, EngineState, Stack},
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
||||
record, Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -32,7 +32,7 @@ impl Command for Fmt {
|
||||
vec![Example {
|
||||
description: "Get a record containing multiple formats for the number 42",
|
||||
example: "42 | fmt",
|
||||
result: Some(Value::Record {
|
||||
result: Some(Value::test_record(Record {
|
||||
cols: vec![
|
||||
"binary".into(),
|
||||
"debug".into(),
|
||||
@ -53,8 +53,7 @@ impl Command for Fmt {
|
||||
Value::test_string("4.2E1"),
|
||||
Value::test_string("0x2A"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
})),
|
||||
}]
|
||||
}
|
||||
|
||||
@ -99,71 +98,35 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
}
|
||||
|
||||
fn fmt_it(num: i64, span: Span) -> Value {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
|
||||
cols.push("binary".into());
|
||||
vals.push(Value::string(format!("{num:#b}"), span));
|
||||
|
||||
cols.push("debug".into());
|
||||
vals.push(Value::string(format!("{num:#?}"), span));
|
||||
|
||||
cols.push("display".into());
|
||||
vals.push(Value::string(format!("{num}"), span));
|
||||
|
||||
cols.push("lowerexp".into());
|
||||
vals.push(Value::string(format!("{num:#e}"), span));
|
||||
|
||||
cols.push("lowerhex".into());
|
||||
vals.push(Value::string(format!("{num:#x}"), span));
|
||||
|
||||
cols.push("octal".into());
|
||||
vals.push(Value::string(format!("{num:#o}"), span));
|
||||
|
||||
// cols.push("pointer".into());
|
||||
// vals.push(Value::string(format!("{:#p}", &num), span));
|
||||
|
||||
cols.push("upperexp".into());
|
||||
vals.push(Value::string(format!("{num:#E}"), span));
|
||||
|
||||
cols.push("upperhex".into());
|
||||
vals.push(Value::string(format!("{num:#X}"), span));
|
||||
|
||||
Value::Record { cols, vals, span }
|
||||
Value::record(
|
||||
record! {
|
||||
"binary" => Value::string(format!("{num:#b}"), span),
|
||||
"debug" => Value::string(format!("{num:#?}"), span),
|
||||
"display" => Value::string(format!("{num}"), span),
|
||||
"lowerexp" => Value::string(format!("{num:#e}"), span),
|
||||
"lowerhex" => Value::string(format!("{num:#x}"), span),
|
||||
"octal" => Value::string(format!("{num:#o}"), span),
|
||||
"upperexp" => Value::string(format!("{num:#E}"), span),
|
||||
"upperhex" => Value::string(format!("{num:#X}"), span),
|
||||
},
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
fn fmt_it_64(num: f64, span: Span) -> Value {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
|
||||
cols.push("binary".into());
|
||||
vals.push(Value::string(format!("{:b}", num.to_bits()), span));
|
||||
|
||||
cols.push("debug".into());
|
||||
vals.push(Value::string(format!("{num:#?}"), span));
|
||||
|
||||
cols.push("display".into());
|
||||
vals.push(Value::string(format!("{num}"), span));
|
||||
|
||||
cols.push("lowerexp".into());
|
||||
vals.push(Value::string(format!("{num:#e}"), span));
|
||||
|
||||
cols.push("lowerhex".into());
|
||||
vals.push(Value::string(format!("{:0x}", num.to_bits()), span));
|
||||
|
||||
cols.push("octal".into());
|
||||
vals.push(Value::string(format!("{:0o}", num.to_bits()), span));
|
||||
|
||||
// cols.push("pointer".into());
|
||||
// vals.push(Value::string(format!("{:#p}", &num), span));
|
||||
|
||||
cols.push("upperexp".into());
|
||||
vals.push(Value::string(format!("{num:#E}"), span));
|
||||
|
||||
cols.push("upperhex".into());
|
||||
vals.push(Value::string(format!("{:0X}", num.to_bits()), span));
|
||||
|
||||
Value::Record { cols, vals, span }
|
||||
Value::record(
|
||||
record! {
|
||||
"binary" => Value::string(format!("{:b}", num.to_bits()), span),
|
||||
"debug" => Value::string(format!("{num:#?}"), span),
|
||||
"display" => Value::string(format!("{num}"), span),
|
||||
"lowerexp" => Value::string(format!("{num:#e}"), span),
|
||||
"lowerhex" => Value::string(format!("{:0x}", num.to_bits()), span),
|
||||
"octal" => Value::string(format!("{:0o}", num.to_bits()), span),
|
||||
"upperexp" => Value::string(format!("{num:#E}"), span),
|
||||
"upperhex" => Value::string(format!("{:0X}", num.to_bits()), span),
|
||||
},
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -56,37 +56,24 @@ fn horizontal_rotate_value(
|
||||
) -> Result<Value, ShellError> {
|
||||
match value {
|
||||
Value::Record {
|
||||
mut cols,
|
||||
mut vals,
|
||||
val: mut record,
|
||||
span,
|
||||
} => {
|
||||
let rotations = by.map(|n| n % vals.len()).unwrap_or(1);
|
||||
|
||||
let columns = if cells_only {
|
||||
cols
|
||||
} else {
|
||||
let columns = cols.as_mut_slice();
|
||||
let rotations = by.map(|n| n % record.len()).unwrap_or(1);
|
||||
|
||||
if !cells_only {
|
||||
match direction {
|
||||
HorizontalDirection::Right => columns.rotate_right(rotations),
|
||||
HorizontalDirection::Left => columns.rotate_left(rotations),
|
||||
HorizontalDirection::Right => record.cols.rotate_right(rotations),
|
||||
HorizontalDirection::Left => record.cols.rotate_left(rotations),
|
||||
}
|
||||
|
||||
columns.to_owned()
|
||||
};
|
||||
|
||||
let values = vals.as_mut_slice();
|
||||
|
||||
match direction {
|
||||
HorizontalDirection::Right => values.rotate_right(rotations),
|
||||
HorizontalDirection::Left => values.rotate_left(rotations),
|
||||
HorizontalDirection::Right => record.vals.rotate_right(rotations),
|
||||
HorizontalDirection::Left => record.vals.rotate_left(rotations),
|
||||
}
|
||||
|
||||
Ok(Value::Record {
|
||||
cols: columns,
|
||||
vals: values.to_owned(),
|
||||
span,
|
||||
})
|
||||
Ok(Value::record(record, span))
|
||||
}
|
||||
Value::List { vals, span } => {
|
||||
let values = vals
|
||||
|
@ -2,8 +2,8 @@ use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
|
||||
Type, Value,
|
||||
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
|
||||
SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::{vertical_rotate_value, VerticalDirection};
|
||||
@ -39,21 +39,18 @@ impl Command for RollDown {
|
||||
example: "[[a b]; [1 2] [3 4] [5 6]] | roll down",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: columns.clone(),
|
||||
vals: vec![Value::test_int(5), Value::test_int(6)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: columns.clone(),
|
||||
vals: vec![Value::test_int(1), Value::test_int(2)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: columns,
|
||||
vals: vec![Value::test_int(3), Value::test_int(4)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
|
@ -2,8 +2,8 @@ use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
|
||||
Type, Value,
|
||||
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
|
||||
SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::{horizontal_rotate_value, HorizontalDirection};
|
||||
@ -51,27 +51,24 @@ impl Command for RollLeft {
|
||||
Example {
|
||||
description: "Rolls columns of a record to the left",
|
||||
example: "{a:1 b:2 c:3} | roll left",
|
||||
result: Some(Value::Record {
|
||||
result: Some(Value::test_record(Record {
|
||||
cols: rotated_columns.clone(),
|
||||
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(1)],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
})),
|
||||
},
|
||||
Example {
|
||||
description: "Rolls columns of a table to the left",
|
||||
example: "[[a b c]; [1 2 3] [4 5 6]] | roll left",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: rotated_columns.clone(),
|
||||
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(1)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: rotated_columns,
|
||||
vals: vec![Value::test_int(5), Value::test_int(6), Value::test_int(4)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -81,16 +78,14 @@ impl Command for RollLeft {
|
||||
example: "[[a b c]; [1 2 3] [4 5 6]] | roll left --cells-only",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: columns.clone(),
|
||||
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(1)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: columns,
|
||||
vals: vec![Value::test_int(5), Value::test_int(6), Value::test_int(4)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
|
@ -2,8 +2,8 @@ use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
|
||||
Type, Value,
|
||||
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
|
||||
SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::{horizontal_rotate_value, HorizontalDirection};
|
||||
@ -51,27 +51,24 @@ impl Command for RollRight {
|
||||
Example {
|
||||
description: "Rolls columns of a record to the right",
|
||||
example: "{a:1 b:2 c:3} | roll right",
|
||||
result: Some(Value::Record {
|
||||
result: Some(Value::test_record(Record {
|
||||
cols: rotated_columns.clone(),
|
||||
vals: vec![Value::test_int(3), Value::test_int(1), Value::test_int(2)],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
})),
|
||||
},
|
||||
Example {
|
||||
description: "Rolls columns to the right",
|
||||
example: "[[a b c]; [1 2 3] [4 5 6]] | roll right",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: rotated_columns.clone(),
|
||||
vals: vec![Value::test_int(3), Value::test_int(1), Value::test_int(2)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: rotated_columns,
|
||||
vals: vec![Value::test_int(6), Value::test_int(4), Value::test_int(5)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -81,16 +78,14 @@ impl Command for RollRight {
|
||||
example: "[[a b c]; [1 2 3] [4 5 6]] | roll right --cells-only",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: columns.clone(),
|
||||
vals: vec![Value::test_int(3), Value::test_int(1), Value::test_int(2)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: columns,
|
||||
vals: vec![Value::test_int(6), Value::test_int(4), Value::test_int(5)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
|
@ -2,8 +2,8 @@ use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
|
||||
Type, Value,
|
||||
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
|
||||
SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::{vertical_rotate_value, VerticalDirection};
|
||||
@ -39,21 +39,18 @@ impl Command for RollUp {
|
||||
example: "[[a b]; [1 2] [3 4] [5 6]] | roll up",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: columns.clone(),
|
||||
vals: vec![Value::test_int(3), Value::test_int(4)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: columns.clone(),
|
||||
vals: vec![Value::test_int(5), Value::test_int(6)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: columns,
|
||||
vals: vec![Value::test_int(1), Value::test_int(2)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
|
@ -2,8 +2,8 @@ use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
|
||||
Type, Value,
|
||||
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
|
||||
SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -40,16 +40,14 @@ impl Command for Rotate {
|
||||
example: "{a:1, b:2} | rotate",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: vec!["column0".to_string(), "column1".to_string()],
|
||||
vals: vec![Value::test_int(1), Value::test_string("a")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["column0".to_string(), "column1".to_string()],
|
||||
vals: vec![Value::test_int(2), Value::test_string("b")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -59,7 +57,7 @@ impl Command for Rotate {
|
||||
example: "[[a b]; [1 2] [3 4] [5 6]] | rotate",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: vec![
|
||||
"column0".to_string(),
|
||||
"column1".to_string(),
|
||||
@ -72,9 +70,8 @@ impl Command for Rotate {
|
||||
Value::test_int(1),
|
||||
Value::test_string("a"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec![
|
||||
"column0".to_string(),
|
||||
"column1".to_string(),
|
||||
@ -87,8 +84,7 @@ impl Command for Rotate {
|
||||
Value::test_int(2),
|
||||
Value::test_string("b"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -98,16 +94,14 @@ impl Command for Rotate {
|
||||
example: "[[a b]; [1 2]] | rotate col_a col_b",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: vec!["col_a".to_string(), "col_b".to_string()],
|
||||
vals: vec![Value::test_int(1), Value::test_string("a")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["col_a".to_string(), "col_b".to_string()],
|
||||
vals: vec![Value::test_int(2), Value::test_string("b")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -117,16 +111,14 @@ impl Command for Rotate {
|
||||
example: "[[a b]; [1 2]] | rotate --ccw",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: vec!["column0".to_string(), "column1".to_string()],
|
||||
vals: vec![Value::test_string("b"), Value::test_int(2)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["column0".to_string(), "column1".to_string()],
|
||||
vals: vec![Value::test_string("a"), Value::test_int(1)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -136,7 +128,7 @@ impl Command for Rotate {
|
||||
example: "[[a b]; [1 2] [3 4] [5 6]] | rotate --ccw",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: vec![
|
||||
"column0".to_string(),
|
||||
"column1".to_string(),
|
||||
@ -149,9 +141,8 @@ impl Command for Rotate {
|
||||
Value::test_int(4),
|
||||
Value::test_int(6),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec![
|
||||
"column0".to_string(),
|
||||
"column1".to_string(),
|
||||
@ -164,8 +155,7 @@ impl Command for Rotate {
|
||||
Value::test_int(3),
|
||||
Value::test_int(5),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -175,16 +165,14 @@ impl Command for Rotate {
|
||||
example: "[[a b]; [1 2]] | rotate --ccw col_a col_b",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: vec!["col_a".to_string(), "col_b".to_string()],
|
||||
vals: vec![Value::test_string("b"), Value::test_int(2)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["col_a".to_string(), "col_b".to_string()],
|
||||
vals: vec![Value::test_string("a"), Value::test_int(1)],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -226,9 +214,9 @@ pub fn rotate(
|
||||
if !values.is_empty() {
|
||||
for val in values.into_iter() {
|
||||
match val {
|
||||
Value::Record { cols, vals, .. } => {
|
||||
old_column_names = cols;
|
||||
for v in vals {
|
||||
Value::Record { val: record, .. } => {
|
||||
old_column_names = record.cols;
|
||||
for v in record.vals {
|
||||
new_values.push(v)
|
||||
}
|
||||
}
|
||||
@ -286,11 +274,13 @@ pub fn rotate(
|
||||
|
||||
if not_a_record {
|
||||
return Ok(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
cols: new_column_names,
|
||||
vals: new_values,
|
||||
span: call.head,
|
||||
}],
|
||||
vals: vec![Value::record(
|
||||
Record {
|
||||
cols: new_column_names,
|
||||
vals: new_values,
|
||||
},
|
||||
call.head,
|
||||
)],
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data()
|
||||
@ -333,11 +323,13 @@ pub fn rotate(
|
||||
}
|
||||
res.to_vec()
|
||||
};
|
||||
final_values.push(Value::Record {
|
||||
cols: new_column_names.clone(),
|
||||
vals: new_vals,
|
||||
span: call.head,
|
||||
})
|
||||
final_values.push(Value::record(
|
||||
Record {
|
||||
cols: new_column_names.clone(),
|
||||
vals: new_vals,
|
||||
},
|
||||
call.head,
|
||||
))
|
||||
}
|
||||
|
||||
Ok(Value::List {
|
||||
|
@ -3,7 +3,7 @@ use nu_protocol::ast::{Block, Call};
|
||||
use nu_protocol::engine::{Closure, Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
|
||||
PipelineIterator, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
PipelineIterator, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use std::iter::FromIterator;
|
||||
@ -52,7 +52,7 @@ impl Command for UpdateCells {
|
||||
}
|
||||
}"#,
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec![
|
||||
"2021-04-16".into(),
|
||||
"2021-06-10".into(),
|
||||
@ -71,8 +71,7 @@ impl Command for UpdateCells {
|
||||
Value::test_string(""),
|
||||
Value::test_string(""),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
@ -89,7 +88,7 @@ impl Command for UpdateCells {
|
||||
}
|
||||
}"#,
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec![
|
||||
"2021-04-16".into(),
|
||||
"2021-06-10".into(),
|
||||
@ -108,8 +107,7 @@ impl Command for UpdateCells {
|
||||
Value::test_string(""),
|
||||
Value::test_string(""),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
@ -194,26 +192,26 @@ impl Iterator for UpdateCellIterator {
|
||||
}
|
||||
|
||||
match val {
|
||||
Value::Record { vals, cols, span } => Some(Value::Record {
|
||||
vals: cols
|
||||
.iter()
|
||||
.zip(vals)
|
||||
Value::Record { val, span } => Some(Value::record(
|
||||
val.into_iter()
|
||||
.map(|(col, val)| match &self.columns {
|
||||
Some(cols) if !cols.contains(col) => val,
|
||||
_ => process_cell(
|
||||
val,
|
||||
&self.engine_state,
|
||||
&mut self.stack,
|
||||
&self.block,
|
||||
self.redirect_stdout,
|
||||
self.redirect_stderr,
|
||||
span,
|
||||
Some(cols) if !cols.contains(&col) => (col, val),
|
||||
_ => (
|
||||
col,
|
||||
process_cell(
|
||||
val,
|
||||
&self.engine_state,
|
||||
&mut self.stack,
|
||||
&self.block,
|
||||
self.redirect_stdout,
|
||||
self.redirect_stderr,
|
||||
span,
|
||||
),
|
||||
),
|
||||
})
|
||||
.collect(),
|
||||
cols,
|
||||
span,
|
||||
}),
|
||||
)),
|
||||
val => Some(process_cell(
|
||||
val,
|
||||
&self.engine_state,
|
||||
|
@ -1,6 +1,8 @@
|
||||
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 FromUrl;
|
||||
@ -35,7 +37,7 @@ impl Command for FromUrl {
|
||||
vec![Example {
|
||||
example: "'bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter' | from url",
|
||||
description: "Convert url encoded string into a record",
|
||||
result: Some(Value::Record {
|
||||
result: Some(Value::test_record(Record {
|
||||
cols: vec![
|
||||
"bread".to_string(),
|
||||
"cheese".to_string(),
|
||||
@ -48,8 +50,7 @@ impl Command for FromUrl {
|
||||
Value::test_string("ham"),
|
||||
Value::test_string("butter"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
})),
|
||||
}]
|
||||
}
|
||||
}
|
||||
@ -61,21 +62,12 @@ fn from_url(input: PipelineData, head: Span) -> Result<PipelineData, ShellError>
|
||||
|
||||
match result {
|
||||
Ok(result) => {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
for (k, v) in result {
|
||||
cols.push(k);
|
||||
vals.push(Value::String { val: v, span: head })
|
||||
}
|
||||
let record = result
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k, Value::string(v, head)))
|
||||
.collect();
|
||||
|
||||
Ok(PipelineData::Value(
|
||||
Value::Record {
|
||||
cols,
|
||||
vals,
|
||||
span: head,
|
||||
},
|
||||
metadata,
|
||||
))
|
||||
Ok(PipelineData::Value(Value::record(record, head), metadata))
|
||||
}
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
"String not compatible with URL encoding".to_string(),
|
||||
|
@ -4,8 +4,8 @@ use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Config, DataSource, Example, IntoPipelineData, PipelineData, PipelineMetadata,
|
||||
ShellError, Signature, Spanned, SyntaxShape, Type, Value,
|
||||
record, Category, Config, DataSource, Example, IntoPipelineData, PipelineData,
|
||||
PipelineMetadata, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
|
||||
};
|
||||
use rust_embed::RustEmbed;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -258,62 +258,34 @@ fn to_html(
|
||||
// If asset doesn't work, make sure to return the default theme
|
||||
let html_themes = get_html_themes("228_themes.json").unwrap_or_default();
|
||||
|
||||
let cols = vec![
|
||||
"name".into(),
|
||||
"black".into(),
|
||||
"red".into(),
|
||||
"green".into(),
|
||||
"yellow".into(),
|
||||
"blue".into(),
|
||||
"purple".into(),
|
||||
"cyan".into(),
|
||||
"white".into(),
|
||||
"brightBlack".into(),
|
||||
"brightRed".into(),
|
||||
"brightGreen".into(),
|
||||
"brightYellow".into(),
|
||||
"brightBlue".into(),
|
||||
"brightPurple".into(),
|
||||
"brightCyan".into(),
|
||||
"brightWhite".into(),
|
||||
"background".into(),
|
||||
"foreground".into(),
|
||||
];
|
||||
|
||||
let result: Vec<Value> = html_themes
|
||||
.themes
|
||||
.into_iter()
|
||||
.map(|n| {
|
||||
let vals = vec![
|
||||
n.name,
|
||||
n.black,
|
||||
n.red,
|
||||
n.green,
|
||||
n.yellow,
|
||||
n.blue,
|
||||
n.purple,
|
||||
n.cyan,
|
||||
n.white,
|
||||
n.brightBlack,
|
||||
n.brightRed,
|
||||
n.brightGreen,
|
||||
n.brightYellow,
|
||||
n.brightBlue,
|
||||
n.brightPurple,
|
||||
n.brightCyan,
|
||||
n.brightWhite,
|
||||
n.background,
|
||||
n.foreground,
|
||||
]
|
||||
.into_iter()
|
||||
.map(|val| Value::String { val, span: head })
|
||||
.collect();
|
||||
|
||||
Value::Record {
|
||||
cols: cols.clone(),
|
||||
vals,
|
||||
span: head,
|
||||
}
|
||||
Value::record(
|
||||
record! {
|
||||
"name" => Value::string(n.name, head),
|
||||
"black" => Value::string(n.black, head),
|
||||
"red" => Value::string(n.red, head),
|
||||
"green" => Value::string(n.green, head),
|
||||
"yellow" => Value::string(n.yellow, head),
|
||||
"blue" => Value::string(n.blue, head),
|
||||
"purple" => Value::string(n.purple, head),
|
||||
"cyan" => Value::string(n.cyan, head),
|
||||
"white" => Value::string(n.white, head),
|
||||
"brightBlack" => Value::string(n.brightBlack, head),
|
||||
"brightRed" => Value::string(n.brightRed, head),
|
||||
"brightGreen" => Value::string(n.brightGreen, head),
|
||||
"brightYellow" => Value::string(n.brightYellow, head),
|
||||
"brightBlue" => Value::string(n.brightBlue, head),
|
||||
"brightPurple" => Value::string(n.brightPurple, head),
|
||||
"brightCyan" => Value::string(n.brightCyan, head),
|
||||
"brightWhite" => Value::string(n.brightWhite, head),
|
||||
"background" => Value::string(n.background, head),
|
||||
"foreground" => Value::string(n.foreground, head),
|
||||
},
|
||||
head,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
return Ok(Value::List {
|
||||
|
@ -2,7 +2,7 @@ use inflector::cases::camelcase::to_camel_case;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::operate;
|
||||
@ -74,11 +74,10 @@ impl Command for SubCommand {
|
||||
description: "convert a column from a table to camelCase",
|
||||
example: r#"[[lang, gems]; [nu_test, 100]] | str camel-case lang"#,
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
span: Span::test_data(),
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec!["lang".to_string(), "gems".to_string()],
|
||||
vals: vec![Value::test_string("nuTest"), Value::test_int(100)],
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
@ -2,7 +2,7 @@ use inflector::cases::kebabcase::to_kebab_case;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::operate;
|
||||
@ -74,11 +74,10 @@ impl Command for SubCommand {
|
||||
description: "convert a column from a table to kebab-case",
|
||||
example: r#"[[lang, gems]; [nuTest, 100]] | str kebab-case lang"#,
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
span: Span::test_data(),
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec!["lang".to_string(), "gems".to_string()],
|
||||
vals: vec![Value::test_string("nu-test"), Value::test_int(100)],
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
@ -2,7 +2,7 @@ use inflector::cases::pascalcase::to_pascal_case;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::operate;
|
||||
@ -74,11 +74,10 @@ impl Command for SubCommand {
|
||||
description: "convert a column from a table to PascalCase",
|
||||
example: r#"[[lang, gems]; [nu_test, 100]] | str pascal-case lang"#,
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
span: Span::test_data(),
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec!["lang".to_string(), "gems".to_string()],
|
||||
vals: vec![Value::test_string("NuTest"), Value::test_int(100)],
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
@ -2,7 +2,7 @@ use inflector::cases::screamingsnakecase::to_screaming_snake_case;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::operate;
|
||||
@ -74,11 +74,10 @@ impl Command for SubCommand {
|
||||
description: "convert a column from a table to SCREAMING_SNAKE_CASE",
|
||||
example: r#"[[lang, gems]; [nu_test, 100]] | str screaming-snake-case lang"#,
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
span: Span::test_data(),
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec!["lang".to_string(), "gems".to_string()],
|
||||
vals: vec![Value::test_string("NU_TEST"), Value::test_int(100)],
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
@ -2,7 +2,7 @@ use inflector::cases::snakecase::to_snake_case;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::operate;
|
||||
@ -73,11 +73,10 @@ impl Command for SubCommand {
|
||||
description: "convert a column from a table to snake_case",
|
||||
example: r#"[[lang, gems]; [nuTest, 100]] | str snake-case lang"#,
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
span: Span::test_data(),
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec!["lang".to_string(), "gems".to_string()],
|
||||
vals: vec![Value::test_string("nu_test"), Value::test_int(100)],
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
@ -2,7 +2,7 @@ use inflector::cases::titlecase::to_title_case;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::operate;
|
||||
@ -69,11 +69,10 @@ impl Command for SubCommand {
|
||||
description: "convert a column from a table to Title Case",
|
||||
example: r#"[[title, count]; ['nu test', 100]] | str title-case title"#,
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
span: Span::test_data(),
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec!["title".to_string(), "count".to_string()],
|
||||
vals: vec![Value::test_string("Nu Test"), Value::test_int(100)],
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
Reference in New Issue
Block a user