Skip to content

Commit

Permalink
refactor: added ToTitle method to deal with deprecated titling in go1.18
Browse files Browse the repository at this point in the history
Signed-off-by: Dustin Scott <dustin.scott18@gmail.com>
  • Loading branch information
scottd018 committed Jun 20, 2022
1 parent 627b6e2 commit d4ac84a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions internal/utils/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
5 changes: 3 additions & 2 deletions internal/workload/v1/kinds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")),
Expand Down
5 changes: 3 additions & 2 deletions internal/workload/v1/manifests/child_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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", "")
Expand All @@ -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", "")
Expand Down
5 changes: 3 additions & 2 deletions internal/workload/v1/markers/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit d4ac84a

Please sign in to comment.