Skip to content

Commit

Permalink
Fix some interpolator cases
Browse files Browse the repository at this point in the history
Added some extra code to cover more cases in the new interpolator
mechanism. Also, fixing some clippy offenses too.
  • Loading branch information
fcsonline committed Dec 21, 2022
1 parent f1443f1 commit a00b30c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 10 additions & 0 deletions example/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,13 @@ plan:
- 75
shuffle: true
pick: 1

- name: Complex access
request:
url: /api/users.json
assign: complex

- name: Assert request response code
assert:
key: complex.body[1].phones[1]
value: '+44 2345678'
10 changes: 6 additions & 4 deletions src/interpolator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static INTERPOLATION_SUFFIX: &str = "}}";

lazy_static! {
pub static ref INTERPOLATION_REGEX: Regex = {
let regexp = format!("{}{}{}", regex::escape(INTERPOLATION_PREFIX), r" *([a-zA-Z\-\._\[]+[a-zA-Z\-\._0-9\]]*) *", regex::escape(INTERPOLATION_SUFFIX));
let regexp = format!("{}{}{}", regex::escape(INTERPOLATION_PREFIX), r" *([a-zA-Z]+[a-zA-Z\-\._0-9\[\]]*) *", regex::escape(INTERPOLATION_SUFFIX));

Regex::new(regexp.as_str()).unwrap()
};
Expand Down Expand Up @@ -59,10 +59,10 @@ impl<'a> Interpolator<'a> {
}

fn resolve_context_interpolation(&self, value: &str) -> Option<String> {
//convert "." and "[" to "/" and "]" to "" to look like a json pointer
let val: String = format!("/{}", value.replace(".", "/").replace("[", "/").replace("]", ""));
// convert "." and "[" to "/" and "]" to "" to look like a json pointer
let val: String = format!("/{}", value.replace(['.', '['], "/").replace(']', ""));

//force the context into a Value, and acess by pointer
// force the context into a Value, and acess by pointer
if let Some(item) = json!(self.context).pointer(&val).to_owned() {
return Some(match item.to_owned() {
serde_json::Value::Null => "".to_owned(),
Expand Down Expand Up @@ -107,6 +107,7 @@ mod tests {
context.insert(String::from("Array"), json!(["a", "b", "c"]));
context.insert(String::from("Object"), json!({"this": "that"}));
context.insert(String::from("Nested"), json!({"this": {"that": {"those": [{"wow": 1}, {"so": 2}, {"deee": {"eeee": "eeep"}}]}}}));
context.insert(String::from("ArrayNested"), json!([{"a": [{}, {"aa": 2, "aaa": [{"aaaa": 123 }]}]}]));

let interpolator = Interpolator::new(&context);

Expand All @@ -117,6 +118,7 @@ mod tests {
assert_eq!(interpolator.resolve("{{ Array }}", true), "[\"a\",\"b\",\"c\"]".to_string());
assert_eq!(interpolator.resolve("{{ Object }}", true), "{\"this\":\"that\"}".to_string());
assert_eq!(interpolator.resolve("{{ Nested.this.that.those[2].deee.eeee }}", true), "eeep".to_string());
assert_eq!(interpolator.resolve("{{ ArrayNested[0].a[1].aaa[0].aaaa }}", true), "123".to_string());
}

#[test]
Expand Down

0 comments on commit a00b30c

Please sign in to comment.