Accesing dictionary option not working as expected
freeseacher opened this issue · comments
hi, i have such a template
{% set shard = printf("shard_%s", getenv("SHARD", 1)) %}
{% set config = getv("/shard_config") | parseYAML %}
{{ config.shard_1.machine }}
{% set shard_config = config.shard %}
{{ shard_config }}
And such key in consul
shard_1:
machine: n1-standard-1
shard_2:
machine: n1-standard-1
the problem is in last two lines of template, i'm expecting that will get data from {{ config.shard_1.machine }}
but got empty string instead.
How are you setting the values in confd?
If i set it like this:
curl -X PUT http://172.17.0.2:8500/v1/kv/shard_config -d 'shard_1: :(
machine: n1-standard-1
shard_2:
machine: n1-standard-1'
and use this template:
{% set shard = printf("shard_%s", getenv("SHARD", "1")) %}
{% set config = getv("/shard_config") | parseYAML %}
{{ config.shard_1.machine }}
{% set shard_config = config.shard_2.machine %}
{{ shard_config }}
i get this result:
n1-standard-1
n1-standard-1
here {% set shard_config = config.shard_2.machine %}
you are using hardcoded shard_2
but in template
i want to use shard
variable that i get previously instead.
You can't access map keys like this config.shard
because the map has no value "shard".
You would need a template filter in this case.
The simplest filter could be:
cat getMapValue.js
In[Param];
and the template:
{% set shard = printf("shard_%s", getenv("SHARD", "1")) %}
{% set config = getv("/shard_config") | parseYAML %}
{{ config.shard_1.machine }}
{% set shard_config = config | getMapValue:shard %}
{{ shard_config.machine }}
oh. i see.
How that should be assembled? am i have to put getMapValue.js
to templates directory ? have i load it in template ?
just create the file getMapValue.js wherever you want and set the filter directory in your config.
Here is my config
cat /etc/remco/config
log_level = "debug"
log_format = "text"
filter_dir = "/etc/remco/filter.d"
include_dir = "/etc/rputemco/resource.d"
and /etc/remco/filter.d is my filter_dir with all my js filters.
rkaufmann rkaufmann etc remco filter.d % ls
getMapValue.js jsrev.js toEnv.js
rkaufmann rkaufmann etc remco filter.d % cat getMapValue.js
In[Param];
thx. looks like it working!
But i have few questions :)
why it is called js ?
what should i read about writing new filters ?
seems links in pongo2 dead flosch/pongo2#180
and, don't you think that filter should be build in ?
why it is called js ?
because they are really javascript files :)
I use https://github.com/dop251/goja as the pure go javascript engine.
Take a look at https://heavyhorst.github.io/remco/template/template-filters/ to get a first impression.
don't you think that filter should be build in
Yes I will add it.
I added a filter to the latest master:
{{ getv("/some_yaml_config") | parseYAML | mapValue:"key" }}