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

feat: implementing jinja templater #972

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ name = "depth_map"
harness = false

[features]
default = ["python"]
serde = ["dep:serde"]
stringify = ["dep:serde_yaml", "serde"]
python = ["pyo3"]
Expand Down
5 changes: 5 additions & 0 deletions crates/lib/src/templaters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ use crate::core::config::FluffConfig;
use crate::templaters::placeholder::PlaceholderTemplater;
use crate::templaters::raw::RawTemplater;

#[cfg(feature = "python")]
use crate::templaters::jinja::JinjaTemplater;
#[cfg(feature = "python")]
use crate::templaters::python::PythonTemplater;

#[cfg(feature = "python")]
pub mod jinja;
pub mod placeholder;
#[cfg(feature = "python")]
pub mod python;
Expand All @@ -21,6 +25,7 @@ pub fn templaters() -> Vec<Box<dyn Templater>> {
Box::new(RawTemplater),
Box::new(PlaceholderTemplater),
Box::new(PythonTemplater),
Box::new(JinjaTemplater),
]
}

Expand Down
76 changes: 76 additions & 0 deletions crates/lib/src/templaters/jinja.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use super::python::PythonTemplatedFile;
use super::Templater;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PySlice};
use pyo3::{Py, PyAny, Python};
use sqruff_lib_core::errors::SQLFluffUserError;
use sqruff_lib_core::templaters::base::TemplatedFile;

pub struct JinjaTemplater;

const JINJA_FILE: &str = include_str!("python_templater.py");

impl Templater for JinjaTemplater {
fn name(&self) -> &'static str {
"jinja"
}

fn description(&self) -> &'static str {
todo!()
}

fn process(
&self,
in_str: &str,
f_name: &str,
config: Option<&crate::core::config::FluffConfig>,
formatter: Option<&crate::cli::formatters::OutputStreamFormatter>,
) -> Result<sqruff_lib_core::templaters::base::TemplatedFile, SQLFluffUserError> {
let templated_file = Python::with_gil(|py| -> PyResult<TemplatedFile> {
let fun: Py<PyAny> = PyModule::from_code_bound(py, JINJA_FILE, "", "")?
.getattr("process_from_rust")?
.into();

// pass object with Rust tuple of positional arguments
let py_dict = PyDict::new_bound(py);
let args = (in_str.to_string(), f_name.to_string(), py_dict);
let returned = fun.call1(py, args);

// Parse the returned value
let returned = returned?;
let templated_file: PythonTemplatedFile = returned.extract(py)?;
Ok(templated_file.to_templated_file())
})
.map_err(|e| SQLFluffUserError::new(format!("Python templater error: {:?}", e)))?;

Ok(templated_file)
}
}

#[cfg(test)]
mod tests {
use crate::core::config::FluffConfig;

use super::*;

const JINJA_STRING: &str = "SELECT * FROM {% for c in blah %}{{c}}{% if not loop.last %}, {% endif %}{% endfor %} WHERE {{condition}}\n\n";

#[test]
fn test_jinja_templater() {
let source = r"
[sqruff]
templater = jinja
";
let config = FluffConfig::from_source(source);
let templater = JinjaTemplater;

let processed = templater
.process(JINJA_STRING, "test.sql", Some(&config), None)
.unwrap();

assert_eq!(
processed.to_string(),
"SELECT * FROM f, o, o WHERE a < 10\n\n"
)
}
}
Loading