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:
Ian Manske
2023-08-24 19:50:29 +00:00
committed by GitHub
parent 030e749fe7
commit 8da27a1a09
195 changed files with 4211 additions and 6245 deletions

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
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,
};
#[derive(Clone)]
@ -82,14 +82,13 @@ impl Command for FromCsv {
description: "Convert comma-separated data to a table",
example: "\"ColA,ColB\n1,2\" | from csv",
result: Some(Value::List {
vals: vec![Value::Record {
vals: vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::test_int(1),
Value::test_int(2),
],
span: Span::test_data(),
}],
})],
span: Span::test_data(),
})
},

View File

@ -1,5 +1,5 @@
use csv::{ReaderBuilder, Trim};
use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Span, Value};
use nu_protocol::{IntoPipelineData, PipelineData, Record, ShellError, Span, Value};
fn from_delimited_string_to_value(
DelimitedReaderConfig {
@ -56,11 +56,13 @@ fn from_delimited_string_to_value(
});
}
}
rows.push(Value::Record {
cols: headers.clone(),
vals: output_row,
rows.push(Value::record(
Record {
cols: headers.clone(),
vals: output_row,
},
span,
});
));
}
Ok(Value::List { vals: rows, span })

View File

@ -1,8 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError,
Signature, Span, Type, Value,
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Record,
ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
@ -29,16 +29,15 @@ impl Command for FromJson {
Example {
example: r#"'{ "a": 1 }' | from json"#,
description: "Converts json formatted string to table",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec!["a".to_string()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
}),
})),
},
Example {
example: r#"'{ "a": 1, "b": [1, 2] }' | from json"#,
description: "Converts json formatted string to table",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![
Value::test_int(1),
@ -47,8 +46,7 @@ impl Command for FromJson {
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
})),
},
]
}
@ -107,17 +105,12 @@ fn convert_nujson_to_value(value: &nu_json::Value, span: Span) -> Value {
nu_json::Value::F64(f) => Value::Float { val: *f, span },
nu_json::Value::I64(i) => Value::Int { val: *i, span },
nu_json::Value::Null => Value::Nothing { span },
nu_json::Value::Object(k) => {
let mut cols = vec![];
let mut vals = vec![];
for item in k {
cols.push(item.0.clone());
vals.push(convert_nujson_to_value(item.1, span));
}
Value::Record { cols, vals, span }
}
nu_json::Value::Object(k) => Value::record(
k.iter()
.map(|(k, v)| (k.clone(), convert_nujson_to_value(v, span)))
.collect(),
span,
),
nu_json::Value::U64(u) => {
if *u > i64::MAX as u64 {
Value::Error {

View File

@ -1,8 +1,8 @@
use nu_protocol::ast::{Call, Expr, Expression, PipelineElement};
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Range, ShellError, Signature, Span, Type,
Unit, Value,
Category, Example, IntoPipelineData, PipelineData, Range, Record, ShellError, Signature, Span,
Type, Unit, Value,
};
#[derive(Clone)]
pub struct FromNuon;
@ -27,16 +27,15 @@ impl Command for FromNuon {
Example {
example: "'{ a:1 }' | from nuon",
description: "Converts nuon formatted string to table",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec!["a".to_string()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
}),
})),
},
Example {
example: "'{ a:1, b: [1, 2] }' | from nuon",
description: "Converts nuon formatted string to table",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![
Value::test_int(1),
@ -45,8 +44,7 @@ impl Command for FromNuon {
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
})),
},
]
}
@ -323,8 +321,7 @@ fn convert_to_value(
})
}
Expr::Record(key_vals) => {
let mut cols = vec![];
let mut vals = vec![];
let mut record = Record::new();
for (key, val) in key_vals {
let key_str = match key.expr {
@ -341,11 +338,10 @@ fn convert_to_value(
let value = convert_to_value(val, span, original_text)?;
cols.push(key_str);
vals.push(value);
record.push(key_str, value);
}
Ok(Value::Record { cols, vals, span })
Ok(Value::record(record, span))
}
Expr::RowCondition(..) => Err(ShellError::OutsideSpannedLabeledError(
original_text.to_string(),
@ -409,11 +405,13 @@ fn convert_to_value(
));
}
output.push(Value::Record {
cols: cols.clone(),
vals,
output.push(Value::record(
Record {
cols: cols.clone(),
vals,
},
span,
});
));
}
Ok(Value::List { vals: output, span })

View File

@ -133,41 +133,29 @@ fn from_ods(
sheet_names.retain(|e| sel_sheets.contains(e));
}
for sheet_name in &sheet_names {
for sheet_name in sheet_names {
let mut sheet_output = vec![];
if let Some(Ok(current_sheet)) = ods.worksheet_range(sheet_name) {
if let Some(Ok(current_sheet)) = ods.worksheet_range(&sheet_name) {
for row in current_sheet.rows() {
let mut row_output = IndexMap::new();
for (i, cell) in row.iter().enumerate() {
let value = match cell {
DataType::Empty => Value::nothing(head),
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::bool(*b, head),
_ => Value::nothing(head),
};
let record = row
.iter()
.enumerate()
.map(|(i, cell)| {
let value = match cell {
DataType::Empty => Value::nothing(head),
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::bool(*b, head),
_ => Value::nothing(head),
};
row_output.insert(format!("column{i}"), value);
}
(format!("column{i}"), value)
})
.collect();
let (cols, vals) =
row_output
.into_iter()
.fold((vec![], vec![]), |mut acc, (k, v)| {
acc.0.push(k);
acc.1.push(v);
acc
});
let record = Value::Record {
cols,
vals,
span: head,
};
sheet_output.push(record);
sheet_output.push(Value::record(record, head));
}
dict.insert(
@ -187,19 +175,10 @@ fn from_ods(
}
}
let (cols, vals) = dict.into_iter().fold((vec![], vec![]), |mut acc, (k, v)| {
acc.0.push(k.clone());
acc.1.push(v);
acc
});
let record = Value::Record {
cols,
vals,
span: head,
};
Ok(PipelineData::Value(record, None))
Ok(PipelineData::Value(
Value::record(dict.into_iter().collect(), head),
None,
))
}
#[cfg(test)]

View File

@ -3,8 +3,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,
};
#[derive(Clone)]
@ -44,13 +44,32 @@ impl Command for FromSsv {
example: r#"'FOO BAR
1 2' | from ssv"#,
description: "Converts ssv formatted string to table",
result: Some(Value::List { vals: vec![Value::Record { cols: vec!["FOO".to_string(), "BAR".to_string()], vals: vec![Value::test_string("1"), Value::test_string("2")], span: Span::test_data() }], span: Span::test_data() }),
result: Some(Value::List {
vals: vec![Value::test_record(Record {
cols: vec!["FOO".to_string(), "BAR".to_string()],
vals: vec![Value::test_string("1"), Value::test_string("2")],
})],
span: Span::test_data(),
}),
}, Example {
example: r#"'FOO BAR
1 2' | from ssv -n"#,
description: "Converts ssv formatted string to table but not treating the first row as column names",
result: Some(
Value::List { vals: vec![Value::Record { cols: vec!["column1".to_string(), "column2".to_string()], vals: vec![Value::test_string("FOO"), Value::test_string("BAR")], span: Span::test_data() }, Value::Record { cols: vec!["column1".to_string(), "column2".to_string()], vals: vec![Value::test_string("1"), Value::test_string("2")], span: Span::test_data() }], span: Span::test_data() }),
Value::List {
vals: vec![
Value::test_record(Record {
cols: vec!["column1".to_string(), "column2".to_string()],
vals: vec![Value::test_string("FOO"), Value::test_string("BAR")],
}),
Value::test_record(Record {
cols: vec!["column1".to_string(), "column2".to_string()],
vals: vec![Value::test_string("1"), Value::test_string("2")],
}),
],
span: Span::test_data(),
}
),
}]
}
@ -251,19 +270,13 @@ fn from_ssv_string_to_value(
span: Span,
) -> Value {
let rows = string_to_table(s, noheaders, aligned_columns, split_at)
.iter()
.into_iter()
.map(|row| {
let mut dict = IndexMap::new();
for (col, entry) in row {
dict.insert(
col.to_string(),
Value::String {
val: entry.to_string(),
span,
},
);
dict.insert(col, Value::string(entry, span));
}
Value::from(Spanned { item: dict, span })
Value::record(dict.into_iter().collect(), span)
})
.collect();

View File

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, Type,
Value,
};
#[derive(Clone)]
@ -27,17 +28,16 @@ impl Command for FromToml {
Example {
example: "'a = 1' | from toml",
description: "Converts toml formatted string to record",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec!["a".to_string()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
}),
})),
},
Example {
example: "'a = 1
b = [1, 2]' | from toml",
description: "Converts toml formatted string to record",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![
Value::test_int(1),
@ -46,8 +46,7 @@ b = [1, 2]' | from toml",
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
})),
},
]
}
@ -79,17 +78,12 @@ fn convert_toml_to_value(value: &toml::Value, span: Span) -> Value {
toml::Value::Boolean(b) => Value::Bool { val: *b, span },
toml::Value::Float(f) => Value::Float { val: *f, span },
toml::Value::Integer(i) => Value::Int { val: *i, span },
toml::Value::Table(k) => {
let mut cols = vec![];
let mut vals = vec![];
for item in k {
cols.push(item.0.clone());
vals.push(convert_toml_to_value(item.1, span));
}
Value::Record { cols, vals, span }
}
toml::Value::Table(k) => Value::record(
k.iter()
.map(|(k, v)| (k.clone(), convert_toml_to_value(v, span)))
.collect(),
span,
),
toml::Value::String(s) => Value::String {
val: s.clone(),
span,

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
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,
};
#[derive(Clone)]
@ -76,14 +76,13 @@ impl Command for FromTsv {
description: "Convert tab-separated data to a table",
example: "\"ColA\tColB\n1\t2\" | from tsv",
result: Some(Value::List {
vals: vec![Value::Record {
vals: vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::test_int(1),
Value::test_int(2),
],
span: Span::test_data(),
}],
})],
span: Span::test_data(),
})
},

View File

@ -132,41 +132,29 @@ fn from_xlsx(
sheet_names.retain(|e| sel_sheets.contains(e));
}
for sheet_name in &sheet_names {
for sheet_name in sheet_names {
let mut sheet_output = vec![];
if let Some(Ok(current_sheet)) = xlsx.worksheet_range(sheet_name) {
if let Some(Ok(current_sheet)) = xlsx.worksheet_range(&sheet_name) {
for row in current_sheet.rows() {
let mut row_output = IndexMap::new();
for (i, cell) in row.iter().enumerate() {
let value = match cell {
DataType::Empty => Value::nothing(head),
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::bool(*b, head),
_ => Value::nothing(head),
};
let record = row
.iter()
.enumerate()
.map(|(i, cell)| {
let value = match cell {
DataType::Empty => Value::nothing(head),
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::bool(*b, head),
_ => Value::nothing(head),
};
row_output.insert(format!("column{i}"), value);
}
(format!("column{i}"), value)
})
.collect();
let (cols, vals) =
row_output
.into_iter()
.fold((vec![], vec![]), |mut acc, (k, v)| {
acc.0.push(k);
acc.1.push(v);
acc
});
let record = Value::Record {
cols,
vals,
span: head,
};
sheet_output.push(record);
sheet_output.push(Value::record(record, head));
}
dict.insert(
@ -186,19 +174,10 @@ fn from_xlsx(
}
}
let (cols, vals) = dict.into_iter().fold((vec![], vec![]), |mut acc, (k, v)| {
acc.0.push(k.clone());
acc.1.push(v);
acc
});
let record = Value::Record {
cols,
vals,
span: head,
};
Ok(PipelineData::Value(record, None))
Ok(PipelineData::Value(
Value::record(dict.into_iter().collect(), head),
None,
))
}
#[cfg(test)]

View File

@ -3,7 +3,7 @@ use indexmap::map::IndexMap;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, Type,
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, Type,
Value,
};
use roxmltree::NodeType;
@ -69,38 +69,46 @@ string. This way content of every tag is always a table and is easier to parse"#
<remember>Event</remember>
</note>' | from xml"#,
description: "Converts xml formatted string to record",
result: Some(Value::test_record(
vec![COLUMN_TAG_NAME, COLUMN_ATTRS_NAME, COLUMN_CONTENT_NAME],
vec![
result: Some(Value::test_record(Record {
cols: vec![
COLUMN_TAG_NAME.to_string(),
COLUMN_ATTRS_NAME.to_string(),
COLUMN_CONTENT_NAME.to_string(),
],
vals: vec![
Value::test_string("note"),
Value::test_record(Vec::<&str>::new(), vec![]),
Value::test_record(Record::new()),
Value::list(
vec![Value::test_record(
vec![COLUMN_TAG_NAME, COLUMN_ATTRS_NAME, COLUMN_CONTENT_NAME],
vec![
vec![Value::test_record(Record {
cols: vec![
COLUMN_TAG_NAME.to_string(),
COLUMN_ATTRS_NAME.to_string(),
COLUMN_CONTENT_NAME.to_string(),
],
vals: vec![
Value::test_string("remember"),
Value::test_record(Vec::<&str>::new(), vec![]),
Value::test_record(Record::new()),
Value::list(
vec![Value::test_record(
vec![
COLUMN_TAG_NAME,
COLUMN_ATTRS_NAME,
COLUMN_CONTENT_NAME,
vec![Value::test_record(Record {
cols: vec![
COLUMN_TAG_NAME.to_string(),
COLUMN_ATTRS_NAME.to_string(),
COLUMN_CONTENT_NAME.to_string(),
],
vec![
vals: vec![
Value::test_nothing(),
Value::test_nothing(),
Value::test_string("Event"),
],
)],
})],
Span::test_data(),
),
],
)],
})],
Span::test_data(),
),
],
)),
})),
}]
}
}
@ -116,20 +124,7 @@ fn from_attributes_to_value(attributes: &[roxmltree::Attribute], info: &ParsingI
for a in attributes {
collected.insert(String::from(a.name()), Value::string(a.value(), info.span));
}
let (cols, vals) = collected
.into_iter()
.fold((vec![], vec![]), |mut acc, (k, v)| {
acc.0.push(k);
acc.1.push(v);
acc
});
Value::Record {
cols,
vals,
span: info.span,
}
Value::record(collected.into_iter().collect(), info.span)
}
fn element_to_value(n: &roxmltree::Node, info: &ParsingInfo) -> Value {
@ -151,7 +146,7 @@ fn element_to_value(n: &roxmltree::Node, info: &ParsingInfo) -> Value {
node.insert(String::from(COLUMN_ATTRS_NAME), attributes);
node.insert(String::from(COLUMN_CONTENT_NAME), content);
Value::from(Spanned { item: node, span })
Value::record(node.into_iter().collect(), span)
}
fn text_to_value(n: &roxmltree::Node, info: &ParsingInfo) -> Option<Value> {
@ -168,9 +163,7 @@ fn text_to_value(n: &roxmltree::Node, info: &ParsingInfo) -> Option<Value> {
node.insert(String::from(COLUMN_ATTRS_NAME), Value::nothing(span));
node.insert(String::from(COLUMN_CONTENT_NAME), content);
let result = Value::from(Spanned { item: node, span });
Some(result)
Some(Value::record(node.into_iter().collect(), span))
}
}
@ -188,9 +181,7 @@ fn comment_to_value(n: &roxmltree::Node, info: &ParsingInfo) -> Option<Value> {
node.insert(String::from(COLUMN_ATTRS_NAME), Value::nothing(span));
node.insert(String::from(COLUMN_CONTENT_NAME), content);
let result = Value::from(Spanned { item: node, span });
Some(result)
Some(Value::record(node.into_iter().collect(), span))
} else {
None
}
@ -213,9 +204,7 @@ fn processing_instruction_to_value(n: &roxmltree::Node, info: &ParsingInfo) -> O
node.insert(String::from(COLUMN_ATTRS_NAME), Value::nothing(span));
node.insert(String::from(COLUMN_CONTENT_NAME), content);
let result = Value::from(Spanned { item: node, span });
Some(result)
Some(Value::record(node.into_iter().collect(), span))
} else {
None
}
@ -332,20 +321,18 @@ mod tests {
use indexmap::indexmap;
use indexmap::IndexMap;
use nu_protocol::{Spanned, Value};
fn string(input: impl Into<String>) -> Value {
Value::test_string(input)
}
fn attributes(entries: IndexMap<&str, &str>) -> Value {
Value::from(Spanned {
item: entries
Value::test_record(
entries
.into_iter()
.map(|(k, v)| (k.into(), string(v)))
.collect::<IndexMap<String, Value>>(),
span: Span::test_data(),
})
.collect(),
)
}
fn table(list: &[Value]) -> Value {
@ -360,24 +347,28 @@ mod tests {
attrs: IndexMap<&str, &str>,
content: &[Value],
) -> Value {
Value::from(Spanned {
item: indexmap! {
COLUMN_TAG_NAME.into() => string(tag),
COLUMN_ATTRS_NAME.into() => attributes(attrs),
COLUMN_CONTENT_NAME.into() => table(content),
},
span: Span::test_data(),
Value::test_record(Record {
cols: vec![
COLUMN_TAG_NAME.into(),
COLUMN_ATTRS_NAME.into(),
COLUMN_CONTENT_NAME.into(),
],
vals: vec![string(tag), attributes(attrs), table(content)],
})
}
fn content_string(value: impl Into<String>) -> Value {
Value::from(Spanned {
item: indexmap! {
COLUMN_TAG_NAME.into() => Value::nothing(Span::test_data()),
COLUMN_ATTRS_NAME.into() => Value::nothing(Span::test_data()),
COLUMN_CONTENT_NAME.into() => string(value),
},
span: Span::test_data(),
Value::test_record(Record {
cols: vec![
COLUMN_TAG_NAME.into(),
COLUMN_ATTRS_NAME.into(),
COLUMN_CONTENT_NAME.into(),
],
vals: vec![
Value::nothing(Span::test_data()),
Value::nothing(Span::test_data()),
string(value),
],
})
}

View File

@ -3,7 +3,7 @@ use itertools::Itertools;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, Type,
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span, Type,
Value,
};
use serde::de::Deserialize;
@ -112,11 +112,8 @@ fn convert_yaml_value_to_nu_value(
}
}
serde_yaml::Value::Mapping(t) => {
let mut collected = Spanned {
// Using an IndexMap ensures consistent ordering
item: IndexMap::new(),
span,
};
// Using an IndexMap ensures consistent ordering
let mut collected = IndexMap::new();
for (k, v) in t {
// A ShellError that we re-use multiple times in the Mapping scenario
@ -128,19 +125,19 @@ fn convert_yaml_value_to_nu_value(
);
match (k, v) {
(serde_yaml::Value::Number(k), _) => {
collected.item.insert(
collected.insert(
k.to_string(),
convert_yaml_value_to_nu_value(v, span, val_span)?,
);
}
(serde_yaml::Value::Bool(k), _) => {
collected.item.insert(
collected.insert(
k.to_string(),
convert_yaml_value_to_nu_value(v, span, val_span)?,
);
}
(serde_yaml::Value::String(k), _) => {
collected.item.insert(
collected.insert(
k.clone(),
convert_yaml_value_to_nu_value(v, span, val_span)?,
);
@ -173,7 +170,7 @@ fn convert_yaml_value_to_nu_value(
}
}
Value::from(collected)
Value::record(collected.into_iter().collect(), span)
}
serde_yaml::Value::Tagged(t) => {
let tag = &t.tag;
@ -238,30 +235,27 @@ pub fn get_examples() -> Vec<Example<'static>> {
Example {
example: "'a: 1' | from yaml",
description: "Converts yaml formatted string to table",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec!["a".to_string()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
}),
})),
},
Example {
example: "'[ a: 1, b: [1, 2] ]' | from yaml",
description: "Converts yaml formatted string to table",
result: Some(Value::List {
vals: vec![
Value::Record {
Value::test_record(Record {
cols: vec!["a".to_string()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
},
Value::Record {
}),
Value::test_record(Record {
cols: vec!["b".to_string()],
vals: vec![Value::List {
vals: vec![Value::test_int(1), Value::test_int(2)],
span: Span::test_data(),
}],
span: Span::test_data(),
},
}),
],
span: Span::test_data(),
}),
@ -294,20 +288,18 @@ mod test {
TestCase {
description: "Double Curly Braces With Quotes",
input: r#"value: "{{ something }}""#,
expected: Ok(Value::Record {
expected: Ok(Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::test_string("{{ something }}")],
span: Span::test_data(),
}),
})),
},
TestCase {
description: "Double Curly Braces Without Quotes",
input: r#"value: {{ something }}"#,
expected: Ok(Value::Record {
expected: Ok(Value::test_record(Record {
cols: vec!["value".to_string()],
vals: vec![Value::test_string("{{ something }}")],
span: Span::test_data(),
}),
})),
},
];
let config = Config::default();
@ -359,16 +351,14 @@ mod test {
let expected: Result<Value, ShellError> = Ok(Value::List {
vals: vec![
Value::Record {
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_string("b"), Value::test_string("c")],
span: Span::test_data(),
},
Value::Record {
}),
Value::test_record(Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![Value::test_string("g"), Value::test_string("h")],
span: Span::test_data(),
},
}),
],
span: Span::test_data(),
});
@ -388,15 +378,15 @@ mod test {
let actual_record = actual_vals[jj].as_record().unwrap();
let expected_record = expected_vals[jj].as_record().unwrap();
let actual_columns = actual_record.0;
let expected_columns = expected_record.0;
let actual_columns = &actual_record.cols;
let expected_columns = &expected_record.cols;
assert_eq!(
expected_columns, actual_columns,
"record {jj}, iteration {ii}"
);
let actual_vals = actual_record.1;
let expected_vals = expected_record.1;
let actual_vals = &actual_record.vals;
let expected_vals = &expected_record.vals;
assert_eq!(expected_vals, actual_vals, "record {jj}, iteration {ii}")
}
}
@ -412,43 +402,38 @@ mod test {
let test_cases: Vec<TestCase> = vec![
TestCase {
input: "Key: !Value ${TEST}-Test-role",
expected: Ok(Value::Record {
expected: Ok(Value::test_record(Record {
cols: vec!["Key".to_string()],
vals: vec![Value::test_string("!Value ${TEST}-Test-role")],
span: Span::test_data(),
}),
})),
},
TestCase {
input: "Key: !Value test-${TEST}",
expected: Ok(Value::Record {
expected: Ok(Value::test_record(Record {
cols: vec!["Key".to_string()],
vals: vec![Value::test_string("!Value test-${TEST}")],
span: Span::test_data(),
}),
})),
},
TestCase {
input: "Key: !Value",
expected: Ok(Value::Record {
expected: Ok(Value::test_record(Record {
cols: vec!["Key".to_string()],
vals: vec![Value::test_string("!Value")],
span: Span::test_data(),
}),
})),
},
TestCase {
input: "Key: !True",
expected: Ok(Value::Record {
expected: Ok(Value::test_record(Record {
cols: vec!["Key".to_string()],
vals: vec![Value::test_string("!True")],
span: Span::test_data(),
}),
})),
},
TestCase {
input: "Key: !123",
expected: Ok(Value::Record {
expected: Ok(Value::test_record(Record {
cols: vec!["Key".to_string()],
vals: vec![Value::test_string("!123")],
span: Span::test_data(),
}),
})),
},
];

View File

@ -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)?);

View File

@ -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)

View File

@ -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(),
};

View File

@ -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}\"{}\": {}",

View File

@ -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() {

View File

@ -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");

View File

@ -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 {

View File

@ -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)?,