2020-04-30 06:18:24 +02:00
|
|
|
use crate::commands::classified::block::run_block;
|
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::data::value::merge_values;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use nu_errors::ShellError;
|
2020-05-06 05:56:31 +02:00
|
|
|
use nu_protocol::{hir::Block, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
2020-04-30 06:18:24 +02:00
|
|
|
pub struct Merge;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct MergeArgs {
|
|
|
|
block: Block,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Merge {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"merge"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("merge").required(
|
|
|
|
"block",
|
|
|
|
SyntaxShape::Block,
|
|
|
|
"the block to run and merge into the table",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Merge a table."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
merge(args, registry)
|
2020-04-30 06:18:24 +02:00
|
|
|
}
|
2020-05-12 17:54:29 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
2020-05-12 17:54:29 +02:00
|
|
|
description: "Merge a 1-based index column with some ls output",
|
|
|
|
example: "ls | select name | keep 3 | merge { echo [1 2 3] | wrap index }",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 17:54:29 +02:00
|
|
|
}]
|
|
|
|
}
|
2020-04-30 06:18:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
fn merge(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let registry = registry.clone();
|
2020-05-27 06:50:26 +02:00
|
|
|
let scope = raw_args.call_info.scope.clone();
|
2020-04-30 06:18:24 +02:00
|
|
|
let stream = async_stream! {
|
2020-05-16 05:18:24 +02:00
|
|
|
let mut context = Context::from_raw(&raw_args, ®istry);
|
|
|
|
let name_tag = raw_args.call_info.name_tag.clone();
|
|
|
|
let (merge_args, mut input): (MergeArgs, _) = raw_args.process(®istry).await?;
|
|
|
|
let block = merge_args.block;
|
|
|
|
|
2020-04-30 06:18:24 +02:00
|
|
|
let table: Option<Vec<Value>> = match run_block(&block,
|
|
|
|
&mut context,
|
|
|
|
InputStream::empty(),
|
2020-05-27 06:50:26 +02:00
|
|
|
&scope.it,
|
|
|
|
&scope.vars,
|
|
|
|
&scope.env).await {
|
2020-04-30 06:18:24 +02:00
|
|
|
Ok(mut stream) => Some(stream.drain_vec().await),
|
|
|
|
Err(err) => {
|
|
|
|
yield Err(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let table = table.unwrap_or_else(|| vec![Value {
|
|
|
|
value: UntaggedValue::row(IndexMap::default()),
|
2020-05-16 05:18:24 +02:00
|
|
|
tag: name_tag,
|
2020-04-30 06:18:24 +02:00
|
|
|
}]);
|
|
|
|
|
|
|
|
let mut idx = 0;
|
|
|
|
|
|
|
|
while let Some(value) = input.next().await {
|
|
|
|
let other = table.get(idx);
|
|
|
|
|
|
|
|
match other {
|
|
|
|
Some(replacement) => {
|
|
|
|
match merge_values(&value.value, &replacement.value) {
|
|
|
|
Ok(merged_value) => yield ReturnSuccess::value(merged_value.into_value(&value.tag)),
|
|
|
|
Err(err) => {
|
|
|
|
let message = format!("The row at {:?} types mismatch", idx);
|
|
|
|
yield Err(ShellError::labeled_error("Could not merge", &message, &value.tag));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => yield ReturnSuccess::value(value),
|
|
|
|
}
|
|
|
|
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Merge;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(Merge {})
|
|
|
|
}
|
|
|
|
}
|