forked from extern/nushell
Support more Values to plain string. (#1169)
* Support more Values to plain string. * Continue converting to delimited data for simple values.
This commit is contained in:
parent
7451414b9e
commit
00c0327031
@ -287,7 +287,7 @@ fn to_bson(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
|
|||||||
UntaggedValue::binary(x).into_value(&name_tag),
|
UntaggedValue::binary(x).into_value(&name_tag),
|
||||||
),
|
),
|
||||||
_ => yield Err(ShellError::labeled_error_with_secondary(
|
_ => yield Err(ShellError::labeled_error_with_secondary(
|
||||||
"Expected a table with BSON-compatible structure.tag() from pipeline",
|
"Expected a table with BSON-compatible structure from pipeline",
|
||||||
"requires BSON-compatible input",
|
"requires BSON-compatible input",
|
||||||
name_span,
|
name_span,
|
||||||
"originates from here".to_string(),
|
"originates from here".to_string(),
|
||||||
|
@ -66,5 +66,6 @@ fn to_csv(
|
|||||||
}
|
}
|
||||||
_ => ',',
|
_ => ',',
|
||||||
};
|
};
|
||||||
|
|
||||||
to_delimited_data(headerless, sep, "CSV", runnable_context)
|
to_delimited_data(headerless, sep, "CSV", runnable_context)
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ use indexmap::{indexset, IndexSet};
|
|||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue, Value};
|
use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue, Value};
|
||||||
use nu_source::Spanned;
|
use nu_source::Spanned;
|
||||||
use nu_value_ext::get_data_by_key;
|
use nu_value_ext::{as_string, get_data_by_key};
|
||||||
|
|
||||||
fn from_value_to_delimited_string(
|
fn from_value_to_delimited_string(
|
||||||
tagged_value: &Value,
|
tagged_value: &Value,
|
||||||
@ -52,23 +52,28 @@ fn from_value_to_delimited_string(
|
|||||||
|
|
||||||
let merged_descriptors = merge_descriptors(&list);
|
let merged_descriptors = merge_descriptors(&list);
|
||||||
|
|
||||||
|
if merged_descriptors.is_empty() {
|
||||||
|
wtr.write_record(
|
||||||
|
list.iter()
|
||||||
|
.map(|ele| to_string_tagged_value(ele).unwrap_or_else(|_| String::new()))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
.expect("can not write");
|
||||||
|
} else {
|
||||||
wtr.write_record(merged_descriptors.iter().map(|item| &item.item[..]))
|
wtr.write_record(merged_descriptors.iter().map(|item| &item.item[..]))
|
||||||
.expect("can not write.");
|
.expect("can not write.");
|
||||||
|
|
||||||
for l in list {
|
for l in list {
|
||||||
let mut row = vec![];
|
let mut row = vec![];
|
||||||
for desc in &merged_descriptors {
|
for desc in &merged_descriptors {
|
||||||
match get_data_by_key(l, desc.borrow_spanned()) {
|
row.push(match get_data_by_key(l, desc.borrow_spanned()) {
|
||||||
Some(s) => {
|
Some(s) => to_string_tagged_value(&s)?,
|
||||||
row.push(to_string_tagged_value(&s)?);
|
None => String::new(),
|
||||||
}
|
});
|
||||||
None => {
|
|
||||||
row.push(String::new());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
wtr.write_record(&row).expect("can not write");
|
wtr.write_record(&row).expect("can not write");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
let v = String::from_utf8(wtr.into_inner().map_err(|_| {
|
let v = String::from_utf8(wtr.into_inner().map_err(|_| {
|
||||||
ShellError::labeled_error(
|
ShellError::labeled_error(
|
||||||
"Could not convert record",
|
"Could not convert record",
|
||||||
@ -127,19 +132,16 @@ pub fn clone_tagged_value(v: &Value) -> Value {
|
|||||||
// NOTE: could this be useful more widely and implemented on Value ?
|
// NOTE: could this be useful more widely and implemented on Value ?
|
||||||
fn to_string_tagged_value(v: &Value) -> Result<String, ShellError> {
|
fn to_string_tagged_value(v: &Value) -> Result<String, ShellError> {
|
||||||
match &v.value {
|
match &v.value {
|
||||||
|
UntaggedValue::Primitive(Primitive::String(_))
|
||||||
|
| UntaggedValue::Primitive(Primitive::Line(_))
|
||||||
|
| UntaggedValue::Primitive(Primitive::Bytes(_))
|
||||||
|
| UntaggedValue::Primitive(Primitive::Boolean(_))
|
||||||
|
| UntaggedValue::Primitive(Primitive::Decimal(_))
|
||||||
|
| UntaggedValue::Primitive(Primitive::Path(_))
|
||||||
|
| UntaggedValue::Primitive(Primitive::Int(_)) => as_string(v),
|
||||||
UntaggedValue::Primitive(Primitive::Date(d)) => Ok(d.to_string()),
|
UntaggedValue::Primitive(Primitive::Date(d)) => Ok(d.to_string()),
|
||||||
UntaggedValue::Primitive(Primitive::Bytes(b)) => {
|
|
||||||
let tmp = format!("{}", b);
|
|
||||||
Ok(tmp)
|
|
||||||
}
|
|
||||||
UntaggedValue::Primitive(Primitive::Boolean(_)) => Ok(v.as_string()?),
|
|
||||||
UntaggedValue::Primitive(Primitive::Decimal(_)) => Ok(v.as_string()?),
|
|
||||||
UntaggedValue::Primitive(Primitive::Int(_)) => Ok(v.as_string()?),
|
|
||||||
UntaggedValue::Primitive(Primitive::Path(_)) => Ok(v.as_string()?),
|
|
||||||
UntaggedValue::Table(_) => Ok(String::from("[Table]")),
|
UntaggedValue::Table(_) => Ok(String::from("[Table]")),
|
||||||
UntaggedValue::Row(_) => Ok(String::from("[Row]")),
|
UntaggedValue::Row(_) => Ok(String::from("[Row]")),
|
||||||
UntaggedValue::Primitive(Primitive::Line(s)) => Ok(s.to_string()),
|
|
||||||
UntaggedValue::Primitive(Primitive::String(s)) => Ok(s.to_string()),
|
|
||||||
_ => Err(ShellError::labeled_error(
|
_ => Err(ShellError::labeled_error(
|
||||||
"Unexpected value",
|
"Unexpected value",
|
||||||
"",
|
"",
|
||||||
@ -193,8 +195,8 @@ pub fn to_delimited_data(
|
|||||||
};
|
};
|
||||||
yield ReturnSuccess::value(UntaggedValue::Primitive(Primitive::String(converted)).into_value(&name_tag))
|
yield ReturnSuccess::value(UntaggedValue::Primitive(Primitive::String(converted)).into_value(&name_tag))
|
||||||
}
|
}
|
||||||
_ => {
|
Err(x) => {
|
||||||
let expected = format!("Expected a table with {}-compatible structure.tag() from pipeline", format_name);
|
let expected = format!("Expected a table with {}-compatible structure from pipeline", format_name);
|
||||||
let requires = format!("requires {}-compatible input", format_name);
|
let requires = format!("requires {}-compatible input", format_name);
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
expected,
|
expected,
|
||||||
|
@ -211,7 +211,7 @@ fn to_sqlite(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
|
|||||||
Ok(out) => yield ReturnSuccess::value(out),
|
Ok(out) => yield ReturnSuccess::value(out),
|
||||||
_ => {
|
_ => {
|
||||||
yield Err(ShellError::labeled_error(
|
yield Err(ShellError::labeled_error(
|
||||||
"Expected a table with SQLite-compatible structure.tag() from pipeline",
|
"Expected a table with SQLite-compatible structure from pipeline",
|
||||||
"requires SQLite-compatible input",
|
"requires SQLite-compatible input",
|
||||||
name_tag,
|
name_tag,
|
||||||
))
|
))
|
||||||
|
@ -151,7 +151,7 @@ fn to_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
|
|||||||
UntaggedValue::Primitive(Primitive::String(x)).into_value(&name_tag),
|
UntaggedValue::Primitive(Primitive::String(x)).into_value(&name_tag),
|
||||||
),
|
),
|
||||||
_ => yield Err(ShellError::labeled_error_with_secondary(
|
_ => yield Err(ShellError::labeled_error_with_secondary(
|
||||||
"Expected a table with YAML-compatible structure.tag() from pipeline",
|
"Expected a table with YAML-compatible structure from pipeline",
|
||||||
"requires YAML-compatible input",
|
"requires YAML-compatible input",
|
||||||
name_span,
|
name_span,
|
||||||
"originates from here".to_string(),
|
"originates from here".to_string(),
|
||||||
|
Loading…
Reference in New Issue
Block a user