Skip to content

Commit

Permalink
Merge pull request #126503 from skitt/generic-fake-client
Browse files Browse the repository at this point in the history
Use generics to share code in fake client-go implementations

Kubernetes-commit: 31970d418ccae80c9c7e25e8c503035a79a53763
  • Loading branch information
k8s-publishing-bot committed Nov 27, 2024
2 parents c7820d2 + 43ff2eb commit 56b470e
Show file tree
Hide file tree
Showing 31 changed files with 550 additions and 2,539 deletions.
3 changes: 1 addition & 2 deletions cmd/client-gen/generators/fake/fake_client_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func TargetForGroup(gv clientgentypes.GroupVersion, typeList []*types.Type, clie
OutputFilename: "fake_" + strings.ToLower(c.Namers["private"].Name(t)) + ".go",
},
outputPackage: outputPkg,
realClientPackage: realClientPkg,
inputPackage: inputPkg,
group: gv.Group.NonEmpty(),
version: gv.Version.String(),
groupGoName: groupGoName,
typeToMatch: t,
Expand All @@ -74,7 +74,6 @@ func TargetForGroup(gv clientgentypes.GroupVersion, typeList []*types.Type, clie
},
outputPackage: outputPkg,
realClientPackage: realClientPkg,
group: gv.Group.NonEmpty(),
version: gv.Version.String(),
groupGoName: groupGoName,
types: typeList,
Expand Down
7 changes: 4 additions & 3 deletions cmd/client-gen/generators/fake/generator_fake_for_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type genFakeForGroup struct {
generator.GoGenerator
outputPackage string // must be a Go import-path
realClientPackage string // must be a Go import-path
group string
version string
groupGoName string
// types in this group
Expand Down Expand Up @@ -78,6 +77,8 @@ func (g *genFakeForGroup) GenerateType(c *generator.Context, t *types.Type, w io
"Fake": c.Universe.Type(types.Name{Package: "k8s.io/client-go/testing", Name: "Fake"}),
"RESTClientInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Interface"}),
"RESTClient": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "RESTClient"}),
"FakeClient": c.Universe.Type(types.Name{Package: "k8s.io/client-go/gentype", Name: "FakeClient"}),
"NewFakeClient": c.Universe.Function(types.Name{Package: "k8s.io/client-go/gentype", Name: "NewFakeClient"}),
}

sw.Do(groupClientTemplate, m)
Expand Down Expand Up @@ -110,13 +111,13 @@ type Fake$.GroupGoName$$.Version$ struct {

var getterImplNamespaced = `
func (c *Fake$.GroupGoName$$.Version$) $.type|publicPlural$(namespace string) $.realClientPackage$.$.type|public$Interface {
return &Fake$.type|publicPlural${c, namespace}
return newFake$.type|publicPlural$(c, namespace)
}
`

var getterImplNonNamespaced = `
func (c *Fake$.GroupGoName$$.Version$) $.type|publicPlural$() $.realClientPackage$.$.type|public$Interface {
return &Fake$.type|publicPlural${c}
return newFake$.type|publicPlural$(c)
}
`

Expand Down
432 changes: 194 additions & 238 deletions cmd/client-gen/generators/fake/generator_fake_for_type.go

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 56b470e

Please sign in to comment.