Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #32 from kluctl/fix-local-vars
Browse files Browse the repository at this point in the history
fix: Also take local vars into account when using render or get_var
  • Loading branch information
codablock authored Mar 1, 2024
2 parents ad6f239 + 3c2dea4 commit bc1b3c5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
48 changes: 48 additions & 0 deletions jinja2_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,51 @@ func TestSha256PrefixLength(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "9f86d0", s)
}

func TestGetVarAndRender(t *testing.T) {
j2 := newJinja2(t, WithGlobals(map[string]any{
"g1": "v1",
"g2": "v2",
"g3": map[string]any{
"nested": "v3",
},
"list": []map[string]any{
{"x": "i1"},
{"x": "i2"},
},
}))

type testCase struct {
tmpl string
result string
}

tests := []testCase{
{tmpl: "{{ get_var('missing') }}", result: "None"},
{tmpl: "{{ get_var('missing', 'default') }}", result: "default"},
{tmpl: "{{ get_var(['missing']) }}", result: "None"},
{tmpl: "{{ get_var(['missing'], 'default') }}", result: "default"},
{tmpl: "{{ get_var(['missing1', 'missing2'], 'default') }}", result: "default"},
{tmpl: "{{ get_var(['g1', 'missing']) }}", result: "v1"},
{tmpl: "{{ get_var(['missing', 'g1']) }}", result: "v1"},
{tmpl: "{{ get_var(['g1', 'g2']) }}", result: "v1"},
{tmpl: "{{ get_var(['g2', 'g1']) }}", result: "v2"},
{tmpl: "{{ get_var('list') }}", result: `[{'x': 'i1'}, {'x': 'i2'}]`},
{tmpl: "{{ get_var('list[0].x') }}", result: "i1"},
{tmpl: "{% set loc = 'l1' %}{{ get_var('loc') }}", result: "l1"},
// TODO test for this when https://github.com/pallets/jinja/issues/1478 gets fixed
// {tmpl: "{% for e in list %}{{ get_var('e.x') }}{% endfor %}", result: ""},
{tmpl: "{% set loc = 'l1' %}{{ '{{ loc }}' | render }}", result: "l1"},
// TODO test for this when https://github.com/pallets/jinja/issues/1478 gets fixed
// {tmpl: "{% for e in list %}{{ '{{ e }}' | render }}{% endfor %}", result: ""},
}

for i, tc := range tests {
tc := tc
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
s, err := j2.RenderString(tc.tmpl)
assert.NoError(t, err)
assert.Equal(t, tc.result, s)
})
}
}
8 changes: 5 additions & 3 deletions python_src/go_jinja2/ext/kluctl_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def from_yaml(s):
@jinja2.pass_context
def render(ctx, string):
t = ctx.environment.from_string(string)
return t.render(ctx.parent)
return t.render(ctx.get_all())


def sha256(s, digest_len=None):
Expand All @@ -63,7 +63,7 @@ def slugify(s, allow_unicode=False):
def load_template(ctx, path, **kwargs):
ctx.environment.print_debug("load_template(%s)" % path)
t = ctx.environment.get_template(path.replace(os.path.sep, '/'), parent=ctx.name)
vars = merge_dict(ctx.parent, kwargs)
vars = merge_dict(ctx.get_all(), kwargs)
return t.render(vars)


Expand All @@ -75,8 +75,10 @@ class VarNotFoundException(Exception):
def get_var(ctx, path, default=None):
if not isinstance(path, list):
path = [path]

all_vars = ctx.get_all()
for p in path:
r = get_dict_value(ctx.parent, p, VarNotFoundException())
r = get_dict_value(all_vars, p, VarNotFoundException())
if isinstance(r, VarNotFoundException):
continue
return r
Expand Down

0 comments on commit bc1b3c5

Please sign in to comment.