Skip to content
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
48 changes: 3 additions & 45 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, VecDeque};

use serde::Serialize;
use serde_json::value::{to_value, Map, Value as Json};
use serde_json::value::{to_value, Value as Json};

use crate::block::{BlockContext, BlockParamHolder};
use crate::error::{RenderError, RenderErrorReason};
Expand Down Expand Up @@ -141,19 +141,6 @@ fn get_in_block_params<'a>(
None
}

pub(crate) fn merge_json(base: &Json, addition: &HashMap<&str, &Json>) -> Json {
let mut base_map = match base {
Json::Object(ref m) => m.clone(),
_ => Map::new(),
};

for (k, v) in addition {
base_map.insert((*k).to_string(), (*v).clone());
}

Json::Object(base_map)
}

impl Context {
/// Create a context with null data
pub fn null() -> Context {
Expand Down Expand Up @@ -228,12 +215,12 @@ impl From<Json> for Context {
#[cfg(test)]
mod test {
use crate::block::{BlockContext, BlockParams};
use crate::context::{self, Context};
use crate::context::Context;
use crate::error::RenderError;
use crate::json::path::Path;
use crate::json::value::{self, ScopedJson};
use serde_json::value::Map;
use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;

fn navigate_from_root<'rc>(
ctx: &'rc Context,
Expand Down Expand Up @@ -330,35 +317,6 @@ mod test {
);
}

#[test]
fn test_merge_json() {
let map = json!({ "age": 4 });
let s = "hello".to_owned();
let mut hash = HashMap::new();
let v = value::to_json("h1");
hash.insert("tag", &v);

let ctx_a1 = Context::wraps(context::merge_json(&map, &hash)).unwrap();
assert_eq!(
navigate_from_root(&ctx_a1, "age").unwrap().render(),
"4".to_owned()
);
assert_eq!(
navigate_from_root(&ctx_a1, "tag").unwrap().render(),
"h1".to_owned()
);

let ctx_a2 = Context::wraps(context::merge_json(&value::to_json(s), &hash)).unwrap();
assert_eq!(
navigate_from_root(&ctx_a2, "this").unwrap().render(),
"[object]".to_owned()
);
assert_eq!(
navigate_from_root(&ctx_a2, "tag").unwrap().render(),
"h1".to_owned()
);
}

#[test]
fn test_key_name_with_this() {
let m = json!({
Expand Down
37 changes: 16 additions & 21 deletions src/partial.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use std::collections::HashMap;

use serde_json::value::Value as Json;

use crate::block::BlockContext;
use crate::context::{merge_json, Context};
use crate::context::Context;
use crate::error::RenderError;
use crate::json::path::Path;
use crate::output::Output;
use crate::registry::Registry;
use crate::render::{Decorator, Evaluable, RenderContext, Renderable};
use crate::template::Template;
use crate::RenderErrorReason;
use crate::{BlockParamHolder, RenderErrorReason};

pub(crate) const PARTIAL_BLOCK: &str = "@partial-block";

Expand Down Expand Up @@ -91,13 +86,6 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
}

if !d.hash().is_empty() {
// hash given, update base_value
let hash_ctx = d
.hash()
.iter()
.map(|(k, v)| (*k, v.value()))
.collect::<HashMap<&str, &Json>>();

// create block if we didn't (no param provided for partial expression)
if !block_created {
let block_inner = if let Some(block) = rc.block() {
Expand All @@ -112,14 +100,13 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
rc.push_block(block_inner);
}

// evaluate context within current block, this includes block
// context provided by partial expression parameter
let merged_context = merge_json(rc.evaluate2(ctx, &Path::current())?.as_json(), &hash_ctx);

// update the base value, there must be a block for this so it's
// also safe to unwrap.
if let Some(block) = rc.block_mut() {
block.set_base_value(merged_context);
// treat hash value as block params
for (k, v) in d.hash() {
block.set_block_param(k, BlockParamHolder::Value(v.value().clone()));
}
}
}

Expand Down Expand Up @@ -549,7 +536,7 @@ Name:{{name}}
.unwrap();

hb.register_template_string(
"t1",
"t2",
r#"{{~#*inline "displayName"~}}
Template:{{this}}
{{/inline}}
Expand All @@ -564,13 +551,21 @@ Name:{{name}}
"data": ["hudel", "test"]
});

assert_eq!(
r"Name:hudel
Template:aaaa
Name:test
Template:aaaa
",
hb.render("t1", &data).unwrap()
);
assert_eq!(
r"Name:hudel
Template:hudel
Name:test
Template:test
",
hb.render("t1", &data).unwrap()
hb.render("t2", &data).unwrap()
);
}

Expand Down
Loading