use regex::Regex;
use std::borrow::Cow;
#[derive(Debug, Clone, Default)]
pub struct JsonPathEvalContext {
regex_cache: Option<RegexCache>,
}
impl JsonPathEvalContext {
pub fn with_regex_cache() -> Self {
JsonPathEvalContext {
regex_cache: Some(RegexCache::default()),
}
}
pub(crate) fn regex(&mut self, pattern: &str) -> Result<Cow<'_, Regex>, regex::Error> {
match &mut self.regex_cache {
Some(cache) => cache.get_or_compile(pattern).map(Cow::Borrowed),
None => Regex::new(pattern).map(Cow::Owned),
}
}
}
#[derive(Debug, Clone, Default)]
struct RegexCache(Vec<Regex>);
impl RegexCache {
const CAPACITY: usize = 8;
fn get_or_compile(&mut self, pattern: &str) -> Result<&Regex, regex::Error> {
match self.0.iter().position(|regex| regex.as_str() == pattern) {
Some(hit) => self.0[..=hit].rotate_right(1),
None => {
let regex = Regex::new(pattern)?;
self.0.truncate(Self::CAPACITY - 1);
self.0.insert(0, regex);
}
}
Ok(&self.0[0])
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::parse_json_path;
use serde_json::json;
fn patterns(cache: &RegexCache) -> Vec<&str> {
cache.0.iter().map(Regex::as_str).collect()
}
#[test]
fn cache_keeps_the_patterns_used_last() {
let mut cache = RegexCache::default();
for i in 0..RegexCache::CAPACITY {
cache.get_or_compile(&format!("p{i}")).unwrap();
}
cache.get_or_compile("p0").unwrap();
cache.get_or_compile("next").unwrap();
assert_eq!(
patterns(&cache),
["next", "p0", "p7", "p6", "p5", "p4", "p3", "p2"]
);
}
#[test]
fn cache_skips_an_invalid_pattern() {
let mut cache = RegexCache::default();
cache.get_or_compile("a").unwrap();
assert!(cache.get_or_compile("a[").is_err());
assert_eq!(patterns(&cache), ["a"]);
}
#[test]
fn default_context_keeps_nothing() {
let mut ctx = JsonPathEvalContext::default();
assert!(ctx.regex("a").is_ok());
assert!(ctx.regex_cache.is_none());
}
#[test]
fn context_reaches_every_function_call() {
let json = json!({
"a": [{"b": "x1", "c": {"b": "x1"}}, {"b": "x2", "c": {"b": "x2"}}],
"p": "x."
});
for (path, compiled) in [
("$.a[?match(@.b, 'x.')]", 1),
("$.a[?!search(@.b, 'x.')]", 1),
("$.a[?match(@.b, $.p)]", 1),
("$.a[?@.b == 'y' || match(@.b, 'x.')]", 1),
("$.a[?@.b && (match(@.b, 'x.'))]", 1),
("$..[?match(@.b, 'x.')]", 1),
("$.a[?match(@.b, 'x1'), ?match(@.b, 'x2')]", 2),
("$.a[?@[?match(@.b, 'x.')]]", 1),
("$[?$.a[?match(@.b, 'x.')]]", 1),
("$.a[?count(@[?match(@.b, 'x.')]) == 1]", 1),
("$.a[?1 == count(@[?match(@.b, 'x.')])]", 1),
("$.a[?value(@[?match(@.b, 'x.')]) == 'x1']", 1),
("$.a[?length(@[?match(@.b, 'x.')]) == 1]", 1),
("$.a[?match(@[?match(@.b, 'x.')], 'y')]", 1),
("$.a[?match(@.b, @[?match(@.b, 'x.')])]", 1),
("$.a[?search(@[?search(@.b, 'x.')], 'y')]", 1),
("$.a[?search(@.b, @[?search(@.b, 'x.')])]", 1),
("$.a[?in(@.b, $.a[?match(@.b, 'x.')])]", 1),
("$.a[?in(!match(@.b, 'x.'), $.p)]", 1),
] {
let mut ctx = JsonPathEvalContext::with_regex_cache();
parse_json_path(path)
.and_then(|query| query.execute_with_context(&json, &mut ctx))
.expect(path);
assert_eq!(ctx.regex_cache.unwrap().0.len(), compiled, "{path}");
}
}
}