From d4ac84a9791d3e89e9a788d0481c021327633110 Mon Sep 17 00:00:00 2001 From: Dustin Scott Date: Mon, 20 Jun 2022 14:10:18 -0500 Subject: [PATCH] refactor: added ToTitle method to deal with deprecated titling in go1.18 Signed-off-by: Dustin Scott --- internal/license/license.go | 2 +- internal/utils/names.go | 9 +++++++++ internal/workload/v1/kinds/api.go | 5 +++-- internal/workload/v1/manifests/child_resource.go | 5 +++-- internal/workload/v1/markers/markers.go | 5 +++-- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/internal/license/license.go b/internal/license/license.go index 6adf62f..572fb37 100644 --- a/internal/license/license.go +++ b/internal/license/license.go @@ -100,7 +100,7 @@ func getSourceLicense(source string) ([]byte, error) { if source[0:4] == "http" { // source is HTTP URL - resp, err := http.Get(source) //nolint:gosec + resp, err := http.Get(source) //nolint:gosec,noctx if err != nil { return []byte{}, fmt.Errorf("unable to get license source from %s, %w", source, err) } diff --git a/internal/utils/names.go b/internal/utils/names.go index 726fe68..60c1caa 100644 --- a/internal/utils/names.go +++ b/internal/utils/names.go @@ -5,6 +5,9 @@ package utils import ( "strings" + + "golang.org/x/text/cases" + "golang.org/x/text/language" ) // ToPascalCase will convert a kebab-case string to a PascalCase name appropriate to @@ -41,3 +44,9 @@ func ToFileName(name string) string { func ToPackageName(name string) string { return strings.ToLower(strings.Replace(name, "-", "", -1)) } + +// ToTitle replaces the strings.Title method, which is deprecated in go1.18. This is a helper +// method to make titling a string much more readable than the new methodology. +func ToTitle(in string) string { + return cases.Title(language.AmericanEnglish, cases.NoLower).String(in) +} diff --git a/internal/workload/v1/kinds/api.go b/internal/workload/v1/kinds/api.go index 2a50418..22a67e2 100644 --- a/internal/workload/v1/kinds/api.go +++ b/internal/workload/v1/kinds/api.go @@ -11,6 +11,7 @@ import ( "reflect" "strings" + "github.com/vmware-tanzu-labs/operator-builder/internal/utils" "github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/markers" ) @@ -198,7 +199,7 @@ func (api *APIFields) generateStructName(path string) { mustWrite(buf.WriteString("Spec")) for _, part := range strings.Split(path, ".") { - mustWrite(buf.WriteString(strings.Title(part))) + mustWrite(buf.WriteString(utils.ToTitle(part))) if part == api.manifestName { break @@ -288,7 +289,7 @@ func (api *APIFields) setCommentsAndDefault(comments []string, sampleVal interfa func (api *APIFields) newChild(name string, fieldType markers.FieldType, sample interface{}) *APIFields { child := &APIFields{ - Name: strings.Title(name), + Name: utils.ToTitle(name), manifestName: name, Type: fieldType, Tags: fmt.Sprintf("`json:%q`", fmt.Sprintf("%s,%s", name, "omitempty")), diff --git a/internal/workload/v1/manifests/child_resource.go b/internal/workload/v1/manifests/child_resource.go index 0fb94fd..892686c 100644 --- a/internal/workload/v1/manifests/child_resource.go +++ b/internal/workload/v1/manifests/child_resource.go @@ -10,6 +10,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "github.com/vmware-tanzu-labs/operator-builder/internal/utils" "github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/markers" "github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/rbac" ) @@ -139,7 +140,7 @@ func (resource *ChildResource) NameConstant() string { // when we generate the function names to avoid collisions. func uniqueName(object unstructured.Unstructured) string { // get the resource name taking into account appropriate yaml tags - resourceName := strings.ReplaceAll(strings.Title(object.GetName()), "-", "") + resourceName := strings.ReplaceAll(utils.ToTitle(object.GetName()), "-", "") resourceName = strings.ReplaceAll(resourceName, ".", "") resourceName = strings.ReplaceAll(resourceName, ":", "") resourceName = strings.ReplaceAll(resourceName, "!!Start", "") @@ -149,7 +150,7 @@ func uniqueName(object unstructured.Unstructured) string { resourceName = strings.ReplaceAll(resourceName, " ", "") // get the namespace name taking into account appropriate yaml tags - namespaceName := strings.ReplaceAll(strings.Title(object.GetNamespace()), "-", "") + namespaceName := strings.ReplaceAll(utils.ToTitle(object.GetNamespace()), "-", "") namespaceName = strings.ReplaceAll(namespaceName, ".", "") namespaceName = strings.ReplaceAll(namespaceName, ":", "") namespaceName = strings.ReplaceAll(namespaceName, "!!Start", "") diff --git a/internal/workload/v1/markers/markers.go b/internal/workload/v1/markers/markers.go index b4159cb..5a041d9 100644 --- a/internal/workload/v1/markers/markers.go +++ b/internal/workload/v1/markers/markers.go @@ -13,6 +13,7 @@ import ( "github.com/vmware-tanzu-labs/operator-builder/internal/markers/inspect" markerparser "github.com/vmware-tanzu-labs/operator-builder/internal/markers/marker" + "github.com/vmware-tanzu-labs/operator-builder/internal/utils" ) var ( @@ -208,7 +209,7 @@ func isSupported(parentField string) bool { // validField determines if a field is valid based on a known list of valid fields. func validField(field string, validFields []string) bool { for _, valid := range validFields { - if strings.Title(valid) == strings.Title(field) { + if utils.ToTitle(valid) == utils.ToTitle(field) { return true } } @@ -236,7 +237,7 @@ func getSourceCodeFieldVariable(marker FieldMarkerProcessor) (string, error) { // scaffolded in the source code. func getSourceCodeVariable(marker MarkerProcessor) (string, error) { if marker.GetParent() == "" { - return fmt.Sprintf("%s.%s", marker.GetSpecPrefix(), strings.Title((marker.GetName()))), nil + return fmt.Sprintf("%s.%s", marker.GetSpecPrefix(), utils.ToTitle(marker.GetName())), nil } if isSupported(marker.GetParent()) {