From 2d1966397667a9c88d8f3d0ce7b46bb04cd0feed Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Tue, 22 Oct 2024 11:58:02 -0400 Subject: [PATCH] use `github.com/gertd/go-pluralize` to correct special pluralization cases --- Third_Party_Code/NOTICES.md | 33 ++++++++++++++ .../github.com/gertd/go-pluralize/LICENSE | 23 ++++++++++ apstra/enum/generated_enums.go | 6 +-- apstra/enum/generator/generator.go | 43 +++++++++++++------ apstra/helpers.go | 2 +- go.mod | 3 +- go.sum | 2 + 7 files changed, 95 insertions(+), 17 deletions(-) create mode 100644 Third_Party_Code/github.com/gertd/go-pluralize/LICENSE diff --git a/Third_Party_Code/NOTICES.md b/Third_Party_Code/NOTICES.md index 85740a24..096d7e45 100644 --- a/Third_Party_Code/NOTICES.md +++ b/Third_Party_Code/NOTICES.md @@ -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 (hello@blakeembrey.com) + +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 diff --git a/Third_Party_Code/github.com/gertd/go-pluralize/LICENSE b/Third_Party_Code/github.com/gertd/go-pluralize/LICENSE new file mode 100644 index 00000000..f002d157 --- /dev/null +++ b/Third_Party_Code/github.com/gertd/go-pluralize/LICENSE @@ -0,0 +1,23 @@ +MIT License + +Copyright (c) 2019 Gert Drapers + +Copyright (c) 2013 Blake Embrey (hello@blakeembrey.com) + +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. diff --git a/apstra/enum/generated_enums.go b/apstra/enum/generated_enums.go index 84d37d17..ba615e48 100644 --- a/apstra/enum/generated_enums.go +++ b/apstra/enum/generated_enums.go @@ -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 @@ -312,8 +312,8 @@ var ( FFResourceTypeVni, ) - _ enum = new(FeatureSwitch) - FeatureSwitchs = oenum.New( + _ enum = new(FeatureSwitch) + FeatureSwitches = oenum.New( FeatureSwitchEnabled, FeatureSwitchDisabled, ) diff --git a/apstra/enum/generator/generator.go b/apstra/enum/generator/generator.go index aa045a5a..468ea8a1 100644 --- a/apstra/enum/generator/generator.go +++ b/apstra/enum/generator/generator.go @@ -17,6 +17,7 @@ import ( "text/template" "time" + "github.com/gertd/go-pluralize" "golang.org/x/tools/go/packages" ) @@ -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 { @@ -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} @@ -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 { @@ -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) } @@ -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 diff --git a/apstra/helpers.go b/apstra/helpers.go index a5147518..da9568c9 100644 --- a/apstra/helpers.go +++ b/apstra/helpers.go @@ -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 { diff --git a/go.mod b/go.mod index d2a2ef50..3cfcf16e 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 ) diff --git a/go.sum b/go.sum index 291c4115..cfb42f3d 100644 --- a/go.sum +++ b/go.sum @@ -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=