Oops, match semantics of each group/window (#967)

This commit is contained in:
JT 2022-02-06 21:26:01 -05:00 committed by GitHub
parent 8a373dd554
commit 84d3620d9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 30 deletions

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block_with_redirect, CallExt};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Signature, Category, Example, IntoInterruptiblePipelineData, PipelineData, Signature, Span, Spanned,
Span, Spanned, SyntaxShape, Value, SyntaxShape, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -73,7 +73,7 @@ impl Command for EachGroup {
span: call.head, span: call.head,
}; };
Ok(each_group_iterator.flatten().into_pipeline_data(ctrlc)) Ok(each_group_iterator.into_pipeline_data(ctrlc))
} }
} }
@ -87,7 +87,7 @@ struct EachGroupIterator {
} }
impl Iterator for EachGroupIterator { impl Iterator for EachGroupIterator {
type Item = PipelineData; type Item = Value;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let mut group = vec![]; let mut group = vec![];
@ -129,7 +129,7 @@ pub(crate) fn run_block_on_vec(
engine_state: EngineState, engine_state: EngineState,
stack: Stack, stack: Stack,
span: Span, span: Span,
) -> PipelineData { ) -> Value {
let value = Value::List { vals: input, span }; let value = Value::List { vals: input, span };
let mut stack = stack.captures_to_stack(&capture_block.captures); let mut stack = stack.captures_to_stack(&capture_block.captures);
@ -143,8 +143,8 @@ pub(crate) fn run_block_on_vec(
} }
match eval_block_with_redirect(&engine_state, &mut stack, block, PipelineData::new(span)) { match eval_block_with_redirect(&engine_state, &mut stack, block, PipelineData::new(span)) {
Ok(pipeline) => pipeline, Ok(pipeline) => pipeline.into_value(span),
Err(error) => Value::Error { error }.into_pipeline_data(), Err(error) => Value::Error { error },
} }
} }

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block_with_redirect, CallExt};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Signature, Category, Example, IntoInterruptiblePipelineData, PipelineData, Signature, Span, Spanned,
Span, Spanned, SyntaxShape, Value, SyntaxShape, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -113,7 +113,7 @@ impl Command for EachWindow {
stride, stride,
}; };
Ok(each_group_iterator.flatten().into_pipeline_data(ctrlc)) Ok(each_group_iterator.into_pipeline_data(ctrlc))
} }
} }
@ -129,7 +129,7 @@ struct EachWindowIterator {
} }
impl Iterator for EachWindowIterator { impl Iterator for EachWindowIterator {
type Item = PipelineData; type Item = Value;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let mut group = self.previous.clone(); let mut group = self.previous.clone();
@ -148,7 +148,7 @@ impl Iterator for EachWindowIterator {
break; break;
} }
} }
None => break, None => return None,
} }
} }
} else { } else {
@ -166,7 +166,7 @@ impl Iterator for EachWindowIterator {
break; break;
} }
} }
None => break, None => return None,
} }
} }
@ -197,7 +197,7 @@ pub(crate) fn run_block_on_vec(
engine_state: EngineState, engine_state: EngineState,
stack: Stack, stack: Stack,
span: Span, span: Span,
) -> PipelineData { ) -> Value {
let value = Value::List { vals: input, span }; let value = Value::List { vals: input, span };
let mut stack = stack.captures_to_stack(&capture_block.captures); let mut stack = stack.captures_to_stack(&capture_block.captures);
@ -211,8 +211,8 @@ pub(crate) fn run_block_on_vec(
} }
match eval_block_with_redirect(&engine_state, &mut stack, block, PipelineData::new(span)) { match eval_block_with_redirect(&engine_state, &mut stack, block, PipelineData::new(span)) {
Ok(pipeline) => pipeline, Ok(pipeline) => pipeline.into_value(span),
Err(error) => Value::Error { error }.into_pipeline_data(), Err(error) => Value::Error { error },
} }
} }

View File

@ -2,8 +2,8 @@ use nu_engine::{eval_block_with_redirect, CallExt};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Signature, Category, Example, IntoInterruptiblePipelineData, PipelineData, Signature, Spanned,
Spanned, SyntaxShape, Value, SyntaxShape, Value,
}; };
use rayon::prelude::*; use rayon::prelude::*;
@ -78,13 +78,12 @@ impl Command for ParEachGroup {
block, block,
PipelineData::new(span), PipelineData::new(span),
) { ) {
Ok(v) => v, Ok(v) => v.into_value(span),
Err(error) => Value::Error { error }.into_pipeline_data(), Err(error) => Value::Error { error },
} }
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
.into_iter() .into_iter()
.flatten()
.into_pipeline_data(ctrlc)) .into_pipeline_data(ctrlc))
} }
} }

View File

@ -12,42 +12,36 @@ fn each_works_separately() {
assert_eq!(actual.out, "[11,12,13]"); assert_eq!(actual.out, "[11,12,13]");
} }
// FIXME: jt: needs more work
#[ignore]
#[test] #[test]
fn each_group_works() { fn each_group_works() {
let actual = nu!( let actual = nu!(
cwd: "tests/fixtures/formats", pipeline( cwd: "tests/fixtures/formats", pipeline(
r#" r#"
echo [1 2 3 4 5 6] | each group 3 { $it } | to json echo [1 2 3 4 5 6] | each group 3 { $it } | to json --raw
"# "#
)); ));
assert_eq!(actual.out, "[[1,2,3],[4,5,6]]"); assert_eq!(actual.out, "[[1,2,3],[4,5,6]]");
} }
// FIXME: jt: needs more work
#[ignore]
#[test] #[test]
fn each_window() { fn each_window() {
let actual = nu!( let actual = nu!(
cwd: "tests/fixtures/formats", pipeline( cwd: "tests/fixtures/formats", pipeline(
r#" r#"
echo [1 2 3 4] | each window 3 { $it } | to json echo [1 2 3 4] | each window 3 { $it } | to json --raw
"# "#
)); ));
assert_eq!(actual.out, "[[1,2,3],[2,3,4]]"); assert_eq!(actual.out, "[[1,2,3],[2,3,4]]");
} }
// FIXME: jt: needs more work
#[ignore]
#[test] #[test]
fn each_window_stride() { fn each_window_stride() {
let actual = nu!( let actual = nu!(
cwd: "tests/fixtures/formats", pipeline( cwd: "tests/fixtures/formats", pipeline(
r#" r#"
echo [1 2 3 4 5 6] | each window 3 -s 2 { echo $it } | to json echo [1 2 3 4 5 6] | each window 3 -s 2 { echo $it } | to json --raw
"# "#
)); ));