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