-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(native): Jinja - support fields reflection for PyObject (#7312)
- Loading branch information
Showing
12 changed files
with
117 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod entry; | ||
mod mj_value; | ||
mod neon; | ||
|
||
pub use entry::template_register_module; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use minijinja as mj; | ||
use std::error::Error; | ||
|
||
use neon::prelude::*; | ||
|
||
pub(crate) trait NeonMiniJinjaContext<'a>: Context<'a> { | ||
fn throw_from_mj_error<T>(&mut self, err: mj::Error) -> NeonResult<T> { | ||
let codeblock = if let Some(source) = err.template_source() { | ||
let lines: Vec<_> = source.lines().enumerate().collect(); | ||
let idx = err.line().unwrap_or(1).saturating_sub(1); | ||
let skip = idx.saturating_sub(3); | ||
|
||
let pre = lines.iter().skip(skip).take(3.min(idx)).collect::<Vec<_>>(); | ||
let post = lines.iter().skip(idx + 1).take(3).collect::<Vec<_>>(); | ||
|
||
let mut content = "".to_string(); | ||
|
||
for (idx, line) in pre { | ||
content += &format!("{:>4} | {}\r\n", idx + 1, line); | ||
} | ||
|
||
content += &format!("{:>4} > {}\r\n", idx + 1, lines[idx].1); | ||
|
||
if let Some(_span) = err.range() { | ||
// TODO(ovr): improve | ||
content += &format!( | ||
" i {}{} {}\r\n", | ||
" ".repeat(0), | ||
"^".repeat(24), | ||
err.kind(), | ||
); | ||
} else { | ||
content += &format!(" | {}\r\n", "^".repeat(24)); | ||
} | ||
|
||
for (idx, line) in post { | ||
content += &format!("{:>4} | {}\r\n", idx + 1, line); | ||
} | ||
|
||
format!("{}\r\n{}\r\n{}", "-".repeat(79), content, "-".repeat(79)) | ||
} else { | ||
"".to_string() | ||
}; | ||
|
||
if let Some(next_err) = err.source() { | ||
self.throw_error(format!( | ||
"{} caused by: {:#}\r\n{}", | ||
err, next_err, codeblock | ||
)) | ||
} else { | ||
self.throw_error(format!("{}\r\n{}", err, codeblock)) | ||
} | ||
} | ||
} | ||
|
||
impl<'a> NeonMiniJinjaContext<'a> for FunctionContext<'a> {} | ||
|
||
impl<'a> NeonMiniJinjaContext<'a> for TaskContext<'a> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/cubejs-backend-native/test/templates/dump_context.yml.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{# TODO: We need stable sort for dump #} | ||
<pre>{# debug() #}</pre> | ||
|
||
print: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
{%- set unsafe_string = '"unsafe string" <>' -%} | ||
{%- set safe_string = new_safe_string() -%} | ||
{%- set dict_as_obj = new_object_from_dict() -%} | ||
|
||
test: | ||
unsafe_string: "{{ unsafe_string }}" | ||
safe_string: "{{ safe_string }}" | ||
|
||
dump: | ||
dict_as_obj: {{ debug(dict_as_obj) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters