feat: add disable field type inferencing for from csv and from tsv, fix: #3485 and #4217 (#5467)

This commit is contained in:
Justin Ma 2022-05-08 04:04:31 +08:00 committed by GitHub
parent 9b99b2f6ac
commit d08c072f19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View File

@ -26,6 +26,7 @@ impl Command for FromCsv {
"don't treat the first row as column names",
Some('n'),
)
.switch("no-infer", "no field type inferencing", None)
.named(
"trim",
SyntaxShape::String,
@ -98,6 +99,7 @@ fn from_csv(
) -> Result<PipelineData, ShellError> {
let name = call.head;
let no_infer = call.has_flag("no-infer");
let noheaders = call.has_flag("noheaders");
let separator: Option<Value> = call.get_flag(engine_state, stack, "separator")?;
let trim: Option<Value> = call.get_flag(engine_state, stack, "trim")?;
@ -123,7 +125,7 @@ fn from_csv(
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)]

View File

@ -4,6 +4,7 @@ use nu_protocol::{Config, IntoPipelineData, PipelineData, ShellError, Span, Valu
fn from_delimited_string_to_value(
s: String,
noheaders: bool,
no_infer: bool,
separator: char,
trim: Trim,
span: Span,
@ -26,6 +27,14 @@ fn from_delimited_string_to_value(
for row in reader.records() {
let mut output_row = vec![];
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>() {
output_row.push(Value::Int { val: i, span });
} else if let Ok(f) = value.parse::<f64>() {
@ -49,6 +58,7 @@ fn from_delimited_string_to_value(
pub fn from_delimited_data(
noheaders: bool,
no_infer: bool,
sep: char,
trim: Trim,
input: PipelineData,
@ -58,7 +68,7 @@ pub fn from_delimited_data(
let concat_string = input.collect_string("", config)?;
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))?
.into_pipeline_data(),
)

View File

@ -20,6 +20,7 @@ impl Command for FromTsv {
"don't treat the first row as column names",
Some('n'),
)
.switch("no-infer", "no field type inferencing", None)
.named(
"trim",
SyntaxShape::String,
@ -82,12 +83,14 @@ fn from_tsv(
) -> Result<PipelineData, ShellError> {
let name = call.head;
let no_infer = call.has_flag("no-infer");
let noheaders = call.has_flag("noheaders");
let trim: Option<Value> = call.get_flag(engine_state, stack, "trim")?;
let trim = trim_from_str(trim)?;
from_delimited_data(
noheaders,
no_infer,
'\t',
trim,
input,