Skip to content

Commit 6fa0f54

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 6fa0f54

File tree

6 files changed

+67
-14
lines changed

6 files changed

+67
-14
lines changed

CHANGELOG.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.12.1 (July 04, 2022)
2+
3+
BUG FIXES:
4+
5+
* template functions: funtion `title` creates capitalize strings instead of upper strings ([#165](https://github.com/hashicorp/terraform-plugin-docs/pull/165)).
6+
17
# 0.12.0 (June 29, 2022)
28

39
BUG FIXES:
@@ -71,13 +77,13 @@ BUG FIXES:
7177

7278
ENHANCEMENTS:
7379

74-
* 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).
75-
* 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).
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>).
7682

7783
BUG FIXES:
7884

79-
* 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).
80-
* 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).
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>).
8187

8288
# 0.6.0 (March 14, 2022)
8389

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Available commands are:
2929
the generate command is run by default
3030
generate generates a plugin website from code, templates, and examples for the current directory
3131
validate validates a plugin website for the current directory
32-
32+
3333
```
3434

3535
`generate` command:
@@ -93,7 +93,7 @@ Otherwise, the provider developer can set an arbitrary description like this:
9393
Type: schema.TypeString,
9494
Computed: true,
9595
Description: "Unique identifier for this resource",
96-
},
96+
},
9797
// ...
9898
}
9999
// ...
@@ -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
)

go.sum

Lines changed: 0 additions & 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=

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)