sprig
sprig copied to clipboard
Feature Request: version of pluck that can take a list-of-maps
The current implementation of the pluck
function takes two-or-more arguments and expects the key to be plucked as the first argument and the maps to pluck from as the remaining arguments. A variant of this would be a function that takes exactly two arguments, one the key to be plucked and the second a list of maps to pluck from. This would support use cases like a helm "values" file of the form
items:
- name: foo
colour: red
- name: bar
colour: green
and a template that wants to extract a list containing the colour of each item. Currently
{{ .items | pluck "colour" | join ", " }}
fails with an error
wrong type for value; expected map[string]interface {}; got []interface {}
{{ pluck "colour" (index .items 0) (index .items 1) | ... }}
works of course, but that requires you to know the number of items up front.
I just spent the last hour trying to find a way to do this, only to resign to making a feature request and using the following work-around:
{{- $things := list -}}
{{- range $item := .Values.global.items -}}
{{- $things = append $things $item.thing -}}
{{- end -}}
{{- $things | uniq | join "," -}}
{{- end }}
Lo-and-behold, you've already made the request for the exact same reason, Helm values files.