Skip to content

Commit

Permalink
Merge pull request jqnatividad#1031 from jqnatividad/rename-all_generic
Browse files Browse the repository at this point in the history
`rename`: add `"_all generic"` special value for headers
  • Loading branch information
jqnatividad authored Jun 8, 2023
2 parents 58b1619 + 532de10 commit 45cdab4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/cmd/rename.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
static USAGE: &str = r#"
Rename the columns of CSV data efficiently.
Rename the columns of a CSV efficiently.
This command lets you rename the columns in CSV data. You must specify
all of the headers, and separate them by a comma.
This command lets you rename the columns in a CSV. The new column names
are given as a comma-separated list of names. The number of column names
given must match the number of columns in the CSV unless "_all_generic"
is used.
Change the name of the columns:
$ qsv rename id,name,title
Replace the headers with generic column names:
$ qsv rename _all_generic
Add generic column names to a CSV with no headers:
$ qsv rename _all_generic --no-headers
Use column names that contains commas and conflict with the separator:
$ qsv rename '"Date - Opening","Date - Actual Closing"'
Expand All @@ -16,6 +24,13 @@ Usage:
qsv rename [options] [--] <headers> [<input>]
qsv rename --help
rename arguments:
<headers> The new headers to use for the CSV.
Separate multiple headers with a comma.
If "_all_generic" is given, the headers will be renamed
to generic column names, where the column name uses
the format "_col_N" where N is the 1-based column index.
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
Expand All @@ -41,7 +56,7 @@ struct Args {
}

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let mut args: Args = util::get_args(USAGE, argv)?;

let rconfig = Config::new(&args.arg_input)
.delimiter(args.flag_delimiter)
Expand All @@ -51,6 +66,16 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
let mut wtr = Config::new(&args.flag_output).writer()?;
let headers = rdr.byte_headers()?;

if args.arg_headers.to_lowercase() == "_all_generic" {
let mut generic_headers = String::new();
for (i, _) in headers.iter().enumerate() {
generic_headers.push_str(&format!("_col_{},", i + 1));
}
// remove the trailing comma
generic_headers.pop();
args.arg_headers = generic_headers;
}

let mut new_rdr = csv::Reader::from_reader(args.arg_headers.as_bytes());
let new_headers = new_rdr.byte_headers()?;

Expand Down
47 changes: 47 additions & 0 deletions tests/test_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ fn rename() {
assert_eq!(got, expected);
}

#[test]
fn rename_generic() {
let wrk = Workdir::new("rename");
wrk.create(
"in.csv",
vec![
svec!["R", "S"],
svec!["1", "b"],
svec!["2", "a"],
svec!["3", "d"],
],
);

let mut cmd = wrk.command("rename");
cmd.arg("_all_generic").arg("in.csv");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["_col_1", "_col_2"],
svec!["1", "b"],
svec!["2", "a"],
svec!["3", "d"],
];
assert_eq!(got, expected);
}

#[test]
fn rename_noheaders() {
let wrk = Workdir::new("rename_noheaders");
Expand All @@ -46,3 +72,24 @@ fn rename_noheaders() {
];
assert_eq!(got, expected);
}

#[test]
fn rename_noheaders_generic() {
let wrk = Workdir::new("rename_noheaders");
wrk.create(
"in.csv",
vec![svec!["1", "b"], svec!["2", "a"], svec!["3", "d"]],
);

let mut cmd = wrk.command("rename");
cmd.arg("_ALL_Generic").arg("--no-headers").arg("in.csv");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["_col_1", "_col_2"],
svec!["1", "b"],
svec!["2", "a"],
svec!["3", "d"],
];
assert_eq!(got, expected);
}

0 comments on commit 45cdab4

Please sign in to comment.