Is this the best way to create a list (that can be empty) for use with templates?

VENV_TO_INSTALL_STR:
sh: |-
for pkg in "{{join "\" \"" .PYTHON_PACKAGES}}"; do
echo '{{.VENV_ALL_PACKAGES}}' | grep -q "{\"name\": \"$pkg\"" || echo "$pkg"
done
echo ""
VENV_TO_INSTALL:
ref: initial (splitList "\n" .VENV_TO_INSTALL_STR)
VENV_TO_INSTALL_STR:
sh: |-
for pkg in "{{join "\" \"" .PYTHON_PACKAGES}}"; do
echo '{{.VENV_ALL_PACKAGES}}' | grep -q "{\"name\": \"$pkg\"" || echo "$pkg"
done
echo ""
VENV_TO_INSTALL:
ref: initial (splitList "\n" .VENV_TO_INSTALL_STR)
I need to do echo "" and use initial because if the script in VENV_TO_INSTALL_STR returns nothing, splitList would create a list with one item of an empty string (this causes problems with range , for example). To work around this I use initial, which in this case would return an empty list, however, if the shell script prints some items, that would drop one of those, so I always add an empty echo to account for that case. Is there a better way of handling this? Also, is there any way to use if statements in ref: keys? Edit: I think this parses the stuff in ref: keys: https://github.com/go-task/task/blob/27056a9827ca36f44df72cbd89a98c77c101cb07/internal/templater/templater.go#L47
t, err := template.New("resolver").Funcs(templateFuncs).Parse(fmt.Sprintf("{{%s}}", ref))
t, err := template.New("resolver").Funcs(templateFuncs).Parse(fmt.Sprintf("{{%s}}", ref))
I tried tricking it by doing }}{{ [if statement stuff] }}{{ but I kept getting various errors, seems like it is expecting it to be only one {{ }} node? I also checked if they can be embedded in parenthesis, but that also didn't work out.
Solution:
You could use compact instead of initial. This would remove any empty items from your array. Alternatively, you can build a JSON string and call fromJson. There are probably other ways too. Regarding ref. By design, it only accepts a single ActionNode (i.e. One set of {{...}}) so if statements are not possible. ref is designed for you to be able to refer to other variables while maintaining their type, not for logic. It is actually a side-effect of the templating engine that functions are available for use in ref, but we felt this was acceptable/useful....
Jump to solution
1 Reply
Solution
pd93
pd933w ago
You could use compact instead of initial. This would remove any empty items from your array. Alternatively, you can build a JSON string and call fromJson. There are probably other ways too. Regarding ref. By design, it only accepts a single ActionNode (i.e. One set of {{...}}) so if statements are not possible. ref is designed for you to be able to refer to other variables while maintaining their type, not for logic. It is actually a side-effect of the templating engine that functions are available for use in ref, but we felt this was acceptable/useful.
Want results from more Discord servers?
Add your server