Merge pull request #56099 from caesarxuchao/fix-client-gen-0-types

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Fix client-gen for groups that no types requiring clients

If the group has no type with the `+genClient` tag, the group client is generated multiple times because the `Filter` function always returns true, resulting in bad output like https://gist.github.com/gmarek/9a11d5a305a52b193889684e56c103e4.

unblock #49112 
cc @gmarek
pull/6/head
Kubernetes Submit Queue 2017-11-22 09:34:43 -08:00 committed by GitHub
commit 9d1e082105
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -40,13 +40,19 @@ type genFakeForGroup struct {
// types in this group
types []*types.Type
imports namer.ImportTracker
// If the genGroup has been called. This generator should only execute once.
called bool
}
var _ generator.Generator = &genFakeForGroup{}
// We only want to call GenerateType() once per group.
func (g *genFakeForGroup) Filter(c *generator.Context, t *types.Type) bool {
return len(g.types) == 0 || t == g.types[0]
if !g.called {
g.called = true
return true
}
return false
}
func (g *genFakeForGroup) Namers(c *generator.Context) namer.NameSystems {
@ -56,7 +62,10 @@ func (g *genFakeForGroup) Namers(c *generator.Context) namer.NameSystems {
}
func (g *genFakeForGroup) Imports(c *generator.Context) (imports []string) {
imports = append(g.imports.ImportLines(), strings.ToLower(fmt.Sprintf("%s \"%s\"", filepath.Base(g.realClientPackage), g.realClientPackage)))
imports = g.imports.ImportLines()
if len(g.types) != 0 {
imports = append(imports, strings.ToLower(fmt.Sprintf("%s \"%s\"", filepath.Base(g.realClientPackage), g.realClientPackage)))
}
return imports
}

View File

@ -41,13 +41,19 @@ type genGroup struct {
imports namer.ImportTracker
inputPackage string
clientsetPackage string
// If the genGroup has been called. This generator should only execute once.
called bool
}
var _ generator.Generator = &genGroup{}
// We only want to call GenerateType() once per group.
func (g *genGroup) Filter(c *generator.Context, t *types.Type) bool {
return len(g.types) == 0 || t == g.types[0]
if !g.called {
g.called = true
return true
}
return false
}
func (g *genGroup) Namers(c *generator.Context) namer.NameSystems {