2020-09-19 21:29:51 +00:00
|
|
|
use crate::command_registry::CommandRegistry;
|
2020-07-30 04:58:54 +00:00
|
|
|
use crate::commands::classified::block::run_block;
|
2020-04-27 02:04:54 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-12-05 20:15:41 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
use nu_errors::ShellError;
|
2020-08-26 10:56:45 +00:00
|
|
|
use nu_protocol::{
|
|
|
|
ColumnPath, Primitive, ReturnSuccess, Scope, Signature, SyntaxShape, UntaggedValue, Value,
|
|
|
|
};
|
2019-12-09 18:52:01 +00:00
|
|
|
use nu_value_ext::ValueExt;
|
2019-12-05 20:15:41 +00:00
|
|
|
|
2020-08-26 10:56:45 +00:00
|
|
|
use futures::stream::once;
|
2019-12-05 20:15:41 +00:00
|
|
|
pub struct Insert;
|
|
|
|
|
2020-04-27 02:04:54 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct InsertArgs {
|
|
|
|
column: ColumnPath,
|
|
|
|
value: Value,
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:22:52 +00:00
|
|
|
#[async_trait]
|
2020-04-27 02:04:54 +00:00
|
|
|
impl WholeStreamCommand for Insert {
|
2019-12-05 20:15:41 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"insert"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("insert")
|
|
|
|
.required(
|
|
|
|
"column",
|
|
|
|
SyntaxShape::ColumnPath,
|
|
|
|
"the column name to insert",
|
|
|
|
)
|
2020-07-30 04:58:54 +00:00
|
|
|
.required("value", SyntaxShape::Any, "the value to give the cell(s)")
|
2019-12-05 20:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-04-27 02:04:54 +00:00
|
|
|
"Insert a new column with a given value."
|
2019-12-05 20:15:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 08:22:52 +00:00
|
|
|
async fn run(
|
2019-12-05 20:15:41 +00:00
|
|
|
&self,
|
2020-04-27 02:04:54 +00:00
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
2019-12-05 20:15:41 +00:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-06-13 04:03:39 +00:00
|
|
|
insert(args, registry).await
|
2020-04-27 02:04:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 20:15:41 +00:00
|
|
|
|
2020-07-30 04:58:54 +00:00
|
|
|
async fn process_row(
|
|
|
|
scope: Arc<Scope>,
|
2020-09-19 21:29:51 +00:00
|
|
|
mut context: Arc<EvaluationContext>,
|
2020-07-30 04:58:54 +00:00
|
|
|
input: Value,
|
|
|
|
mut value: Arc<Value>,
|
2020-08-26 10:56:45 +00:00
|
|
|
field: Arc<ColumnPath>,
|
2020-07-30 04:58:54 +00:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let value = Arc::make_mut(&mut value);
|
2020-04-27 02:04:54 +00:00
|
|
|
|
2020-07-30 04:58:54 +00:00
|
|
|
Ok(match value {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Block(block),
|
2020-08-26 10:56:45 +00:00
|
|
|
tag: block_tag,
|
2020-07-30 04:58:54 +00:00
|
|
|
} => {
|
|
|
|
let for_block = input.clone();
|
|
|
|
let input_stream = once(async { Ok(for_block) }).to_input_stream();
|
2019-12-05 20:15:41 +00:00
|
|
|
|
2020-09-25 23:40:02 +00:00
|
|
|
let scope = Scope::append_it(scope, input.clone());
|
|
|
|
|
|
|
|
let result = run_block(&block, Arc::make_mut(&mut context), input_stream, scope).await;
|
2020-07-30 04:58:54 +00:00
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(mut stream) => {
|
2020-08-12 07:51:24 +00:00
|
|
|
let values = stream.drain_vec().await;
|
|
|
|
|
2020-07-30 04:58:54 +00:00
|
|
|
let errors = context.get_errors();
|
|
|
|
if let Some(error) = errors.first() {
|
|
|
|
return Err(error.clone());
|
|
|
|
}
|
|
|
|
|
2020-08-12 07:51:24 +00:00
|
|
|
let result = if values.len() == 1 {
|
|
|
|
let value = values
|
|
|
|
.get(0)
|
|
|
|
.ok_or_else(|| ShellError::unexpected("No value to insert with"))?;
|
|
|
|
|
|
|
|
value.clone()
|
|
|
|
} else if values.is_empty() {
|
|
|
|
UntaggedValue::nothing().into_untagged_value()
|
|
|
|
} else {
|
|
|
|
UntaggedValue::table(&values).into_untagged_value()
|
|
|
|
};
|
|
|
|
|
2020-07-30 04:58:54 +00:00
|
|
|
match input {
|
|
|
|
obj
|
|
|
|
@
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Row(_),
|
|
|
|
..
|
2020-08-26 10:56:45 +00:00
|
|
|
} => match obj.insert_data_at_column_path(&field, result) {
|
2020-08-12 07:51:24 +00:00
|
|
|
Ok(v) => OutputStream::one(ReturnSuccess::value(v)),
|
|
|
|
Err(e) => OutputStream::one(Err(e)),
|
|
|
|
},
|
2020-08-26 10:56:45 +00:00
|
|
|
_ => OutputStream::one(Err(ShellError::labeled_error(
|
2020-07-30 04:58:54 +00:00
|
|
|
"Unrecognized type in stream",
|
|
|
|
"original value",
|
2020-08-26 10:56:45 +00:00
|
|
|
block_tag.clone(),
|
2020-07-30 04:58:54 +00:00
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => OutputStream::one(Err(e)),
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 10:56:45 +00:00
|
|
|
value => match input {
|
2020-06-13 04:03:39 +00:00
|
|
|
Value {
|
2020-08-26 10:56:45 +00:00
|
|
|
value: UntaggedValue::Primitive(Primitive::Nothing),
|
2020-06-13 04:03:39 +00:00
|
|
|
..
|
2020-09-25 23:40:02 +00:00
|
|
|
} => match scope
|
|
|
|
.it()
|
|
|
|
.unwrap_or_else(|| UntaggedValue::nothing().into_untagged_value())
|
|
|
|
.insert_data_at_column_path(&field, value.clone())
|
|
|
|
{
|
2020-08-26 10:56:45 +00:00
|
|
|
Ok(v) => OutputStream::one(ReturnSuccess::value(v)),
|
|
|
|
Err(e) => OutputStream::one(Err(e)),
|
|
|
|
},
|
|
|
|
_ => match input.insert_data_at_column_path(&field, value.clone()) {
|
2020-07-30 04:58:54 +00:00
|
|
|
Ok(v) => OutputStream::one(ReturnSuccess::value(v)),
|
|
|
|
Err(e) => OutputStream::one(Err(e)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn insert(
|
|
|
|
raw_args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let registry = registry.clone();
|
2020-09-25 23:40:02 +00:00
|
|
|
let scope = raw_args.call_info.scope.clone();
|
2020-09-19 21:29:51 +00:00
|
|
|
let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry));
|
2020-07-30 04:58:54 +00:00
|
|
|
let (InsertArgs { column, value }, input) = raw_args.process(®istry).await?;
|
|
|
|
let value = Arc::new(value);
|
|
|
|
let column = Arc::new(column);
|
|
|
|
|
|
|
|
Ok(input
|
|
|
|
.then(move |input| {
|
|
|
|
let scope = scope.clone();
|
|
|
|
let context = context.clone();
|
|
|
|
let value = value.clone();
|
|
|
|
let column = column.clone();
|
|
|
|
|
|
|
|
async {
|
|
|
|
match process_row(scope, context, input, value, column).await {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => OutputStream::one(Err(e)),
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 04:03:39 +00:00
|
|
|
})
|
2020-07-30 04:58:54 +00:00
|
|
|
.flatten()
|
2020-06-13 04:03:39 +00:00
|
|
|
.to_output_stream())
|
2019-12-05 20:15:41 +00:00
|
|
|
}
|
2020-05-18 12:56:01 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Insert;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(Insert {})
|
|
|
|
}
|
|
|
|
}
|