Fix matching numbers against the regex

Still, `Null`, `Bool`, `Array` and `Object` types are
unmatchable. Work TBD...
This commit is contained in:
finga 2021-04-13 16:55:13 +02:00
parent 7f143e0b08
commit ea3121a985

View file

@ -189,6 +189,15 @@ fn replace_parameter(input: &str, headers: &HeaderMap, data: &serde_json::Value)
Ok(result.join("")) Ok(result.join(""))
} }
fn number_to_string(number: &serde_json::Number) -> String {
if number.is_f64() {
return number.as_f64().unwrap().to_string();
} else if number.is_i64() {
return number.as_i64().unwrap().to_string();
}
number.as_u64().unwrap().to_string()
}
fn filter_match( fn filter_match(
hook_name: &str, hook_name: &str,
hook: &Hook, hook: &Hook,
@ -202,8 +211,16 @@ fn filter_match(
let regex = Regex::new(&filter.regex)?; let regex = Regex::new(&filter.regex)?;
if let Some(value) = data.pointer(&filter.pointer) { if let Some(value) = data.pointer(&filter.pointer) {
if let Some(value) = value.as_str() { let value = match &value {
if regex.is_match(value) { serde_json::Value::Null => unimplemented!(),
serde_json::Value::Bool(_bool) => unimplemented!(),
serde_json::Value::Number(number) => number_to_string(number),
serde_json::Value::String(string) => string.as_str().to_string(),
serde_json::Value::Array(_array) => unimplemented!(),
serde_json::Value::Object(_object) => unimplemented!(),
};
if regex.is_match(&value) {
debug!("Filter `{}` of hook `{}` matched", filter_name, hook_name); debug!("Filter `{}` of hook `{}` matched", filter_name, hook_name);
return Ok(Some(replace_parameter( return Ok(Some(replace_parameter(
@ -212,16 +229,9 @@ fn filter_match(
data, data,
)?)); )?));
} }
} else {
bail!(
"Could not parse pointer in hook `{}` from filter `{}`",
hook_name,
filter_name
);
}
} }
trace!( debug!(
"Filter `{}` of hook `{}` did not match", "Filter `{}` of hook `{}` did not match",
filter_name, filter_name,
hook_name hook_name