Columns can be renamed. (#1447)

This commit is contained in:
Andrés N. Robalino 2020-03-03 16:01:24 -05:00 committed by GitHub
parent f97f9d4af3
commit c731a5b628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 203 additions and 0 deletions

View File

@ -130,6 +130,11 @@ impl Dictionary {
self.entries.keys() self.entries.keys()
} }
/// Iterate the values in the Dictionary
pub fn values(&self) -> impl Iterator<Item = &Value> {
self.entries.values()
}
/// Checks if given key exists /// Checks if given key exists
pub fn contains_key(&self, key: &str) -> bool { pub fn contains_key(&self, key: &str) -> bool {
self.entries.contains_key(key) self.entries.contains_key(key)

View File

@ -318,6 +318,7 @@ pub fn create_default_context(
whole_stream_command(Default), whole_stream_command(Default),
whole_stream_command(SkipWhile), whole_stream_command(SkipWhile),
whole_stream_command(Range), whole_stream_command(Range),
whole_stream_command(Rename),
whole_stream_command(Uniq), whole_stream_command(Uniq),
// Table manipulation // Table manipulation
whole_stream_command(Wrap), whole_stream_command(Wrap),

View File

@ -69,6 +69,7 @@ pub(crate) mod range;
#[allow(unused)] #[allow(unused)]
pub(crate) mod reduce_by; pub(crate) mod reduce_by;
pub(crate) mod reject; pub(crate) mod reject;
pub(crate) mod rename;
pub(crate) mod reverse; pub(crate) mod reverse;
pub(crate) mod rm; pub(crate) mod rm;
pub(crate) mod save; pub(crate) mod save;
@ -172,6 +173,7 @@ pub(crate) use range::Range;
#[allow(unused_imports)] #[allow(unused_imports)]
pub(crate) use reduce_by::ReduceBy; pub(crate) use reduce_by::ReduceBy;
pub(crate) use reject::Reject; pub(crate) use reject::Reject;
pub(crate) use rename::Rename;
pub(crate) use reverse::Reverse; pub(crate) use reverse::Reverse;
pub(crate) use rm::Remove; pub(crate) use rm::Remove;
pub(crate) use save::Save; pub(crate) use save::Save;

97
src/commands/rename.rs Normal file
View File

@ -0,0 +1,97 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use indexmap::IndexMap;
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>>,
}
impl WholeStreamCommand for Rename {
fn name(&self) -> &str {
"rename"
}
fn signature(&self) -> Signature {
Signature::build("rename")
.required(
"column_name",
SyntaxShape::String,
"the name of the column to rename for",
)
.rest(
SyntaxShape::Member,
"Additional column name(s) to rename for",
)
}
fn usage(&self) -> &str {
"Creates a new table with columns renamed."
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, rename)?.run()
}
}
pub fn rename(
Arguments { column_name, rest }: Arguments,
RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
let mut new_column_names = vec![vec![column_name]];
new_column_names.push(rest);
let new_column_names = new_column_names.into_iter().flatten().collect::<Vec<_>>();
let stream = input
.values
.map(move |item| {
let mut result = VecDeque::new();
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);
result.push_back(ReturnSuccess::value(out));
} else {
result.push_back(ReturnSuccess::value(
UntaggedValue::Error(ShellError::labeled_error(
"no column names available",
"can't rename",
&name,
))
.into_untagged_value(),
));
}
futures::stream::iter(result)
})
.flatten();
Ok(stream.to_output_stream())
}

View File

@ -22,6 +22,7 @@ mod parse;
mod pick; mod pick;
mod prepend; mod prepend;
mod range; mod range;
mod rename;
mod reverse; mod reverse;
mod rm; mod rm;
mod save; mod save;

97
tests/commands/rename.rs Normal file
View File

@ -0,0 +1,97 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, nu_error, pipeline};
#[test]
fn changes_the_column_name() {
Playground::setup("rename_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_cuatro_mosqueteros.txt",
r#"
Andrés N. Robalino
Jonathan Turner
Yehuda Katz
Jason Gedge
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open los_cuatro_mosqueteros.txt
| lines
| wrap name
| rename mosqueteros
| get mosqueteros
| count
| echo $it
"#
));
assert_eq!(actual, "4");
})
}
#[test]
fn keeps_remaining_original_names_given_less_new_names_than_total_original_names() {
Playground::setup("rename_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_cuatro_mosqueteros.txt",
r#"
Andrés N. Robalino
Jonathan Turner
Yehuda Katz
Jason Gedge
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open los_cuatro_mosqueteros.txt
| lines
| wrap name
| default hit "arepa!"
| rename mosqueteros
| get hit
| count
| echo $it
"#
));
assert_eq!(actual, "4");
})
}
#[test]
fn errors_if_no_columns_present() {
Playground::setup("rename_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_cuatro_mosqueteros.txt",
r#"
Andrés N. Robalino
Jonathan Turner
Yehuda Katz
Jason Gedge
"#,
)]);
let actual = nu_error!(
cwd: dirs.test(), pipeline(
r#"
open los_cuatro_mosqueteros.txt
| lines
| rename mosqueteros
"#
));
assert!(
actual.contains("no column names available"),
format!("actual: {:?}", actual)
);
assert!(
actual.contains("can't rename"),
format!("actual: {:?}", actual)
);
})
}