Skip to content

Commit

Permalink
release(parameters): release new version with parameter support for p…
Browse files Browse the repository at this point in the history
…atters
  • Loading branch information
Gidi committed Aug 7, 2019
1 parent 86e79fa commit 1b2b9db
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jg"
version = "0.1.6"
version = "0.1.7"
authors = ["Gidi Meir Morris <[email protected]>"]
edition = "2018"
description = "Jeff Goldblum (jg) is a command-line JSON processor. jg searches for structural patterns in json input and prints each json object that matches the pattern."
Expand Down
23 changes: 10 additions & 13 deletions src/input/parameter_substitution.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn apply_substitution(sources: &Vec<&str>, params: &Vec<&str>) -> Vec<String> {
let mut params = params.iter().peekable();
let mut params = params.iter().peekable();
sources
.iter()
.map(move |&src| {
Expand All @@ -11,10 +11,10 @@ pub fn apply_substitution(sources: &Vec<&str>, params: &Vec<&str>) -> Vec<String
} else {
break;
}
};
}
}
src
})
})
.collect()
}

Expand All @@ -24,49 +24,46 @@ mod tests {

#[test]
fn substitution_should_return_string_as_is_if_no_parameters_are_present() {
assert_eq!(
vec!["Jeff"],
apply_substitution(&vec!["Jeff"],&vec![])
);
assert_eq!(vec!["Jeff"], apply_substitution(&vec!["Jeff"], &vec![]));
}

#[test]
fn substitution_should_return_string_as_is_if_no_substitution_flag_is_present() {
assert_eq!(
vec!["Jeff"],
apply_substitution(&vec!["Jeff"],&vec!["Goldbloom"])
apply_substitution(&vec!["Jeff"], &vec!["Goldbloom"])
);
}

#[test]
fn substitution_should_replace_a_single_substitution_flag() {
assert_eq!(
vec!["Jeff Goldbloom"],
apply_substitution(&vec!["Jeff {}"],&vec!["Goldbloom"])
apply_substitution(&vec!["Jeff {}"], &vec!["Goldbloom"])
);
}

#[test]
fn substitution_should_replace_multiple_substitution_flag() {
assert_eq!(
vec!["Jeff Goldbloom"],
apply_substitution(&vec!["{} {}"],&vec!["Jeff", "Goldbloom"])
apply_substitution(&vec!["{} {}"], &vec!["Jeff", "Goldbloom"])
);
}

#[test]
fn substitution_leave_substitution_flag_untouched_if_tere_are_no_more_parameters() {
assert_eq!(
vec!["Jeff {}"],
apply_substitution(&vec!["{} {}"],&vec!["Jeff"])
apply_substitution(&vec!["{} {}"], &vec!["Jeff"])
);
}

#[test]
fn substitution_should_replace_across_multiple_sources() {
assert_eq!(
vec!["Jeff","Goldbloom"],
apply_substitution(&vec!["{}","{}"],&vec!["Jeff", "Goldbloom"])
vec!["Jeff", "Goldbloom"],
apply_substitution(&vec!["{}", "{}"], &vec!["Jeff", "Goldbloom"])
);
}
}
12 changes: 5 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ pub mod input;
mod selection;

pub fn json_grep(config: input::Config) -> Result<(), Option<String>> {

let lens_patterns = match config.params {
Some(ref params) => input::parameter_substitution::apply_substitution(
&config.matchers,
params
),
None => config.matchers.iter().map(|s| s.to_string()).collect()
Some(ref params) => {
input::parameter_substitution::apply_substitution(&config.matchers, params)
}
None => config.matchers.iter().map(|s| s.to_string()).collect(),
};

let matched_filters: Result<Vec<_>, String> = lens_patterns
.iter()
.map(|pattern| input::in_configured_case(pattern, &config))
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ fn main() {
})
.expect("No matcher pattern has been specified");


let config = jg::input::Config {
matchers: matched_filters,
params: matches.values_of("params").map(|values| values.collect::<Vec<_>>()),
params: matches
.values_of("params")
.map(|values| values.collect::<Vec<_>>()),
input: matches.value_of("file"),
print_only_count: matches.is_present("count"),
highlight_matches: match (matches.value_of("colour"), stdout_isatty()) {
Expand Down
32 changes: 17 additions & 15 deletions tests/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ mod parameters {
cmd.arg("--params").arg(r#"mp4"#);
cmd.arg("--params").arg(r#"aac"#);
let mut stdin_cmd = cmd.with_stdin();
let mut assert_cmd = stdin_cmd.buffer(r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
let mut assert_cmd = stdin_cmd.buffer(
r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
{"audio":{"format":"aac"}}
{"audio":{"format":"{}"}}
"#);
"#,
);

assert_cmd
.assert()
.success()
.stdout(r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
assert_cmd.assert().success().stdout(
r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
{"audio":{"format":"aac"}}
"#);
"#,
);
}

#[test]
Expand All @@ -52,18 +53,19 @@ mod parameters {
cmd.arg("-e").arg(r#".video.mimes[*="{}"]"#);
cmd.arg("-e").arg(r#".audio{"format"*:"{}"}"#);
cmd.arg("--params").arg(r#"mp4"#);

let mut stdin_cmd = cmd.with_stdin();
let mut assert_cmd = stdin_cmd.buffer(r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
let mut assert_cmd = stdin_cmd.buffer(
r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
{"audio":{"format":"aac"}}
{"audio":{"format":"---{}---"}}
"#);
"#,
);

assert_cmd
.assert()
.success()
.stdout(r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
assert_cmd.assert().success().stdout(
r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
{"audio":{"format":"---{}---"}}
"#);
"#,
);
}
}

0 comments on commit 1b2b9db

Please sign in to comment.