Skip to content

Commit 93ee625

Browse files
committed
fix: docker and sprig coalesce are not identical
1 parent 776e11f commit 93ee625

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
384384
- [Functions from Go](https://pkg.go.dev/text/template#hdr-Functions)
385385
- [Functions from Sprig v3](https://masterminds.github.io/sprig/), except for those that have the same name as one of the following functions.
386386
- _`closest $array $value`_: Returns the longest matching substring in `$array` that matches `$value`
387+
- _`coalesce ...`_: Returns the first non-nil argument.
387388
- _`comment $delimiter $string`_: Returns `$string` with each line prefixed by `$delimiter` (helpful for debugging combined with Sprig `toPrettyJson`: `{{ toPrettyJson $ | comment "#" }}`).
388389
- _`contains $map $key`_: Returns `true` if `$map` contains `$key`. Takes maps from `string` to any type.
389390
- _`dir $path`_: Returns an array of filenames in the specified `$path`.
@@ -417,6 +418,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
417418

418419
Sprig functions that have the same name as docker-gen function (but different behaviour) are made available with the `sprig` prefix:
419420

421+
- _`sprigCoalesce ...`_: Alias for Sprig's [`coalesce`](https://masterminds.github.io/sprig/defaults.html).
420422
- _`sprigContains $string $string`_: Alias for Sprig's [`contains`](https://masterminds.github.io/sprig/strings.html).
421423
- _`sprigDir $path`_: Alias for Sprig's [`dir`](https://masterminds.github.io/sprig/paths.html).
422424
- _`sprigReplace $old $new $string`_: Alias for Sprig's [`replace`](https://masterminds.github.io/sprig/strings.html).

internal/template/functions.go

+10
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ func dirList(path string) ([]string, error) {
103103
return names, nil
104104
}
105105

106+
// coalesce returns the first non nil argument
107+
func coalesce(input ...interface{}) interface{} {
108+
for _, v := range input {
109+
if v != nil {
110+
return v
111+
}
112+
}
113+
return nil
114+
}
115+
106116
// when returns the trueValue when the condition is true and the falseValue otherwise
107117
func when(condition bool, trueValue, falseValue interface{}) interface{} {
108118
if condition {

internal/template/functions_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,11 @@ func TestDirList(t *testing.T) {
236236
filesList, _ = dirList("/wrong/path")
237237
assert.Equal(t, []string{}, filesList)
238238
}
239+
240+
func TestCoalesce(t *testing.T) {
241+
v := coalesce(nil, "second", "third")
242+
assert.Equal(t, "second", v, "Expected second value")
243+
244+
v = coalesce(nil, nil, nil)
245+
assert.Nil(t, v, "Expected nil value")
246+
}

internal/template/template.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ func newTemplate(name string) *template.Template {
6060

6161
sprigFuncMap := sprig.TxtFuncMap()
6262

63-
return tmpl.Funcs(sprigFuncMap).Funcs(template.FuncMap{
63+
tmpl.Funcs(sprigFuncMap).Funcs(template.FuncMap{
6464
"closest": arrayClosest,
65+
"coalesce": coalesce,
6566
"comment": comment,
6667
"contains": contains,
6768
"dir": dirList,
@@ -108,12 +109,15 @@ func newTemplate(name string) *template.Template {
108109
"sha1": sprigFuncMap["sha1sum"],
109110

110111
// aliases to sprig template functions masked by docker-gen functions with the same name
112+
"sprigCoalesce": sprigFuncMap["coalesce"],
111113
"sprigContains": sprigFuncMap["contains"],
112114
"sprigDir": sprigFuncMap["dir"],
113115
"sprigReplace": sprigFuncMap["replace"],
114116
"sprigSplit": sprigFuncMap["split"],
115117
"sprigSplitn": sprigFuncMap["splitn"],
116118
})
119+
120+
return tmpl
117121
}
118122

119123
func isBlank(str string) bool {

0 commit comments

Comments
 (0)