Merge pull request #49224 from deads2k/client-01-plurals
Automatic merge from submit-queue (batch tested with PRs 49107, 47177, 49234, 49224, 49227) allow exceptions to be specified to handle conflicting group and resource names When a group name and resource name conflict, the generated code doesn't have prefixes or suffixes to produce compiling code. Instead, it simply produces code that won't compile. This makes it possible for the code generator to have a special kind of namer that can codify the exceptions to get compiling code. As we move the generators to become more general, this should be updated to be plumbed by flags. @gmarek give this a try in your event pull. Specify your type and see if the names are adjusted. @sttts we hit this downstream
This commit is contained in:
commit
d8ac3af420
@ -41,9 +41,36 @@ func NameSystems() namer.NameSystems {
|
||||
"Endpoints": "Endpoints",
|
||||
}
|
||||
lowercaseNamer := namer.NewAllLowercasePluralNamer(pluralExceptions)
|
||||
|
||||
publicNamer := &ExceptionNamer{
|
||||
Exceptions: map[string]string{
|
||||
// these exceptions are used to deconflict the generated code
|
||||
// you can put your fully qualified package like
|
||||
// to generate a name that doesn't conflict with your group.
|
||||
// "k8s.io/apis/events/v1alpha1.Event": "EventResource"
|
||||
},
|
||||
KeyFunc: func(t *types.Type) string {
|
||||
return t.Name.Package + "." + t.Name.Name
|
||||
},
|
||||
Delegate: namer.NewPublicNamer(0),
|
||||
}
|
||||
privateNamer := &ExceptionNamer{
|
||||
Exceptions: map[string]string{
|
||||
// these exceptions are used to deconflict the generated code
|
||||
// you can put your fully qualified package like
|
||||
// to generate a name that doesn't conflict with your group.
|
||||
// "k8s.io/apis/events/v1alpha1.Event": "eventResource"
|
||||
},
|
||||
KeyFunc: func(t *types.Type) string {
|
||||
return t.Name.Package + "." + t.Name.Name
|
||||
},
|
||||
Delegate: namer.NewPrivateNamer(0),
|
||||
}
|
||||
|
||||
return namer.NameSystems{
|
||||
"public": namer.NewPublicNamer(0),
|
||||
"private": namer.NewPrivateNamer(0),
|
||||
"singularKind": namer.NewPublicNamer(0),
|
||||
"public": publicNamer,
|
||||
"private": privateNamer,
|
||||
"raw": namer.NewRawNamer("", nil),
|
||||
"publicPlural": namer.NewPublicPluralNamer(pluralExceptions),
|
||||
"privatePlural": namer.NewPrivatePluralNamer(pluralExceptions),
|
||||
@ -52,6 +79,24 @@ func NameSystems() namer.NameSystems {
|
||||
}
|
||||
}
|
||||
|
||||
// ExceptionNamer allows you specify exceptional cases with exact names. This allows you to have control
|
||||
// for handling various conflicts, like group and resource names for instance.
|
||||
type ExceptionNamer struct {
|
||||
Exceptions map[string]string
|
||||
KeyFunc func(*types.Type) string
|
||||
|
||||
Delegate namer.Namer
|
||||
}
|
||||
|
||||
// Name provides the requested name for a type.
|
||||
func (n *ExceptionNamer) Name(t *types.Type) string {
|
||||
key := n.KeyFunc(t)
|
||||
if exception, ok := n.Exceptions[key]; ok {
|
||||
return exception
|
||||
}
|
||||
return n.Delegate.Name(t)
|
||||
}
|
||||
|
||||
// DefaultNameSystem returns the default name system for ordering the types to be
|
||||
// processed by the generators in this package.
|
||||
func DefaultNameSystem() string {
|
||||
|
@ -213,7 +213,7 @@ var $.type|allLowercasePlural$Resource = $.GroupVersionResource|raw${Group: "$.g
|
||||
`
|
||||
|
||||
var kind = `
|
||||
var $.type|allLowercasePlural$Kind = $.GroupVersionKind|raw${Group: "$.groupName$", Version: "$.version$", Kind: "$.type|public$"}
|
||||
var $.type|allLowercasePlural$Kind = $.GroupVersionKind|raw${Group: "$.groupName$", Version: "$.version$", Kind: "$.type|singularKind$"}
|
||||
`
|
||||
|
||||
var listTemplate = `
|
||||
|
Loading…
Reference in New Issue
Block a user