Skip to content

Commit

Permalink
Support maps/object returned by func.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Sep 30, 2018
1 parent f6858e9 commit 09fa970
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gtmpl"
version = "0.5.4"
version = "0.5.5"
authors = ["Florian Merz <flomerz@gmail.com>"]
description = "The Golang Templating Language for Rust"
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ seamless integration of Rust application into the world of devops tools around
Add the following dependency to your Cargo manifest…
```toml
[dependencies]
gtmpl = "0.5.4"
gtmpl = "0.5.5"
```

and look at the docs:
Expand Down Expand Up @@ -137,7 +137,7 @@ there might be some convenient additions:
Enable `gtmpl_dynamic_template` in your `Cargo.toml`:
```toml
[dependencies.gtmpl]
version = "0.5.2"
version = "0.5.5"
features = ["gtmpl_dynamic_template"]

```
Expand Down
29 changes: 25 additions & 4 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,11 @@ impl<'a, 'b, T: Write> State<'a, 'b, T> {
fin: &Option<Value>,
) -> Result<Value, String> {
let mut arg_vals = vec![];
for arg in &args[1..] {
let val = self.eval_arg(ctx, arg)?;
arg_vals.push(val);
if !args.is_empty() {
for arg in &args[1..] {
let val = self.eval_arg(ctx, arg)?;
arg_vals.push(val);
}
}
if let Some(ref f) = *fin {
arg_vals.push(f.clone());
Expand Down Expand Up @@ -276,6 +278,7 @@ impl<'a, 'b, T: Write> State<'a, 'b, T> {
Nodes::Variable(ref n) => self.eval_variable_node(n, &[], &None),
Nodes::Pipe(ref n) => self.eval_pipeline(ctx, n),
// Nodes::Identifier
Nodes::Identifier(ref n) => self.eval_function(ctx, n, &[], &None),
Nodes::Chain(ref n) => self.eval_chain_node(ctx, n, &[], &None),
Nodes::String(ref n) => Ok(n.value.clone()),
Nodes::Bool(ref n) => Ok(n.value.clone()),
Expand Down Expand Up @@ -567,7 +570,7 @@ mod tests_mocked {
}

#[test]
fn test_funtion_via_dot() {
fn test_function_via_dot() {
#[derive(Gtmpl)]
struct Foo {
foo: Func,
Expand Down Expand Up @@ -608,6 +611,24 @@ mod tests_mocked {
assert_eq!(String::from_utf8(w).unwrap(), "43");
}

#[test]
fn test_function_ret_map() {
fn map(_: &[Value]) -> Result<Value, String> {
let mut h = HashMap::new();
h.insert("field".to_owned(), 1);
Ok(h.into())
}

let data = Context::empty();
let mut w: Vec<u8> = vec![];
let mut t = Template::default();
t.add_func("map", map);
assert!(t.parse(r#"{{map.field}}"#).is_ok());
let out = t.execute(&mut w, &data);
assert!(out.is_ok());
assert_eq!(String::from_utf8(w).unwrap(), "1");
}

#[test]
fn test_dot_value() {
#[derive(Gtmpl, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ impl ChainNode {
}
}

pub fn add(&mut self, val: String) {
pub fn add(&mut self, val: &str) {
let val = val.trim_left_matches('.').to_owned();
self.field.push(val);
}
}
Expand All @@ -443,8 +444,7 @@ impl Display for ChainNode {
return Err(e);
}
for field in &self.field {
// fields are prefixed with .
if let Err(e) = write!(f, "{}", field) {
if let Err(e) = write!(f, ".{}", field) {
return Err(e);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,14 @@ impl Parser {
_ => {}
};
let mut chain = ChainNode::new(self.tree_id, next.pos, n);
chain.add(next.val);
chain.add(&next.val);
while self
.peek()
.map(|p| p.typ == ItemType::ItemField)
.unwrap_or(false)
{
let field = self.next().unwrap();
chain.add(field.val);
chain.add(&field.val);
}
let n = match typ {
NodeType::Field => Nodes::Field(FieldNode::new(
Expand Down

0 comments on commit 09fa970

Please sign in to comment.