Skip to content

Commit f366543

Browse files
committed
Add default arguments for the template functions
{{.Name}}{{"\n"}}{{.Message | missing | indent}} Signed-off-by: Anders F Björklund <[email protected]>
1 parent fc1384b commit f366543

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

pkg/textutil/textutil.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package textutil
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"strings"
89
"text/template"
@@ -74,8 +75,46 @@ var TemplateFuncMap = template.FuncMap{
7475
}
7576
return "---\n" + strings.TrimSuffix(b.String(), "\n")
7677
},
77-
"indent": IndentString,
78-
"missing": MissingString,
78+
"indent": func(a ...interface{}) (string, error) {
79+
if len(a) == 0 {
80+
return "", errors.New("function takes at at least one string argument")
81+
}
82+
if len(a) > 2 {
83+
return "", errors.New("function takes at most 2 arguments")
84+
}
85+
var ok bool
86+
size := 2
87+
if len(a) > 1 {
88+
if size, ok = a[0].(int); !ok {
89+
return "", errors.New("optional first argument must be an integer")
90+
}
91+
}
92+
text := ""
93+
if text, ok = a[len(a)-1].(string); !ok {
94+
return "", errors.New("last argument must be a string")
95+
}
96+
return IndentString(size, text), nil
97+
},
98+
"missing": func(a ...interface{}) (string, error) {
99+
if len(a) == 0 {
100+
return "", errors.New("function takes at at least one string argument")
101+
}
102+
if len(a) > 2 {
103+
return "", errors.New("function takes at most 2 arguments")
104+
}
105+
var ok bool
106+
message := "<missing>"
107+
if len(a) > 1 {
108+
if message, ok = a[0].(string); !ok {
109+
return "", errors.New("optional first argument must be a string")
110+
}
111+
}
112+
text := ""
113+
if text, ok = a[len(a)-1].(string); !ok {
114+
return "", errors.New("last argument must be a string")
115+
}
116+
return MissingString(message, text), nil
117+
},
79118
}
80119

81120
// TemplateFuncHelp is help for TemplateFuncMap.

pkg/textutil/textutil_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ func TestTemplateFuncs(t *testing.T) {
3232
Foo int `json:"foo" yaml:"foo"`
3333
Bar string `json:"bar" yaml:"bar"`
3434
Message string `json:"message,omitempty" yaml:"message,omitempty"`
35+
Missing string `json:"missing,omitempty" yaml:"missing,omitempty"`
3536
}
36-
x := X{Foo: 42, Bar: "hello", Message: "One\nTwo\nThree"}
37+
x := X{Foo: 42, Bar: "hello", Message: "One\nTwo\nThree", Missing: ""}
3738

3839
testCases := map[string]string{
3940
"{{json .}}": `{"foo":42,"bar":"hello","message":"One\nTwo\nThree"}`,
@@ -45,6 +46,8 @@ message: |-
4546
Two
4647
Three`,
4748
`{{.Bar}}{{"\n"}}{{.Message | missing "<no message>" | indent 2}}`: "hello\n One\n Two\n Three",
49+
`{{.Message | indent}}`: " One\n Two\n Three",
50+
`{{.Missing | missing}}`: "<missing>",
4851
}
4952

5053
for format, expected := range testCases {

0 commit comments

Comments
 (0)