Skip to content

Commit

Permalink
✨ replace helper assign by set
Browse files Browse the repository at this point in the history
- fix issue on multi call
- allow multiple assignement by call

FIX #65
  • Loading branch information
davidB committed Jun 1, 2024
1 parent 4bbe777 commit 5bbef3b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 22 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jsonnet = ["dep:jsonnet-rs"]
- [Helpers](#helpers)
- [Blocks](#blocks)
- [Edition via jsonnet](#edition-via-jsonnet)
- [Assign](#assign)
- [Assign, set](#assign-set)
- [Replace section](#replace-section)

<!-- /TOC -->
Expand Down Expand Up @@ -329,17 +329,23 @@ v {
</tr>
</table>

## Assign
## Assign, set

Helper able to assign (to set) a variable to use later in the template.

| usage | output |
| ------------------------------------------- | ------------- |
| `{{ assign "foo" "{}" }}` | `` |
| `{{ assign "foo" "{}" }}{{ foo }}` | `{}` |
| `{{ assign "foo" "hello world" }}{{ foo }}` | `hello world` |
| `{{ assign "foo" {} }}{{ foo }}` | `[object]` |
| `{{ assign "foo" {"bar": 33} }}{{ foo }}` | `[object]` |
⚠️ `assign` is deprecated replaced by `set` (more compact and allow multi assignment in one call)

| usage | output |
| ----------------------------------------------------------------- | --------------- |
| `{{ assign "foo" "hello world" }}{{ foo }}` | `hello world` |
| `{{ set foo="{}" }}` | `` |
| `{{ set foo="{}" }}{{ foo }}` | `{}` |
| `{{ set foo="hello world" }}{{ foo }}` | `hello world` |
| `{{ set foo={} }}{{ foo }}` | `[object]` |
| `{{ set foo={"bar": 33} }}{{ foo }}` | `[object]` |
| `{{ set foo={"bar": 33} }}{{ foo.bar }}` | `33` |
| `{{ set foo="world" bar="hello" }}>{{ bar }} {{ foo }}<` | `>hello world<` |
| `{{ set foo="world" }}{{ set bar="hello" }}>{{ bar }} {{ foo }}<` | `>hello world<` |

## Replace section

Expand Down
66 changes: 53 additions & 13 deletions src/assign_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use handlebars::Context;
use handlebars::Handlebars;
use handlebars::Helper;
use handlebars::HelperResult;
use handlebars::Output;
use handlebars::RenderContext;
use handlebars::RenderErrorReason;
use handlebars::{
Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderErrorReason,
};

fn assign_fct(
h: &Helper,
Expand All @@ -23,17 +19,34 @@ fn assign_fct(
.map(|v| v.value())
.cloned()
.ok_or(RenderErrorReason::ParamNotFoundForIndex("assign", 1))?;
let mut ctx = ctx.clone();
match ctx.data_mut() {
serde_json::value::Value::Object(m) => m.insert(name.to_owned(), value),
_ => None,
};
let mut ctx = rc.context().as_deref().unwrap_or(ctx).clone();
if let Some(ref mut m) = ctx.data_mut().as_object_mut() {
m.insert(name.to_owned(), value);
}
rc.set_context(ctx);
Ok(())
}

fn set_fct(
h: &Helper,
_: &Handlebars,
ctx: &Context,
rc: &mut RenderContext,
_: &mut dyn Output,
) -> HelperResult {
let mut ctx = rc.context().as_deref().unwrap_or(ctx).clone();
if let Some(ref mut m) = ctx.data_mut().as_object_mut() {
for (k, v) in h.hash() {
m.insert(k.to_string(), v.value().clone());
}
}
rc.set_context(ctx);
Ok(())
}

pub fn register(handlebars: &mut Handlebars) {
handlebars.register_helper("assign", Box::new(assign_fct))
handlebars.register_helper("assign", Box::new(assign_fct));
handlebars.register_helper("set", Box::new(set_fct));
}

#[cfg(test)]
Expand All @@ -54,6 +67,33 @@ mod tests {
(
r##"{{ assign "foo" "hello world" }}{{ foo }}"##,
r##"hello world"##,
),
(
r##"{{ assign "foo" "world" }}{{ assign "bar" "hello" }}>{{ bar }} {{ foo }}<"##,
r##">hello world<"##,
)
]
}

#[test]
fn test_helper_set() -> Result<(), Box<dyn Error>> {
assert_renders![
(r##"{{ set foo="{}" }}"##, r##""##),
(r##"{{ set foo="{}" }}{{ foo }}"##, r##"{}"##),
(r##"{{ set foo={} }}{{ foo }}"##, r##"[object]"##),
(r##"{{ set foo={"bar": 33} }}{{ foo }}"##, r##"[object]"##,),
(r##"{{ set foo={"bar": 33} }}{{ foo.bar }}"##, r##"33"##,),
(
r##"{{ set foo="hello world" }}{{ foo }}"##,
r##"hello world"##,
),
(
r##"{{ set foo="world" bar="hello" }}>{{ bar }} {{ foo }}<"##,
r##">hello world<"##,
),
(
r##"{{ set foo="world" }}{{ set bar="hello" }}>{{ bar }} {{ foo }}<"##,
r##">hello world<"##,
)
]
}
Expand Down

0 comments on commit 5bbef3b

Please sign in to comment.