mirror of
https://github.com/nushell/nushell.git
synced 2025-05-30 06:39:33 +02:00
Slight cleanup of 'from json' line-at-a-time (#4512)
This commit is contained in:
parent
393cb7ca6f
commit
fa75c93765
@ -76,8 +76,7 @@ impl Command for FromJson {
|
|||||||
) -> Result<nu_protocol::PipelineData, ShellError> {
|
) -> Result<nu_protocol::PipelineData, ShellError> {
|
||||||
let span = call.head;
|
let span = call.head;
|
||||||
let config = stack.get_config().unwrap_or_default();
|
let config = stack.get_config().unwrap_or_default();
|
||||||
let mut string_input = input.collect_string("", &config)?;
|
let string_input = input.collect_string("", &config)?;
|
||||||
string_input.push('\n');
|
|
||||||
|
|
||||||
// TODO: turn this into a structured underline of the nu_json error
|
// TODO: turn this into a structured underline of the nu_json error
|
||||||
if call.has_flag("objects") {
|
if call.has_flag("objects") {
|
||||||
@ -85,11 +84,14 @@ impl Command for FromJson {
|
|||||||
let lines: Vec<String> = string_input.lines().map(|x| x.to_string()).collect();
|
let lines: Vec<String> = string_input.lines().map(|x| x.to_string()).collect();
|
||||||
Ok(lines
|
Ok(lines
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(move |mut x| {
|
.filter_map(move |x| {
|
||||||
x.push('\n');
|
if x.trim() == "" {
|
||||||
match convert_string_to_value(x, span) {
|
None
|
||||||
Ok(v) => v,
|
} else {
|
||||||
Err(error) => Value::Error { error },
|
match convert_string_to_value(x, span) {
|
||||||
|
Ok(v) => Some(v),
|
||||||
|
Err(error) => Some(Value::Error { error }),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.into_pipeline_data(engine_state.ctrlc.clone()))
|
.into_pipeline_data(engine_state.ctrlc.clone()))
|
||||||
@ -152,8 +154,8 @@ fn convert_string_to_value(string_input: String, span: Span) -> Result<Value, Sh
|
|||||||
match result {
|
match result {
|
||||||
Ok(value) => Ok(convert_nujson_to_value(&value, span)),
|
Ok(value) => Ok(convert_nujson_to_value(&value, span)),
|
||||||
|
|
||||||
Err(_x) => Err(ShellError::CantConvert(
|
Err(x) => Err(ShellError::CantConvert(
|
||||||
"structured data from json".into(),
|
format!("structured data from json ({})", x),
|
||||||
"string".into(),
|
"string".into(),
|
||||||
span,
|
span,
|
||||||
)),
|
)),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user