forked from extern/nushell
This commit is contained in:
parent
9b99b2f6ac
commit
d08c072f19
@ -26,6 +26,7 @@ impl Command for FromCsv {
|
|||||||
"don't treat the first row as column names",
|
"don't treat the first row as column names",
|
||||||
Some('n'),
|
Some('n'),
|
||||||
)
|
)
|
||||||
|
.switch("no-infer", "no field type inferencing", None)
|
||||||
.named(
|
.named(
|
||||||
"trim",
|
"trim",
|
||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
@ -98,6 +99,7 @@ fn from_csv(
|
|||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let name = call.head;
|
let name = call.head;
|
||||||
|
|
||||||
|
let no_infer = call.has_flag("no-infer");
|
||||||
let noheaders = call.has_flag("noheaders");
|
let noheaders = call.has_flag("noheaders");
|
||||||
let separator: Option<Value> = call.get_flag(engine_state, stack, "separator")?;
|
let separator: Option<Value> = call.get_flag(engine_state, stack, "separator")?;
|
||||||
let trim: Option<Value> = call.get_flag(engine_state, stack, "trim")?;
|
let trim: Option<Value> = call.get_flag(engine_state, stack, "trim")?;
|
||||||
@ -123,7 +125,7 @@ fn from_csv(
|
|||||||
|
|
||||||
let trim = trim_from_str(trim)?;
|
let trim = trim_from_str(trim)?;
|
||||||
|
|
||||||
from_delimited_data(noheaders, sep, trim, input, name, config)
|
from_delimited_data(noheaders, no_infer, sep, trim, input, name, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -4,6 +4,7 @@ use nu_protocol::{Config, IntoPipelineData, PipelineData, ShellError, Span, Valu
|
|||||||
fn from_delimited_string_to_value(
|
fn from_delimited_string_to_value(
|
||||||
s: String,
|
s: String,
|
||||||
noheaders: bool,
|
noheaders: bool,
|
||||||
|
no_infer: bool,
|
||||||
separator: char,
|
separator: char,
|
||||||
trim: Trim,
|
trim: Trim,
|
||||||
span: Span,
|
span: Span,
|
||||||
@ -26,6 +27,14 @@ fn from_delimited_string_to_value(
|
|||||||
for row in reader.records() {
|
for row in reader.records() {
|
||||||
let mut output_row = vec![];
|
let mut output_row = vec![];
|
||||||
for value in row?.iter() {
|
for value in row?.iter() {
|
||||||
|
if no_infer {
|
||||||
|
output_row.push(Value::String {
|
||||||
|
span,
|
||||||
|
val: value.into(),
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if let Ok(i) = value.parse::<i64>() {
|
if let Ok(i) = value.parse::<i64>() {
|
||||||
output_row.push(Value::Int { val: i, span });
|
output_row.push(Value::Int { val: i, span });
|
||||||
} else if let Ok(f) = value.parse::<f64>() {
|
} else if let Ok(f) = value.parse::<f64>() {
|
||||||
@ -49,6 +58,7 @@ fn from_delimited_string_to_value(
|
|||||||
|
|
||||||
pub fn from_delimited_data(
|
pub fn from_delimited_data(
|
||||||
noheaders: bool,
|
noheaders: bool,
|
||||||
|
no_infer: bool,
|
||||||
sep: char,
|
sep: char,
|
||||||
trim: Trim,
|
trim: Trim,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
@ -58,7 +68,7 @@ pub fn from_delimited_data(
|
|||||||
let concat_string = input.collect_string("", config)?;
|
let concat_string = input.collect_string("", config)?;
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
from_delimited_string_to_value(concat_string, noheaders, sep, trim, name)
|
from_delimited_string_to_value(concat_string, noheaders, no_infer, sep, trim, name)
|
||||||
.map_err(|x| ShellError::DelimiterError(x.to_string(), name))?
|
.map_err(|x| ShellError::DelimiterError(x.to_string(), name))?
|
||||||
.into_pipeline_data(),
|
.into_pipeline_data(),
|
||||||
)
|
)
|
||||||
|
@ -20,6 +20,7 @@ impl Command for FromTsv {
|
|||||||
"don't treat the first row as column names",
|
"don't treat the first row as column names",
|
||||||
Some('n'),
|
Some('n'),
|
||||||
)
|
)
|
||||||
|
.switch("no-infer", "no field type inferencing", None)
|
||||||
.named(
|
.named(
|
||||||
"trim",
|
"trim",
|
||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
@ -82,12 +83,14 @@ fn from_tsv(
|
|||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let name = call.head;
|
let name = call.head;
|
||||||
|
|
||||||
|
let no_infer = call.has_flag("no-infer");
|
||||||
let noheaders = call.has_flag("noheaders");
|
let noheaders = call.has_flag("noheaders");
|
||||||
let trim: Option<Value> = call.get_flag(engine_state, stack, "trim")?;
|
let trim: Option<Value> = call.get_flag(engine_state, stack, "trim")?;
|
||||||
let trim = trim_from_str(trim)?;
|
let trim = trim_from_str(trim)?;
|
||||||
|
|
||||||
from_delimited_data(
|
from_delimited_data(
|
||||||
noheaders,
|
noheaders,
|
||||||
|
no_infer,
|
||||||
'\t',
|
'\t',
|
||||||
trim,
|
trim,
|
||||||
input,
|
input,
|
||||||
|
Loading…
Reference in New Issue
Block a user