forked from extern/nushell
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:
@ -1,6 +1,6 @@
|
||||
use csv::{Writer, WriterBuilder};
|
||||
use nu_cmd_base::formats::to::delimited::merge_descriptors;
|
||||
use nu_protocol::{Config, IntoPipelineData, PipelineData, ShellError, Span, Value};
|
||||
use nu_protocol::{Config, IntoPipelineData, PipelineData, Record, ShellError, Span, Value};
|
||||
use std::collections::VecDeque;
|
||||
use std::error::Error;
|
||||
|
||||
@ -11,9 +11,7 @@ fn from_value_to_delimited_string(
|
||||
head: Span,
|
||||
) -> Result<String, ShellError> {
|
||||
match value {
|
||||
Value::Record { cols, vals, span } => {
|
||||
record_to_delimited(cols, vals, *span, separator, config, head)
|
||||
}
|
||||
Value::Record { val, span } => record_to_delimited(val, *span, separator, config, head),
|
||||
Value::List { vals, span } => table_to_delimited(vals, *span, separator, config, head),
|
||||
// Propagate errors by explicitly matching them before the final case.
|
||||
Value::Error { error } => Err(*error.clone()),
|
||||
@ -22,8 +20,7 @@ fn from_value_to_delimited_string(
|
||||
}
|
||||
|
||||
fn record_to_delimited(
|
||||
cols: &[String],
|
||||
vals: &[Value],
|
||||
record: &Record,
|
||||
span: Span,
|
||||
separator: char,
|
||||
config: &Config,
|
||||
@ -35,7 +32,7 @@ fn record_to_delimited(
|
||||
let mut fields: VecDeque<String> = VecDeque::new();
|
||||
let mut values: VecDeque<String> = VecDeque::new();
|
||||
|
||||
for (k, v) in cols.iter().zip(vals.iter()) {
|
||||
for (k, v) in record {
|
||||
fields.push_back(k.clone());
|
||||
|
||||
values.push_back(to_string_tagged_value(v, config, head, span)?);
|
||||
|
@ -134,9 +134,9 @@ pub fn value_to_json_value(v: &Value) -> Result<nu_json::Value, ShellError> {
|
||||
Value::Binary { val, .. } => {
|
||||
nu_json::Value::Array(val.iter().map(|x| nu_json::Value::U64(*x as u64)).collect())
|
||||
}
|
||||
Value::Record { cols, vals, .. } => {
|
||||
Value::Record { val, .. } => {
|
||||
let mut m = nu_json::Map::new();
|
||||
for (k, v) in cols.iter().zip(vals) {
|
||||
for (k, v) in val {
|
||||
m.insert(k.clone(), value_to_json_value(v)?);
|
||||
}
|
||||
nu_json::Value::Object(m)
|
||||
|
@ -105,10 +105,7 @@ fn to_md(
|
||||
}
|
||||
|
||||
fn fragment(input: Value, pretty: bool, config: &Config) -> String {
|
||||
let headers = match input {
|
||||
Value::Record { ref cols, .. } => cols.to_owned(),
|
||||
_ => vec![],
|
||||
};
|
||||
let headers = input.columns();
|
||||
let mut out = String::new();
|
||||
|
||||
if headers.len() == 1 {
|
||||
@ -207,9 +204,12 @@ pub fn group_by(values: PipelineData, head: Span, config: &Config) -> (PipelineD
|
||||
let mut lists = IndexMap::new();
|
||||
let mut single_list = false;
|
||||
for val in values {
|
||||
if let Value::Record { ref cols, .. } = val {
|
||||
if let Value::Record {
|
||||
val: ref record, ..
|
||||
} = val
|
||||
{
|
||||
lists
|
||||
.entry(cols.concat())
|
||||
.entry(record.cols.concat())
|
||||
.and_modify(|v: &mut Vec<Value>| v.push(val.clone()))
|
||||
.or_insert_with(|| vec![val.clone()]);
|
||||
} else {
|
||||
@ -329,7 +329,7 @@ fn get_padded_string(text: String, desired_length: usize, padding_character: cha
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use nu_protocol::{Config, IntoPipelineData, Span, Value};
|
||||
use nu_protocol::{Config, IntoPipelineData, Record, Span, Value};
|
||||
|
||||
fn one(string: &str) -> String {
|
||||
string
|
||||
@ -351,44 +351,40 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn render_h1() {
|
||||
let value = Value::Record {
|
||||
let value = Value::test_record(Record {
|
||||
cols: vec!["H1".to_string()],
|
||||
vals: vec![Value::test_string("Ecuador")],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
});
|
||||
|
||||
assert_eq!(fragment(value, false, &Config::default()), "# Ecuador\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_h2() {
|
||||
let value = Value::Record {
|
||||
let value = Value::test_record(Record {
|
||||
cols: vec!["H2".to_string()],
|
||||
vals: vec![Value::test_string("Ecuador")],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
});
|
||||
|
||||
assert_eq!(fragment(value, false, &Config::default()), "## Ecuador\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_h3() {
|
||||
let value = Value::Record {
|
||||
let value = Value::test_record(Record {
|
||||
cols: vec!["H3".to_string()],
|
||||
vals: vec![Value::test_string("Ecuador")],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
});
|
||||
|
||||
assert_eq!(fragment(value, false, &Config::default()), "### Ecuador\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_blockquote() {
|
||||
let value = Value::Record {
|
||||
let value = Value::test_record(Record {
|
||||
cols: vec!["BLOCKQUOTE".to_string()],
|
||||
vals: vec![Value::test_string("Ecuador")],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
});
|
||||
|
||||
assert_eq!(fragment(value, false, &Config::default()), "> Ecuador\n");
|
||||
}
|
||||
@ -397,21 +393,18 @@ mod tests {
|
||||
fn render_table() {
|
||||
let value = Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
Value::test_record(Record {
|
||||
cols: vec!["country".to_string()],
|
||||
vals: vec![Value::test_string("Ecuador")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["country".to_string()],
|
||||
vals: vec![Value::test_string("New Zealand")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
}),
|
||||
Value::test_record(Record {
|
||||
cols: vec!["country".to_string()],
|
||||
vals: vec![Value::test_string("USA")],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
|
@ -209,8 +209,8 @@ pub fn value_to_string(
|
||||
for val in vals {
|
||||
let mut row = vec![];
|
||||
|
||||
if let Value::Record { vals, .. } = val {
|
||||
for val in vals {
|
||||
if let Value::Record { val, .. } = val {
|
||||
for val in &val.vals {
|
||||
row.push(value_to_string_without_quotes(
|
||||
val,
|
||||
span,
|
||||
@ -259,9 +259,9 @@ pub fn value_to_string(
|
||||
},
|
||||
value_to_string(&val.to, span, depth + 1, indent)?
|
||||
)),
|
||||
Value::Record { cols, vals, .. } => {
|
||||
Value::Record { val, .. } => {
|
||||
let mut collection = vec![];
|
||||
for (col, val) in cols.iter().zip(vals) {
|
||||
for (col, val) in val {
|
||||
collection.push(if needs_quotes(col) {
|
||||
format!(
|
||||
"{idt_po}\"{}\": {}",
|
||||
|
@ -132,14 +132,13 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
|
||||
}
|
||||
Value::String { val, .. } => val,
|
||||
Value::List { vals: val, .. } => val
|
||||
.iter()
|
||||
.map(|x| local_into_string(x.clone(), ", ", config))
|
||||
.into_iter()
|
||||
.map(|x| local_into_string(x, ", ", config))
|
||||
.collect::<Vec<_>>()
|
||||
.join(separator),
|
||||
Value::Record { cols, vals, .. } => cols
|
||||
.iter()
|
||||
.zip(vals.iter())
|
||||
.map(|(x, y)| format!("{}: {}", x, local_into_string(y.clone(), ", ", config)))
|
||||
Value::Record { val, .. } => val
|
||||
.into_iter()
|
||||
.map(|(x, y)| format!("{}: {}", x, local_into_string(y, ", ", config)))
|
||||
.collect::<Vec<_>>()
|
||||
.join(separator),
|
||||
Value::LazyRecord { val, .. } => match val.collect() {
|
||||
|
@ -54,9 +54,9 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
|
||||
Value::Range { .. } => toml::Value::String("<Range>".to_string()),
|
||||
Value::Float { val, .. } => toml::Value::Float(*val),
|
||||
Value::String { val, .. } => toml::Value::String(val.clone()),
|
||||
Value::Record { cols, vals, .. } => {
|
||||
Value::Record { val, .. } => {
|
||||
let mut m = toml::map::Map::new();
|
||||
for (k, v) in cols.iter().zip(vals.iter()) {
|
||||
for (k, v) in val {
|
||||
m.insert(k.clone(), helper(engine_state, v)?);
|
||||
}
|
||||
toml::Value::Table(m)
|
||||
@ -172,7 +172,6 @@ fn to_toml(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use nu_protocol::Spanned;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
@ -201,10 +200,7 @@ mod tests {
|
||||
);
|
||||
let tv = value_to_toml_value(
|
||||
&engine_state,
|
||||
&Value::from(Spanned {
|
||||
item: m,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
&Value::record(m.into_iter().collect(), Span::test_data()),
|
||||
Span::test_data(),
|
||||
)
|
||||
.expect("Expected Ok from valid TOML dictionary");
|
||||
|
@ -4,8 +4,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, Spanned,
|
||||
SyntaxShape, Type, Value,
|
||||
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
|
||||
Spanned, SyntaxShape, Type, Value,
|
||||
};
|
||||
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
|
||||
use std::io::Cursor;
|
||||
@ -208,9 +208,9 @@ fn to_tag_like<W: Write>(
|
||||
// Allow tag to have no attributes or content for short hand input
|
||||
// alternatives like {tag: a attributes: {} content: []}, {tag: a attribbutes: null
|
||||
// content: null}, {tag: a}. See to_xml_entry for more
|
||||
let (attr_cols, attr_values) = match attrs {
|
||||
Value::Record { cols, vals, .. } => (cols, vals),
|
||||
Value::Nothing { .. } => (Vec::new(), Vec::new()),
|
||||
let attrs = match attrs {
|
||||
Value::Record { val, .. } => val,
|
||||
Value::Nothing { .. } => Record::new(),
|
||||
_ => {
|
||||
return Err(ShellError::CantConvert {
|
||||
to_type: "XML".into(),
|
||||
@ -234,15 +234,7 @@ fn to_tag_like<W: Write>(
|
||||
}
|
||||
};
|
||||
|
||||
to_tag(
|
||||
entry_span,
|
||||
tag,
|
||||
tag_span,
|
||||
attr_cols,
|
||||
attr_values,
|
||||
content,
|
||||
writer,
|
||||
)
|
||||
to_tag(entry_span, tag, tag_span, attrs, content, writer)
|
||||
}
|
||||
}
|
||||
|
||||
@ -305,8 +297,7 @@ fn to_tag<W: Write>(
|
||||
entry_span: Span,
|
||||
tag: String,
|
||||
tag_span: Span,
|
||||
attr_cols: Vec<String>,
|
||||
attr_vals: Vec<Value>,
|
||||
attrs: Record,
|
||||
children: Vec<Value>,
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
) -> Result<(), ShellError> {
|
||||
@ -322,7 +313,7 @@ fn to_tag<W: Write>(
|
||||
});
|
||||
}
|
||||
|
||||
let attributes = parse_attributes(attr_cols, attr_vals)?;
|
||||
let attributes = parse_attributes(attrs)?;
|
||||
let mut open_tag_event = BytesStart::new(tag.clone());
|
||||
add_attributes(&mut open_tag_event, &attributes);
|
||||
|
||||
@ -350,12 +341,9 @@ fn to_tag<W: Write>(
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_attributes(
|
||||
cols: Vec<String>,
|
||||
vals: Vec<Value>,
|
||||
) -> Result<IndexMap<String, String>, ShellError> {
|
||||
fn parse_attributes(attrs: Record) -> Result<IndexMap<String, String>, ShellError> {
|
||||
let mut h = IndexMap::new();
|
||||
for (k, v) in cols.into_iter().zip(vals) {
|
||||
for (k, v) in attrs {
|
||||
if let Value::String { val, .. } = v {
|
||||
h.insert(k, val);
|
||||
} else {
|
||||
|
@ -53,9 +53,9 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
||||
Value::Range { .. } => serde_yaml::Value::Null,
|
||||
Value::Float { val, .. } => serde_yaml::Value::Number(serde_yaml::Number::from(*val)),
|
||||
Value::String { val, .. } => serde_yaml::Value::String(val.clone()),
|
||||
Value::Record { cols, vals, .. } => {
|
||||
Value::Record { val, .. } => {
|
||||
let mut m = serde_yaml::Mapping::new();
|
||||
for (k, v) in cols.iter().zip(vals.iter()) {
|
||||
for (k, v) in val {
|
||||
m.insert(
|
||||
serde_yaml::Value::String(k.clone()),
|
||||
value_to_yaml_value(v)?,
|
||||
|
Reference in New Issue
Block a user