2020-03-03 22:01:24 +01:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
2020-10-02 03:13:42 +02:00
|
|
|
use indexmap::indexmap;
|
2020-03-03 22:01:24 +01:00
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
|
|
|
use nu_source::Tagged;
|
|
|
|
|
|
|
|
pub struct Rename;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Arguments {
|
|
|
|
column_name: Tagged<String>,
|
|
|
|
rest: Vec<Tagged<String>>,
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2020-03-03 22:01:24 +01:00
|
|
|
impl WholeStreamCommand for Rename {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"rename"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("rename")
|
|
|
|
.required(
|
|
|
|
"column_name",
|
|
|
|
SyntaxShape::String,
|
2020-05-12 17:54:29 +02:00
|
|
|
"the new name for the first column",
|
2020-03-03 22:01:24 +01:00
|
|
|
)
|
2020-05-12 17:54:29 +02:00
|
|
|
.rest(SyntaxShape::String, "the new name for additional columns")
|
2020-03-03 22:01:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Creates a new table with columns renamed."
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
async fn run(
|
2020-03-03 22:01:24 +01:00
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-06-13 10:43:21 +02:00
|
|
|
rename(args, registry).await
|
2020-03-03 22:01:24 +01:00
|
|
|
}
|
2020-05-12 17:54:29 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
2020-05-12 17:54:29 +02:00
|
|
|
Example {
|
|
|
|
description: "Rename a column",
|
2020-10-02 03:13:42 +02:00
|
|
|
example: "echo [[a, b]; [1, 2]] | rename my_column",
|
|
|
|
result: Some(vec![UntaggedValue::row(indexmap! {
|
|
|
|
"my_column".to_string() => UntaggedValue::int(1).into(),
|
|
|
|
"b".to_string() => UntaggedValue::int(2).into(),
|
|
|
|
})
|
|
|
|
.into()]),
|
2020-05-12 17:54:29 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Rename many columns",
|
2020-10-02 03:13:42 +02:00
|
|
|
example: "echo [[a, b, c]; [1, 2, 3]] | rename eggs ham bacon",
|
|
|
|
result: Some(vec![UntaggedValue::row(indexmap! {
|
|
|
|
"eggs".to_string() => UntaggedValue::int(1).into(),
|
|
|
|
"ham".to_string() => UntaggedValue::int(2).into(),
|
|
|
|
"bacon".to_string() => UntaggedValue::int(3).into(),
|
|
|
|
})
|
|
|
|
.into()]),
|
2020-05-12 17:54:29 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2020-03-03 22:01:24 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 10:43:21 +02:00
|
|
|
pub async fn rename(
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
let registry = registry.clone();
|
|
|
|
let name = args.call_info.name_tag.clone();
|
2020-06-13 10:43:21 +02:00
|
|
|
let (Arguments { column_name, rest }, input) = args.process(®istry).await?;
|
|
|
|
let mut new_column_names = vec![vec![column_name]];
|
|
|
|
new_column_names.push(rest);
|
2020-03-03 22:01:24 +01:00
|
|
|
|
2020-06-13 10:43:21 +02:00
|
|
|
let new_column_names = new_column_names.into_iter().flatten().collect::<Vec<_>>();
|
2020-03-03 22:01:24 +01:00
|
|
|
|
2020-06-13 10:43:21 +02:00
|
|
|
Ok(input
|
|
|
|
.map(move |item| {
|
2020-03-03 22:01:24 +01:00
|
|
|
if let Value {
|
|
|
|
value: UntaggedValue::Row(row),
|
|
|
|
tag,
|
|
|
|
} = item
|
|
|
|
{
|
|
|
|
let mut renamed_row = IndexMap::new();
|
|
|
|
|
|
|
|
for (idx, (key, value)) in row.entries.iter().enumerate() {
|
|
|
|
let key = if idx < new_column_names.len() {
|
|
|
|
&new_column_names[idx].item
|
|
|
|
} else {
|
|
|
|
key
|
|
|
|
};
|
|
|
|
|
|
|
|
renamed_row.insert(key.clone(), value.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
let out = UntaggedValue::Row(renamed_row.into()).into_value(tag);
|
|
|
|
|
2020-06-13 10:43:21 +02:00
|
|
|
ReturnSuccess::value(out)
|
2020-03-03 22:01:24 +01:00
|
|
|
} else {
|
2020-06-13 10:43:21 +02:00
|
|
|
ReturnSuccess::value(
|
2020-03-03 22:01:24 +01:00
|
|
|
UntaggedValue::Error(ShellError::labeled_error(
|
|
|
|
"no column names available",
|
|
|
|
"can't rename",
|
|
|
|
&name,
|
|
|
|
))
|
|
|
|
.into_untagged_value(),
|
2020-06-13 10:43:21 +02:00
|
|
|
)
|
2020-03-03 22:01:24 +01:00
|
|
|
}
|
2020-06-13 10:43:21 +02:00
|
|
|
})
|
|
|
|
.to_output_stream())
|
2020-03-03 22:01:24 +01:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Rename;
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 14:56:01 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
Ok(test_examples(Rename {})?)
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
|
|
|
}
|