Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up get_json_pointer #678

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions benches/json_pointer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(test)]
extern crate tera;
extern crate test;

#[bench]
fn bench_get_json_pointer(b: &mut test::Bencher) {
b.iter(|| tera::get_json_pointer("foo.bar.baz"))
}

#[bench]
fn bench_get_json_pointer_with_map(b: &mut test::Bencher) {
b.iter(|| tera::get_json_pointer("foo[\"http://example.com/\"].bar.baz"))
}
27 changes: 22 additions & 5 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use std::io::Write;
use std::{collections::BTreeMap, iter};

use serde::ser::Serialize;
use serde_json::value::{to_value, Map, Value};
Expand Down Expand Up @@ -212,12 +212,17 @@ pub fn get_json_pointer(key: &str) -> String {
lazy_static::lazy_static! {
// Split the key into dot-separated segments, respecting quoted strings as single units
// to fix https://github.com/Keats/tera/issues/590
static ref JSON_POINTER_REGEX: regex::Regex = regex::Regex::new("\"[^\"]*\"|[^.]+").unwrap();
static ref JSON_POINTER_REGEX: regex::Regex = regex::Regex::new(r#""[^"]*"|[^.]+"#).unwrap();
}

let mut segments = vec![""];
segments.extend(JSON_POINTER_REGEX.find_iter(key).map(|mat| mat.as_str().trim_matches('"')));
segments.join("/")
if key.find('"').is_some() {
let segments: Vec<&str> = iter::once("")
.chain(JSON_POINTER_REGEX.find_iter(key).map(|mat| mat.as_str().trim_matches('"')))
.collect();
segments.join("/")
} else {
["/", &key.replace(".", "/")].join("")
}
}

#[cfg(test)]
Expand All @@ -227,6 +232,18 @@ mod tests {
use serde_json::json;
use std::collections::HashMap;

#[test]
fn test_get_json_pointer() {
assert_eq!(get_json_pointer(""), "/");
assert_eq!(get_json_pointer("foo"), "/foo");
assert_eq!(get_json_pointer("foo.bar.baz"), "/foo/bar/baz");
assert_eq!(get_json_pointer(r#"foo["bar"].baz"#), r#"/foo["bar"]/baz"#);
assert_eq!(
get_json_pointer(r#"foo["bar"].baz["qux"].blub"#),
r#"/foo["bar"]/baz["qux"]/blub"#
);
}

#[test]
fn can_extend_context() {
let mut target = Context::new();
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ mod utils;

// Library exports.

// Template is meant to be used internally only but is exported for test/bench.
pub use crate::builtins::filters::Filter;
pub use crate::builtins::functions::Function;
pub use crate::builtins::testers::Test;
pub use crate::context::Context;
pub use crate::errors::{Error, ErrorKind, Result};
// Template and get_json_pointer are meant to be used internally only but is exported for test/bench.
#[doc(hidden)]
pub use crate::context::get_json_pointer;
#[doc(hidden)]
pub use crate::template::Template;
pub use crate::tera::Tera;
Expand Down