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 committed Jul 4, 2022
1 parent fac6290 commit 6fa0f54
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
14 changes: 10 additions & 4 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
# 0.12.1 (July 04, 2022)

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 @@ -71,13 +77,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 (<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>).

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

# 0.6.0 (March 14, 2022)

Expand Down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -29,7 +29,7 @@ Available commands are:
the generate command is run by default
generate generates a plugin website from code, templates, and examples for the current directory
validate validates a plugin website for the current directory

```

`generate` command:
Expand Down Expand Up @@ -93,7 +93,7 @@ Otherwise, the provider developer can set an arbitrary description like this:
Type: schema.TypeString,
Computed: true,
Description: "Unique identifier for this resource",
},
},
// ...
}
// ...
Expand Down 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
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
)
4 changes: 0 additions & 4 deletions go.sum
Expand Up @@ -167,8 +167,6 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -196,8 +194,6 @@ golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8=
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
6 changes: 5 additions & 1 deletion internal/provider/template.go
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
@@ -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 6fa0f54

Please sign in to comment.