diff --git a/hack/generator/pkg/astmodel/property_definition.go b/hack/generator/pkg/astmodel/property_definition.go index 1dcf9fe6d2f..24628e229b7 100644 --- a/hack/generator/pkg/astmodel/property_definition.go +++ b/hack/generator/pkg/astmodel/property_definition.go @@ -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) @@ -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()) +} diff --git a/hack/generator/pkg/astmodel/type_name.go b/hack/generator/pkg/astmodel/type_name.go index facd63cc264..98b5775525f 100644 --- a/hack/generator/pkg/astmodel/type_name.go +++ b/hack/generator/pkg/astmodel/type_name.go @@ -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)) diff --git a/hack/generator/pkg/astmodel/type_name_test.go b/hack/generator/pkg/astmodel/type_name_test.go new file mode 100644 index 00000000000..f60bb846640 --- /dev/null +++ b/hack/generator/pkg/astmodel/type_name_test.go @@ -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)) + }) + } +}