From 6fb57b6ec9168892b64a37e429f35dd1e82de4dc Mon Sep 17 00:00:00 2001 From: Cameron Thornton Date: Mon, 4 Apr 2022 12:01:19 -0500 Subject: [PATCH] Fix sidebar placement of resources generated by tpgtools (#5890) --- tpgtools/names.go | 22 +++++++++++++++ tpgtools/resource.go | 28 +++++++++++-------- .../templates/resource.html.markdown.tmpl | 1 + 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/tpgtools/names.go b/tpgtools/names.go index 553eb896329c..0b267801ee17 100644 --- a/tpgtools/names.go +++ b/tpgtools/names.go @@ -22,6 +22,10 @@ type lowercaseName interface { lowercase() string } +type kebabCaseName interface { + kebabcase() string +} + // e.g. `google_compute_instance` or `google_orgpolicy_policy`. type SnakeCaseTerraformResourceName string @@ -74,6 +78,18 @@ func snakeToTitleCase(s snakeCaseName) miscellaneousNameTitleCase { return miscellaneousNameTitleCase(strings.Join(snakeToParts(s, true), "")) } +// e.g. `google-compute-instance` or `google-orgpolicy-policy`. +type KebabCaseTerraformResourceName string + +// snakeToKebabCase converts a snake_case string to kebab case. +func snakeToKebabCase(s snakeCaseName) miscellaneousNameKebabCase { + return miscellaneousNameKebabCase(strings.Join(snakeToParts(s, false), "-")) +} + +func (s SnakeCaseFullName) ToKebab() RenderedString { + return RenderedString(snakeToKebabCase(s).kebabcase()) +} + // A type for a string that is not meant for further conversion. Some functions return a // RenderedString to indicate that they have been lossily converted to another format. type RenderedString string @@ -143,3 +159,9 @@ type miscellaneousNameLowercase string func (m miscellaneousNameLowercase) lowercase() string { return string(m) } + +type miscellaneousNameKebabCase string + +func (m miscellaneousNameKebabCase) kebabcase() string { + return string(m) +} diff --git a/tpgtools/resource.go b/tpgtools/resource.go index 344f61f61046..41890b7bc6b0 100644 --- a/tpgtools/resource.go +++ b/tpgtools/resource.go @@ -229,6 +229,12 @@ func (r Resource) TerraformName() SnakeCaseFullName { return SnakeCaseFullName(concatenateSnakeCase(googlePrefix, r.ProductName(), r.Name())) } +// TerraformName is the Terraform resource type used for the sidebar_current field in documentation. +// For example, "google-compute-instance" +func (r Resource) SidebarCurrentName() KebabCaseTerraformResourceName { + return KebabCaseTerraformResourceName(r.TerraformName().ToKebab()) +} + // PathType is the title-cased name of a resource preceded by its package, // often used to namespace functions. For example, "RedisInstance". func (r Resource) PathType() TitleCaseFullName { @@ -492,17 +498,17 @@ func createResource(schema *openapi.Schema, info *openapi.Info, typeFetcher *Typ res.Properties = props onlyLongFormFormat := shouldAllowForwardSlashInFormat(res.ID, res.Properties) - // Resource Override: Import formats - ifd := ImportFormatDetails{} - ifdOk, err := overrides.ResourceOverrideWithDetails(ImportFormat, &ifd, location) - if err != nil { - return nil, fmt.Errorf("failed to decode import format details: %v", err) - } - if ifdOk { - res.ImportFormats = ifd.Formats - } else { - res.ImportFormats = defaultImportFormats(res.ID, onlyLongFormFormat) - } + // Resource Override: Import formats + ifd := ImportFormatDetails{} + ifdOk, err := overrides.ResourceOverrideWithDetails(ImportFormat, &ifd, location) + if err != nil { + return nil, fmt.Errorf("failed to decode import format details: %v", err) + } + if ifdOk { + res.ImportFormats = ifd.Formats + } else { + res.ImportFormats = defaultImportFormats(res.ID, onlyLongFormFormat) + } _, res.HasProject = schema.Properties["project"] diff --git a/tpgtools/templates/resource.html.markdown.tmpl b/tpgtools/templates/resource.html.markdown.tmpl index 5c86e676aa18..22f612ff538b 100644 --- a/tpgtools/templates/resource.html.markdown.tmpl +++ b/tpgtools/templates/resource.html.markdown.tmpl @@ -29,6 +29,7 @@ subcategory: "{{$.DocsSection}}" layout: "google" page_title: "Google: {{$.TerraformName}}" +sidebar_current: "docs-{{$.SidebarCurrentName}}" description: |- {{$.Description}} ---