-
Hi everyone, I’m currently struggling with accessing values from nested maps in a Taskfile. Maybe someone can help me figure out what I’m doing wrong. Taskfile
Output
So far, so good — I can access the first-level map. The Problem
This fails with: Access with get
This also fails with... Question Why am I able to echo the nested map, but not access a specific key (like env) from it? Any insights would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @falconTwo. Let's start with some debugging. It's often useful to use the cmds:
- - cmd: 'echo {{.BUILD_PROFILES}}'
- - cmd: 'echo {{.profile_config}}'
+ - cmd: echo "{{spew .BUILD_PROFILES}}"
+ - cmd: echo "{{spew .profile_config}}" The first line outputs your map variable as expected:
However, you might be surprised that the second output is actually a string instead of a map:
This isn't obvious because the output contains the word "map". However, the output of a template is always a string in Task. This is a limitation of the underlying templating engine from Golang. If the type being output is not a string, then it will be "stringified". That's what you're seeing here. This scenario is exactly why we forked the Golang templating library and created the vars:
- profile: '{{.profile | default "default"}}'
- profile_config: '{{index .BUILD_PROFILES .profile}}'
+ profile: '{{.profile | default "default"}}'
+ profile_config:
+ ref: "index .BUILD_PROFILES .profile" This time, the second line will output your map type as intended:
It's also worth noting that you can access map fields using dot notation ( Hope that helps. Feel free to ask any follow-up questions. |
Beta Was this translation helpful? Give feedback.
Hey @falconTwo. Let's start with some debugging. It's often useful to use the
spew
function when trying to resolve issues like this as it gives a bit more information about what's going on:The first line outputs your map variable as expected: