Skip to content

Commit

Permalink
jsonl: add --delimiter option
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Aug 10, 2023
1 parent f724824 commit 1167236
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/cmd/jsonl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jsonl options:
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
-d, --delimiter <arg> The delimiter to use when writing CSV data.
Must be a single character. [default: ,]
"#;

use std::{
Expand All @@ -30,12 +32,16 @@ use std::{
use serde::Deserialize;
use serde_json::Value;

use crate::{config::Config, util, CliResult};
use crate::{
config::{Config, Delimiter},
util, CliResult,
};

#[derive(Deserialize)]
struct Args {
arg_input: Option<String>,
flag_output: Option<String>,
flag_delimiter: Option<Delimiter>,
flag_ignore_errors: bool,
}

Expand Down Expand Up @@ -131,7 +137,9 @@ fn json_line_to_csv_record(value: &Value, headers: &[Vec<String>]) -> csv::Strin

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let mut wtr = Config::new(&args.flag_output).writer()?;
let mut wtr = Config::new(&args.flag_output)
.delimiter(args.flag_delimiter)
.writer()?;

let rdr: Box<dyn BufRead> = match args.arg_input {
None => Box::new(BufReader::new(io::stdin())),
Expand Down
24 changes: 23 additions & 1 deletion tests/test_jsonl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::workdir::Workdir;

#[test]
fn jsonl_simple() {
let wrk = Workdir::new("jsonl");
let wrk = Workdir::new("jsonl_simple");
wrk.create_from_string(
"data.jsonl",
r#"{"id":1,"father":"Mark","mother":"Charlotte","oldest_child":"Tom","boy":true}
Expand All @@ -22,6 +22,28 @@ fn jsonl_simple() {
assert_eq!(got, expected);
}

#[test]
fn jsonl_simple_delimiter() {
let wrk = Workdir::new("jsonl_simple_delimiter");
wrk.create_from_string(
"data.jsonl",
r#"{"id":1,"father":"Mark","mother":"Charlotte","oldest_child":"Tom","boy":true}
{"id":2,"father":"John","mother":"Ann","oldest_child":"Jessika","boy":false}
{"id":3,"father":"Bob","mother":"Monika","oldest_child":"Jerry","boy":true}"#,
);
let mut cmd = wrk.command("jsonl");
cmd.args(["--delimiter", ";"]).arg("data.jsonl");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["id;father;mother;oldest_child;boy"],
svec!["1;Mark;Charlotte;Tom;true"],
svec!["2;John;Ann;Jessika;false"],
svec!["3;Bob;Monika;Jerry;true"],
];
assert_eq!(got, expected);
}

#[test]
fn jsonl_simple_error() {
let wrk = Workdir::new("jsonl");
Expand Down

0 comments on commit 1167236

Please sign in to comment.