Skip to content

Commit

Permalink
use github.com/gertd/go-pluralize to correct special pluralization …
Browse files Browse the repository at this point in the history
…cases
  • Loading branch information
chrismarget-j committed Oct 22, 2024
1 parent 0d8fc28 commit 2d19663
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 17 deletions.
33 changes: 33 additions & 0 deletions Third_Party_Code/NOTICES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@

## github.com/gertd/go-pluralize

* Name: github.com/gertd/go-pluralize
* Version: v0.2.1
* License: [MIT](https://github.com/gertd/go-pluralize/blob/v0.2.1/LICENSE)

```
MIT License
Copyright (c) 2019 Gert Drapers
Copyright (c) 2013 Blake Embrey ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

## github.com/google/uuid

* Name: github.com/google/uuid
Expand Down
23 changes: 23 additions & 0 deletions Third_Party_Code/github.com/gertd/go-pluralize/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MIT License

Copyright (c) 2019 Gert Drapers

Copyright (c) 2013 Blake Embrey ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 3 additions & 3 deletions apstra/enum/generated_enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (o FeatureSwitch) String() string {
}

func (o *FeatureSwitch) FromString(s string) error {
if FeatureSwitchs.Parse(s) == nil {
if FeatureSwitches.Parse(s) == nil {
return newEnumParseError(o, s)
}
o.Value = s
Expand Down Expand Up @@ -312,8 +312,8 @@ var (
FFResourceTypeVni,
)

_ enum = new(FeatureSwitch)
FeatureSwitchs = oenum.New(
_ enum = new(FeatureSwitch)
FeatureSwitches = oenum.New(
FeatureSwitchEnabled,
FeatureSwitchDisabled,
)
Expand Down
43 changes: 31 additions & 12 deletions apstra/enum/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"text/template"
"time"

"github.com/gertd/go-pluralize"
"golang.org/x/tools/go/packages"
)

Expand All @@ -33,7 +34,7 @@ const (
package enum
import oenum "github.com/orsinium-labs/enum"
{{ range $key, $value := .TypeToVals }}
{{ range $key, $value := .NameToTypeInfo }}
var _ enum = (*{{ $key }})(nil)
func (o {{ $key }}) String() string {
Expand All @@ -48,19 +49,28 @@ func (o *{{ $key }}) FromString(s string) error {
return nil
}
{{ end }}
var ({{ range $key, $value := .TypeToVals }}
var ({{ range $key, $value := .NameToTypeInfo }}
_ enum = new({{ $key }})
{{ $key }}s = oenum.New({{ range $v := $value }}
{{ $value.Plural }} = oenum.New({{ range $v := $value.Values }}
{{ $v }},{{ end }}
)
{{ end }})
`
)

var TypeToVals map[string][]string
type TypeInfo struct {
Plural string // used with oenum.New() - Things
Values []string // each enum value - Thing1, Thing2, ... ThingN
}

var (
Pluralize *pluralize.Client
TypeNameToInfo map[string]TypeInfo
)

func main() {
TypeToVals = make(map[string][]string)
Pluralize = pluralize.NewClient()
TypeNameToInfo = make(map[string]TypeInfo)

cfg := packages.Config{Mode: packages.NeedTypes | packages.NeedSyntax}

Expand Down Expand Up @@ -104,13 +114,13 @@ func main() {
}

func render() error {
var data struct {
Year string
TypeToVals map[string][]string
var tmplData struct {
Year string
NameToTypeInfo map[string]TypeInfo
}

data.Year = time.Now().Format("2006")
data.TypeToVals = TypeToVals
tmplData.Year = time.Now().Format("2006")
tmplData.NameToTypeInfo = TypeNameToInfo

f, err := os.Create(outFile)
if err != nil {
Expand All @@ -122,7 +132,7 @@ func render() error {
return fmt.Errorf("while parsing template - %w", err)
}

err = tmpl.Execute(f, data)
err = tmpl.Execute(f, tmplData)
if err != nil {
return fmt.Errorf("while executing template - %w", err)
}
Expand Down Expand Up @@ -163,7 +173,16 @@ func handleVar(gd *ast.GenDecl) error {

tName := valueType.Name

TypeToVals[tName] = append(TypeToVals[tName], name.Name)
// Fill the TypeNameToInfo map which is used by render()
var typeInfo TypeInfo
if typeInfo, ok = TypeNameToInfo[tName]; !ok {
typeInfo.Plural = Pluralize.Plural(tName)
if tName == typeInfo.Plural {
return fmt.Errorf("cannot pluralize - plural of %q is %q", tName, typeInfo.Plural)
}
}
typeInfo.Values = append(typeInfo.Values, name.Name)
TypeNameToInfo[tName] = typeInfo
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion apstra/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func featureSwitchEnumFromStringPtr(in *string) *enum.FeatureSwitch {
if in == nil {
return nil
}
return enum.FeatureSwitchs.Parse(*in)
return enum.FeatureSwitches.Parse(*in)
}

func isv4(ip net.IP) bool {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.18.21
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.19.3
github.com/chrismarget-j/go-licenses v0.0.0-20240224210557-f22f3e06d3d4
github.com/gertd/go-pluralize v0.2.1
github.com/google/uuid v1.3.0
github.com/hashicorp/go-version v1.7.0
github.com/hashicorp/hcl/v2 v2.19.1
github.com/orsinium-labs/enum v1.3.0
github.com/stretchr/testify v1.8.1
golang.org/x/tools v0.17.0
google.golang.org/protobuf v1.33.0
mvdan.cc/gofumpt v0.6.0
)
Expand Down Expand Up @@ -49,7 +51,6 @@ require (
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA=
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand Down

0 comments on commit 2d19663

Please sign in to comment.