Skip to content

Commit

Permalink
Improve pluralization of type names (#364)
Browse files Browse the repository at this point in the history
* Extend manual fixes for pluralization

* Implement Stringer on PropertyDefinition
  • Loading branch information
theunrepentantgeek authored Jan 7, 2021
1 parent 3025caf commit 44e6e05
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
6 changes: 6 additions & 0 deletions hack/generator/pkg/astmodel/property_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type PropertyDefinition struct {
tags map[string][]string
}

var _ fmt.Stringer = &PropertyDefinition{}

// NewPropertyDefinition is a factory method for creating a new PropertyDefinition
// name is the name for the new property (mandatory)
// propertyType is the type for the new property (mandatory)
Expand Down Expand Up @@ -325,3 +327,7 @@ func (property *PropertyDefinition) copy() *PropertyDefinition {

return &result
}

func (property *PropertyDefinition) String() string {
return fmt.Sprintf("%s: %s %s", property.propertyName, property.propertyType, property.renderedTags())
}
16 changes: 14 additions & 2 deletions hack/generator/pkg/astmodel/type_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,23 @@ func (typeName TypeName) String() string {
return fmt.Sprintf("%s/%s", typeName.PackageReference, typeName.name)
}

var typeNameSingulars map[string]string = map[string]string{
"Services": "Service",
"services": "service",
"Redis": "Redis",
"redis": "redis",
}

// Singular returns a typename with the name singularized
func (typeName TypeName) Singular() TypeName {
// work around bug in flect: https://github.com/Azure/k8s-infra/issues/319
if strings.HasSuffix(strings.ToLower(typeName.name), "services") {
return MakeTypeName(typeName.PackageReference, typeName.name[0:len(typeName.name)-1])
name := typeName.name
for plural, single := range typeNameSingulars {

if strings.HasSuffix(name, plural) {
n := name[0:len(name)-len(plural)] + single
return MakeTypeName(typeName.PackageReference, n)
}
}

return MakeTypeName(typeName.PackageReference, flect.Singularize(typeName.name))
Expand Down
41 changes: 41 additions & 0 deletions hack/generator/pkg/astmodel/type_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package astmodel

import (
"testing"

. "github.com/onsi/gomega"
)

func TestSingular_GivesExpectedResults(t *testing.T) {
cases := []struct {
name string
expected string
}{
{"Account", "Account"},
{"Accounts", "Account"},
{"Batch", "Batch"},
{"Batches", "Batch"},
{"ImportServices", "ImportService"},
{"Exportservices", "Exportservice"},
{"AzureRedis", "AzureRedis"},
}

ref := MakeLocalPackageReference("Demo", "v2010")

for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)

name := MakeTypeName(ref, c.name)
result := name.Singular()
g.Expect(result.name).To(Equal(c.expected))
})
}
}

0 comments on commit 44e6e05

Please # to comment.