Skip to content

Commit 97e2e48

Browse files
committed
fix(template functions): Use function Title instead of ToTitle to get Capitalize strings instead of upper strings
using cases.Title because the strings.Title function is deprecated and refs to cases.Title
1 parent fac6290 commit 97e2e48

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ require (
1111
github.com/mattn/go-colorable v0.1.12
1212
github.com/mitchellh/cli v1.1.4
1313
github.com/russross/blackfriday v1.6.0
14+
github.com/stretchr/testify v1.7.0
1415
github.com/zclconf/go-cty v1.10.0
16+
golang.org/x/text v0.3.7
1517
)
1618

1719
require (
@@ -20,6 +22,7 @@ require (
2022
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
2123
github.com/armon/go-radix v1.0.0 // indirect
2224
github.com/bgentry/speakeasy v0.1.0 // indirect
25+
github.com/davecgh/go-spew v1.1.1 // indirect
2326
github.com/fatih/color v1.13.0 // indirect
2427
github.com/google/uuid v1.3.0 // indirect
2528
github.com/hashicorp/errwrap v1.1.0 // indirect
@@ -32,10 +35,11 @@ require (
3235
github.com/mattn/go-isatty v0.0.14 // indirect
3336
github.com/mitchellh/copystructure v1.2.0 // indirect
3437
github.com/mitchellh/reflectwalk v1.0.2 // indirect
38+
github.com/pmezard/go-difflib v1.0.0 // indirect
3539
github.com/posener/complete v1.2.3 // indirect
3640
github.com/shopspring/decimal v1.3.1 // indirect
3741
github.com/spf13/cast v1.5.0 // indirect
3842
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
3943
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
40-
golang.org/x/text v0.3.7 // indirect
44+
gopkg.in/yaml.v3 v3.0.0 // indirect
4145
)

go.sum

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
167167
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
168168
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
169169
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
170-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
171-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
172170
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
173171
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
174172
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -196,8 +194,6 @@ golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7w
196194
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
197195
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
198196
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
199-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
200-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
201197
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8=
202198
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
203199
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
@@ -216,6 +212,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
216212
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
217213
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
218214
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
215+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
219216
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
220217
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
221218
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=

internal/provider/template.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"strings"
88
"text/template"
99

10+
"golang.org/x/text/cases"
11+
"golang.org/x/text/language"
12+
1013
tfjson "github.com/hashicorp/terraform-json"
1114

1215
"github.com/hashicorp/terraform-plugin-docs/internal/mdplain"
@@ -31,6 +34,7 @@ type (
3134

3235
func newTemplate(name, text string) (*template.Template, error) {
3336
tmpl := template.New(name)
37+
titleCaser := cases.Title(language.Und)
3438

3539
tmpl.Funcs(map[string]interface{}{
3640
"codefile": tmplfuncs.CodeFile,
@@ -39,7 +43,7 @@ func newTemplate(name, text string) (*template.Template, error) {
3943
"prefixlines": tmplfuncs.PrefixLines,
4044
"split": strings.Split,
4145
"tffile": terraformCodeFile,
42-
"title": strings.ToTitle,
46+
"title": titleCaser.String,
4347
"trimspace": strings.TrimSpace,
4448
"upper": strings.ToUpper,
4549
})

internal/provider/template_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestRenderStringTemplate(t *testing.T) {
10+
template := `
11+
Plainmarkdown: {{ plainmarkdown .Text }}
12+
Split: {{ $arr := split .Text " "}}{{ index $arr 3 }}
13+
Trimspace: {{ trimspace .Text }}
14+
Lower: {{ upper .Text }}
15+
Upper: {{ lower .Text }}
16+
Title: {{ title .Text }}
17+
Prefixlines:
18+
{{ prefixlines " " .MultiLineTest }}
19+
`
20+
21+
expectedString := `
22+
Plainmarkdown: my Odly cAsed striNg
23+
Split: striNg
24+
Trimspace: my Odly cAsed striNg
25+
Lower: MY ODLY CASED STRING
26+
Upper: my odly cased string
27+
Title: My Odly Cased String
28+
Prefixlines:
29+
This text used
30+
multiple lines
31+
`
32+
result, err := renderStringTemplate("testTemplate", template, struct {
33+
Text string
34+
MultiLineTest string
35+
}{
36+
Text: "my Odly cAsed striNg",
37+
MultiLineTest: `This text used
38+
multiple lines`,
39+
})
40+
41+
assert.Nil(t, err)
42+
assert.Equal(t, expectedString, result)
43+
}

0 commit comments

Comments
 (0)