hairyhenderson/gomplate

Question / Feature request - `coll.Omit` and lists/slices

philippeboyd opened this issue · 2 comments

I understand that coll.Omit only works on maps, but why can't it be used on lists as well? Say I have a list of unknown values but no matter what, I don't want the value "XX" or 3 in it.

gomplate -i '{{ $data := slice "foo" "bar" "baz" }}
{{ coll.Omit "foo" $data }}'

Workaround, I can range the slice and have a condition not (eq ...). Such a utility function like coll.Omit would be nice for lists.

Hi @philippeboyd,

coll.Omit is intended to remove entries with a given key, and as such makes no sense for lists, since lists have no keys, only values.

Whenever I've had this kind of situation, it's always been in the context of a range, where I want to skip a given value. One way to handle this could be to use continue:

$ gomplate -i '{{ $data := coll.Slice "foo" "bar" "baz" -}}
{{ range $data -}}
{{ if eq "foo" . }}{{continue}}{{end -}}
The word of the day is {{ . }}!
{{ end }}'
The word of the day is bar!
The word of the day is baz!

I suppose things get more complex if you need to feed that altered list to another template or function, though, so I can see a "without" function being useful for lists.

Hi @hairyhenderson,

Totally makes sense that coll.Omit only work with map then. And for without, I guess something of the following could be elegent? But then again, maybe overkill? I can for sure stay with my workaround for the time being.

{{ $data := coll.Slice "foo" "bar" "baz" -}}
{{ range ($data | coll.Without "bar") -}}
...
{{ end }}