Skip to content

Commit b09070f

Browse files
authored
feat(gateway): correct plural form (#207)
* used flect to pluralize On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]> --------- Signed-off-by: Artem Shcherbatiuk <[email protected]>
1 parent b456bb9 commit b09070f

File tree

6 files changed

+77
-18
lines changed

6 files changed

+77
-18
lines changed

gateway/schema/exports_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
package schema
22

3-
var StringMapScalar = stringMapScalar
3+
import "k8s.io/apimachinery/pkg/runtime/schema"
4+
5+
var StringMapScalarForTest = stringMapScalar
6+
7+
func GetGatewayForTest(typeNameRegistry map[string]string) *Gateway {
8+
return &Gateway{
9+
typeNameRegistry: typeNameRegistry,
10+
}
11+
}
12+
13+
func (g *Gateway) GetNamesForTest(gvk *schema.GroupVersionKind) (singular, plural string) {
14+
return g.getNames(gvk)
15+
}

gateway/schema/scalars_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestStringMapScalar_ParseValue(t *testing.T) {
2929
}
3030

3131
for _, test := range tests {
32-
out := schema.StringMapScalar.ParseValue(test.input)
32+
out := schema.StringMapScalarForTest.ParseValue(test.input)
3333
if !reflect.DeepEqual(out, test.expected) {
3434
t.Errorf("ParseValue(%v) = %v; want %v", test.input, out, test.expected)
3535
}
@@ -68,7 +68,7 @@ func TestStringMapScalar_ParseLiteral(t *testing.T) {
6868

6969
for _, tt := range tests {
7070
t.Run(tt.name, func(t *testing.T) {
71-
out := schema.StringMapScalar.ParseLiteral(tt.input)
71+
out := schema.StringMapScalarForTest.ParseLiteral(tt.input)
7272
if !reflect.DeepEqual(out, tt.expected) {
7373
t.Errorf("ParseLiteral() = %v, want %v", out, tt.expected)
7474
}

gateway/schema/schema.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"regexp"
77
"strings"
88

9-
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
10-
119
"github.com/go-openapi/spec"
10+
"github.com/gobuffalo/flect"
1211
"github.com/graphql-go/graphql"
12+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1313
"k8s.io/apimachinery/pkg/runtime/schema"
1414

1515
"github.com/openmfp/golang-commons/logger"
@@ -33,7 +33,7 @@ type Gateway struct {
3333
// inputTypesCache stores generated GraphQL input object types(input fields) to prevent redundant repeated generation.
3434
inputTypesCache map[string]*graphql.InputObject
3535
// Prevents naming conflict in case of the same Kind name in different groups/versions
36-
typeNameRegistry map[string]string
36+
typeNameRegistry map[string]string // map[Kind]GroupVersion
3737

3838
// categoryRegistry stores resources by category for typeByCategory query
3939
typeByCategory map[string][]resolver.TypeByCategory
@@ -275,28 +275,24 @@ func (g *Gateway) processSingleResource(
275275

276276
func (g *Gateway) getNames(gvk *schema.GroupVersionKind) (singular string, plural string) {
277277
kind := gvk.Kind
278-
singularName := kind
278+
singular = kind
279+
plural = flect.Pluralize(singular)
279280

280281
// Check if the kind name has already been used for a different group/version
281282
if existingGroupVersion, exists := g.typeNameRegistry[kind]; exists {
282283
if existingGroupVersion != gvk.GroupVersion().String() {
283-
// Conflict detected, append group and version
284-
groupVersion := strings.ReplaceAll(gvk.GroupVersion().String(), "/", "")
285-
singularName = kind + groupVersion
284+
// Conflict detected, append group and version to the kind for uniqueness
285+
// we don't add new entry to the registry, because we already have one with the same kind
286+
group := strings.ReplaceAll(gvk.Group, ".", "") // dots are allowed in k8s group, but not in graphql
287+
singular = strings.Join([]string{kind, group, gvk.Version}, "_")
288+
plural = strings.Join([]string{plural, group, gvk.Version}, "_")
286289
}
287290
} else {
288291
// No conflict, register the kind with its group and version
289292
g.typeNameRegistry[kind] = gvk.GroupVersion().String()
290293
}
291294

292-
var pluralName string
293-
if singularName[len(singularName)-1] == 's' {
294-
pluralName = singularName + "es"
295-
} else {
296-
pluralName = singularName + "s"
297-
}
298-
299-
return singularName, pluralName
295+
return singular, plural
300296
}
301297

302298
func (g *Gateway) getDefinitionsByGroup(filteredDefinitions spec.Definitions) map[string]spec.Definitions {

gateway/schema/schema_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package schema_test
2+
3+
import (
4+
"testing"
5+
6+
"k8s.io/apimachinery/pkg/runtime/schema"
7+
8+
gatewaySchema "github.com/openmfp/kubernetes-graphql-gateway/gateway/schema"
9+
)
10+
11+
func TestGateway_getNames(t *testing.T) {
12+
type testCase struct {
13+
name string
14+
registry map[string]string
15+
gvk schema.GroupVersionKind
16+
wantSingular string
17+
wantPlural string
18+
}
19+
20+
tests := []testCase{
21+
{
22+
name: "no_conflict",
23+
registry: map[string]string{},
24+
gvk: schema.GroupVersionKind{Group: "core", Version: "v1", Kind: "Pod"},
25+
wantSingular: "Pod",
26+
wantPlural: "Pods",
27+
},
28+
{
29+
name: "same_kind_different_group_version",
30+
registry: map[string]string{"Pod": "core/v1"},
31+
gvk: schema.GroupVersionKind{Group: "custom.io", Version: "v2", Kind: "Pod"},
32+
wantSingular: "Pod_customio_v2",
33+
wantPlural: "Pods_customio_v2",
34+
},
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
g := gatewaySchema.GetGatewayForTest(tt.registry)
40+
gotSingular, gotPlural := g.GetNamesForTest(&tt.gvk)
41+
42+
if gotSingular != tt.wantSingular || gotPlural != tt.wantPlural {
43+
t.Errorf("getNames() = (%q, %q), want (%q, %q)",
44+
gotSingular, gotPlural, tt.wantSingular, tt.wantPlural)
45+
}
46+
})
47+
}
48+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ replace github.com/graphql-go/handler => github.com/vertex451/handler v0.0.0-202
1111
require (
1212
github.com/fsnotify/fsnotify v1.9.0
1313
github.com/go-openapi/spec v0.21.0
14+
github.com/gobuffalo/flect v1.0.3
1415
github.com/golang-jwt/jwt/v5 v5.2.2
1516
github.com/google/gnostic-models v0.6.9
1617
github.com/graphql-go/graphql v0.8.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v
6363
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
6464
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
6565
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
66+
github.com/gobuffalo/flect v1.0.3 h1:xeWBM2nui+qnVvNM4S3foBhCAL2XgPU+a7FdpelbTq4=
67+
github.com/gobuffalo/flect v1.0.3/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs=
6668
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
6769
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
6870
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=

0 commit comments

Comments
 (0)