Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions tools/importer-msgraph-metadata/components/normalize/plurals.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package normalize
import (
"fmt"
"regexp"
"sync"

"github.com/gertd/go-pluralize"
)
Expand All @@ -27,8 +28,21 @@ var pluralExceptions = []plural{
{"Sortby", "Sortby"},
}

var singularMatchers = map[string]*regexp.Regexp{}
var pluralMatchers = map[string]*regexp.Regexp{}
var (
singularMatchers = map[string]*regexp.Regexp{}
pluralMatchers = map[string]*regexp.Regexp{}
pluralizeClient *pluralize.Client
once sync.Once
// pluralizeClient is not thread safe
mux sync.Mutex
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is actually thread safe, do you have evidence to the contrary? (Singular() and Plural() are "read-only" and don't have any internal state change to lock for, so should be fine?)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, Thanks! it's safe as we only call Singular and Plural here.

)

func PluralizeClient() *pluralize.Client {
once.Do(func() {
pluralizeClient = pluralize.NewClient()
})
return pluralizeClient
}

func init() {
singularMatchers = make(map[string]*regexp.Regexp)
Expand All @@ -47,8 +61,9 @@ func Singularize(name string) string {
}
}

client := pluralize.NewClient()
output := client.Singular(name)
mux.Lock()
defer mux.Unlock()
output := PluralizeClient().Singular(name)

return output
}
Expand All @@ -60,8 +75,9 @@ func Pluralize(name string) string {
}
}

client := pluralize.NewClient()
output := client.Plural(name)
mux.Lock()
defer mux.Unlock()
output := PluralizeClient().Plural(name)

return output
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cleanup
import (
"fmt"
"strings"
"sync"

"github.com/gertd/go-pluralize"
)
Expand All @@ -24,6 +25,20 @@ const (
CAMEL // This is likely the default in the context of TF and Azure?
)

var (
pluralizeClient *pluralize.Client
once sync.Once
// pluralizeClient is not thread safe
mux sync.Mutex
)

func PluralizeClient() *pluralize.Client {
once.Do(func() {
pluralizeClient = pluralize.NewClient()
})
return pluralizeClient
}

// GetSingular return the singular version of a given plural
// return values are case preserved to the input.
func GetSingular(input string) string {
Expand All @@ -43,8 +58,9 @@ func GetSingular(input string) string {
}
}

client := pluralize.NewClient()
output := client.Singular(input)
mux.Lock()
defer mux.Unlock()
output := PluralizeClient().Singular(input)

return returnCased(output, casing)
}
Expand All @@ -69,8 +85,9 @@ func GetPlural(input string) string {
}
}

pluralize := pluralize.NewClient()
output := pluralize.Plural(input)
mux.Lock()
defer mux.Unlock()
output := PluralizeClient().Plural(input)

return returnCased(output, casing)
}
Expand Down
Loading