Fix per-item run_alias (#1669)

* Fix per-item run_alias

* Fix 1609 also
This commit is contained in:
Jonathan Turner 2020-04-27 18:10:34 +12:00 committed by GitHub
parent 8bd3cedce1
commit 006171d669
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 61 deletions

View File

@ -489,10 +489,6 @@ impl Command {
scope: raw_args.call_info.scope.clone().set_it(x.clone()),
}
.evaluate(&registry);
// let call_info = raw_args
// .clone()
// .call_info
// .evaluate(&registry, &Scope::it_value(x.clone()));
match call_info {
Ok(call_info) => match command.run(&call_info, &registry, &raw_args, x) {

View File

@ -6,7 +6,7 @@ use crate::prelude::*;
use futures::stream::once;
use nu_errors::ShellError;
use nu_protocol::{hir::Block, ReturnSuccess, Scope, Signature, SyntaxShape};
use nu_protocol::{hir::Block, ReturnSuccess, Signature, SyntaxShape};
pub struct Each;
@ -47,6 +47,7 @@ fn each(
raw_args: RawCommandArgs,
) -> Result<OutputStream, ShellError> {
let block = each_args.block;
let scope = raw_args.call_info.scope.clone();
let registry = context.registry.clone();
let mut input_stream = context.input;
let stream = async_stream! {
@ -59,7 +60,7 @@ fn each(
&block,
&mut context,
input_stream,
&Scope::new(input_clone),
&scope.clone().set_it(input_clone),
).await;
match result {

View File

@ -4,7 +4,7 @@ use crate::prelude::*;
use derive_new::new;
use nu_errors::ShellError;
use nu_protocol::{hir::Block, ReturnSuccess, Scope, Signature, SyntaxShape};
use nu_protocol::{hir::Block, ReturnSuccess, Signature, SyntaxShape};
#[derive(new, Clone)]
pub struct AliasCommand {
@ -43,67 +43,58 @@ impl WholeStreamCommand for AliasCommand {
let block = self.block.clone();
let alias_command = self.clone();
let mut context = Context::from_args(&args, &registry);
let mut input = args.input;
let input = args.input;
let stream = async_stream! {
while let Some(input) = input.next().await {
let call_info = call_info.clone();
let tag = tag.clone();
let evaluated = call_info.evaluate_with_new_it(&registry, &input)?;
let mut scope = Scope::it_value(input.clone());
if let Some(positional) = &evaluated.args.positional {
for (pos, arg) in positional.iter().enumerate() {
scope = scope.set_var(alias_command.args[pos].to_string(), arg.clone());
}
let mut scope = call_info.scope.clone();
let evaluated = call_info.evaluate(&registry)?;
if let Some(positional) = &evaluated.args.positional {
for (pos, arg) in positional.iter().enumerate() {
scope = scope.set_var(alias_command.args[pos].to_string(), arg.clone());
}
}
let input_clone = Ok(input.clone());
let input_stream = futures::stream::once(async { input_clone }).boxed().to_input_stream();
let result = run_block(
&block,
&mut context,
input,
&scope,
).await;
let result = run_block(
&block,
&mut context,
input_stream,
&scope
).await;
match result {
Ok(stream) if stream.is_empty() => {
yield Err(ShellError::labeled_error(
"Expected a block",
"alias needs a block",
tag,
));
match result {
Ok(stream) if stream.is_empty() => {
yield Err(ShellError::labeled_error(
"Expected a block",
"alias needs a block",
tag,
));
}
Ok(mut stream) => {
// We collect first to ensure errors are put into the context
while let Some(result) = stream.next().await {
yield Ok(ReturnSuccess::Value(result));
}
Ok(mut stream) => {
// We collect first to ensure errors are put into the context
while let Some(result) = stream.next().await {
yield Ok(ReturnSuccess::Value(result));
}
let errors = context.get_errors();
if let Some(x) = errors.first() {
//yield Err(error.clone());
yield Err(ShellError::labeled_error_with_secondary(
"Alias failed to run",
"alias failed to run",
tag.clone(),
x.to_string(),
tag
));
}
}
Err(e) => {
//yield Err(e);
let errors = context.get_errors();
if let Some(x) = errors.first() {
yield Err(ShellError::labeled_error_with_secondary(
"Alias failed to run",
"alias failed to run",
tag.clone(),
e.to_string(),
x.to_string(),
tag
));
}
}
Err(e) => {
yield Err(ShellError::labeled_error_with_secondary(
"Alias failed to run",
"alias failed to run",
tag.clone(),
e.to_string(),
tag
));
}
}
};

View File

@ -3,7 +3,7 @@ use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*;
use log::trace;
use nu_errors::ShellError;
use nu_protocol::{hir::ClassifiedCommand, Scope, Signature, SyntaxShape, UntaggedValue, Value};
use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
pub struct SkipWhile;
@ -32,6 +32,7 @@ impl WholeStreamCommand for SkipWhile {
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let scope = args.call_info.scope.clone();
let call_info = args.evaluate_once(&registry)?;
let block = call_info.args.expect_nth(0)?.clone();
@ -80,7 +81,8 @@ impl WholeStreamCommand for SkipWhile {
let objects = call_info.input.skip_while(move |item| {
let condition = condition.clone();
trace!("ITEM = {:?}", item);
let result = evaluate_baseline_expr(&*condition, &registry, &Scope::new(item.clone()));
let result =
evaluate_baseline_expr(&*condition, &registry, &scope.clone().set_it(item.clone()));
trace!("RESULT = {:?}", result);
let return_value = match result {

View File

@ -3,9 +3,7 @@ use crate::context::CommandRegistry;
use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{
hir::Block, hir::ClassifiedCommand, ReturnSuccess, Scope, Signature, SyntaxShape,
};
use nu_protocol::{hir::Block, hir::ClassifiedCommand, ReturnSuccess, Signature, SyntaxShape};
pub struct Where;
@ -36,7 +34,7 @@ impl WholeStreamCommand for Where {
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, where_command)?.run()
Ok(args.process_raw(registry, where_command)?.run())
}
}
fn where_command(
@ -47,6 +45,7 @@ fn where_command(
input,
..
}: RunnableContext,
raw_args: RawCommandArgs,
) -> Result<OutputStream, ShellError> {
let condition = {
if block.block.len() != 1 {
@ -78,12 +77,12 @@ fn where_command(
};
let mut input = input;
let scope = raw_args.call_info.scope;
let stream = async_stream! {
while let Some(input) = input.next().await {
//FIXME: should we use the scope that's brought in as well?
let condition = evaluate_baseline_expr(&condition, &registry, &Scope::new(input.clone()))?;
let condition = evaluate_baseline_expr(&condition, &registry, &scope.clone().set_it(input.clone()))?;
match condition.as_bool() {
Ok(b) => {