Skip to content

Commit

Permalink
fix(template functions): Use function Title instead of ToTitle to get…
Browse files Browse the repository at this point in the history
… Capitalize strings instead of upper strings

using cases.Title because the strings.Title function is deprecated and refs to cases.Title
  • Loading branch information
anmoel authored and detro committed Jul 8, 2022
1 parent 0aa71ca commit 78dfb78
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
14 changes: 9 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# 0.13.0 (TBD)
# 0.13.0 (Unreleased)

ENHANCEMENTS:

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

BUG FIXES:

* template functions: funtion `title` creates capitalize strings instead of upper strings ([#165](https://github.com/hashicorp/terraform-plugin-docs/pull/165)).

# 0.12.0 (June 29, 2022)

BUG FIXES:
Expand Down Expand Up @@ -77,13 +81,13 @@ BUG FIXES:

ENHANCEMENTS:

* 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).
* 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).
* 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)).
* 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)).

BUG FIXES:

* 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).
* 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).
* 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)).
* 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)).

# 0.6.0 (March 14, 2022)

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

## Disclaimer

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/mitchellh/cli v1.1.4
github.com/russross/blackfriday v1.6.0
github.com/zclconf/go-cty v1.10.0
golang.org/x/text v0.3.7
)

require (
Expand All @@ -37,5 +38,4 @@ require (
github.com/spf13/cast v1.5.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
golang.org/x/text v0.3.7 // indirect
)
6 changes: 5 additions & 1 deletion internal/provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"strings"
"text/template"

"golang.org/x/text/cases"
"golang.org/x/text/language"

tfjson "github.com/hashicorp/terraform-json"

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

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

tmpl.Funcs(map[string]interface{}{
"codefile": tmplfuncs.CodeFile,
Expand All @@ -39,7 +43,7 @@ func newTemplate(name, text string) (*template.Template, error) {
"prefixlines": tmplfuncs.PrefixLines,
"split": strings.Split,
"tffile": terraformCodeFile,
"title": strings.ToTitle,
"title": titleCaser.String,
"trimspace": strings.TrimSpace,
"upper": strings.ToUpper,
})
Expand Down
47 changes: 47 additions & 0 deletions internal/provider/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package provider

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestRenderStringTemplate(t *testing.T) {
template := `
Plainmarkdown: {{ plainmarkdown .Text }}
Split: {{ $arr := split .Text " "}}{{ index $arr 3 }}
Trimspace: {{ trimspace .Text }}
Lower: {{ upper .Text }}
Upper: {{ lower .Text }}
Title: {{ title .Text }}
Prefixlines:
{{ prefixlines " " .MultiLineTest }}
`

expectedString := `
Plainmarkdown: my Odly cAsed striNg
Split: striNg
Trimspace: my Odly cAsed striNg
Lower: MY ODLY CASED STRING
Upper: my odly cased string
Title: My Odly Cased String
Prefixlines:
This text used
multiple lines
`
result, err := renderStringTemplate("testTemplate", template, struct {
Text string
MultiLineTest string
}{
Text: "my Odly cAsed striNg",
MultiLineTest: `This text used
multiple lines`,
})

if err != nil {
t.Error(err)
}
if !cmp.Equal(expectedString, result) {
t.Errorf("expected: %+v, got: %+v", expectedString, result)
}
}

0 comments on commit 78dfb78

Please sign in to comment.