sprig icon indicating copy to clipboard operation
sprig copied to clipboard

Feature Request: version of pluck that can take a list-of-maps

Open ianroberts opened this issue 3 years ago • 1 comments

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.

ianroberts avatar Jun 06 '21 16:06 ianroberts

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.

tculp avatar Jul 13 '21 20:07 tculp