Skip to content

Commit 78dfb78

Browse files
anmoeldetro
authored andcommitted
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 0aa71ca commit 78dfb78

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

CHANGELOG.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
# 0.13.0 (TBD)
1+
# 0.13.0 (Unreleased)
22

33
ENHANCEMENTS:
44

55
* Group nested attributes by optional, required and read-only ([#163](https://github.com/hashicorp/terraform-plugin-docs/pull/163)).
66

7+
BUG FIXES:
8+
9+
* template functions: funtion `title` creates capitalize strings instead of upper strings ([#165](https://github.com/hashicorp/terraform-plugin-docs/pull/165)).
10+
711
# 0.12.0 (June 29, 2022)
812

913
BUG FIXES:
@@ -77,13 +81,13 @@ BUG FIXES:
7781

7882
ENHANCEMENTS:
7983

80-
* cmd/tfplugindocs: Use existing Terraform CLI binary if available on PATH, otherwise download latest Terraform CLI binary (https://github.com/hashicorp/terraform-plugin-docs/pull/124).
81-
* cmd/tfplugindocs: Added `tf-version` flag for specifying Terraform CLI binary version to download, superseding the PATH lookup (https://github.com/hashicorp/terraform-plugin-docs/pull/124).
84+
* cmd/tfplugindocs: Use existing Terraform CLI binary if available on PATH, otherwise download latest Terraform CLI binary ([#124](https://github.com/hashicorp/terraform-plugin-docs/pull/124)).
85+
* cmd/tfplugindocs: Added `tf-version` flag for specifying Terraform CLI binary version to download, superseding the PATH lookup ([#124](https://github.com/hashicorp/terraform-plugin-docs/pull/124)).
8286

8387
BUG FIXES:
8488

85-
* cmd/tfplugindocs: Swapped `.Type` and `.Name` resource and data source template fields so they correctly align (https://github.com/hashicorp/terraform-plugin-docs/pull/44).
86-
* schemamd: Switched attribute name rendering from bold text to code blocks so the Terraform Registry treats them as anchor links (https://github.com/hashicorp/terraform-plugin-docs/pull/59).
89+
* cmd/tfplugindocs: Swapped `.Type` and `.Name` resource and data source template fields so they correctly align ([#44](https://github.com/hashicorp/terraform-plugin-docs/pull/44)).
90+
* schemamd: Switched attribute name rendering from bold text to code blocks so the Terraform Registry treats them as anchor links ([#59](https://github.com/hashicorp/terraform-plugin-docs/pull/59)).
8791

8892
# 0.6.0 (March 14, 2022)
8993

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ using the following data fields and functions:
168168
| `plainmarkdown` | Render Markdown content as plaintext. |
169169
| `prefixlines` | Add a prefix to all (newline-separated) lines in a string. |
170170
| `split` | Split string into sub-strings, by a given separator (ex. `split .Name "_"`). |
171-
| `title` | Equivalent to [`strings.ToLower`](https://pkg.go.dev/strings#ToTitle). |
171+
| `title` | Equivalent to [`cases.Title`](https://pkg.go.dev/golang.org/x/text/cases#Title). |
172172
| `tffile` | A special case of the `codefile` function, designed for Terraform files (i.e. `.tf`). |
173173
| `trimspace` | Equivalent to [`strings.TrimSpace`](https://pkg.go.dev/strings#TrimSpace). |
174-
| `upper` | Equivalent to [`strings.ToLower`](https://pkg.go.dev/strings#ToUpper). |
174+
| `upper` | Equivalent to [`strings.ToUpper`](https://pkg.go.dev/strings#ToUpper). |
175175

176176
## Disclaimer
177177

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/mitchellh/cli v1.1.4
1313
github.com/russross/blackfriday v1.6.0
1414
github.com/zclconf/go-cty v1.10.0
15+
golang.org/x/text v0.3.7
1516
)
1617

1718
require (
@@ -37,5 +38,4 @@ require (
3738
github.com/spf13/cast v1.5.0 // indirect
3839
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
3940
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
40-
golang.org/x/text v0.3.7 // indirect
4141
)

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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
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+
if err != nil {
42+
t.Error(err)
43+
}
44+
if !cmp.Equal(expectedString, result) {
45+
t.Errorf("expected: %+v, got: %+v", expectedString, result)
46+
}
47+
}

0 commit comments

Comments
 (0)