feat: can traverse stuff

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-20 23:21:27 +02:00
parent bbe630e822
commit 06ad4515c2
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
4 changed files with 48 additions and 4 deletions

View File

@ -146,8 +146,14 @@ impl minijinja::value::Object for CuddleVariables {
if let Some(val) = self.0.get(key) { if let Some(val) = self.0.get(key) {
match val { match val {
CuddleVariable::Object(_) => todo!(), CuddleVariable::Object(object) => {
CuddleVariable::Array(_) => todo!(), let obj = object.deref().clone();
return Some(minijinja::Value::from_object(obj));
}
CuddleVariable::Array(arr) => {
return Some(minijinja::Value::from_object(MiniJinjaList(arr.clone())))
}
CuddleVariable::String(str) => { CuddleVariable::String(str) => {
return Some(minijinja::Value::from_safe_string(str.to_owned())) return Some(minijinja::Value::from_safe_string(str.to_owned()))
} }
@ -158,3 +164,29 @@ impl minijinja::value::Object for CuddleVariables {
None None
} }
} }
#[derive(Debug)]
pub struct MiniJinjaList(Vec<CuddleVariable>);
impl minijinja::value::Object for MiniJinjaList {
fn enumerate(self: &std::sync::Arc<Self>) -> minijinja::value::Enumerator {
minijinja::value::Enumerator::Values(
self.0
.iter()
.map(|i| match i {
CuddleVariable::Object(object) => {
let obj = object.deref().clone();
minijinja::Value::from_object(obj)
}
CuddleVariable::Array(arr) => {
minijinja::Value::from_object(MiniJinjaList(arr.clone()))
}
CuddleVariable::String(str) => {
minijinja::Value::from_safe_string(str.to_owned())
}
})
.collect(),
)
}
}

View File

@ -1,5 +1,11 @@
vars: vars:
service: service service: service
some:
nested:
item: something
array:
- item: item
cuddle/clusters: cuddle/clusters:
dev: dev:

View File

@ -1,2 +1,4 @@
service: service service: service
something: something
items:
- item

View File

@ -1,3 +1,7 @@
service: {{ vars.cuddle_vars.service }} service: {{ vars.cuddle_vars.service }}
something: {{ vars.cuddle_vars.some.nested.item }}
items:
{%- for val in vars.cuddle_vars.array %}
- {{val.item}}
{%- endfor %}