Skip to content

Commit 4873de5

Browse files
committed
codegen: get rid of adjustTemplate
This adds the expected verb name to the template map, and uses that in the template instead of replacing the verb name in the template before processing. Signed-off-by: Stephen Kitt <[email protected]>
1 parent 17bae91 commit 4873de5

File tree

1 file changed

+45
-51
lines changed

1 file changed

+45
-51
lines changed

staging/src/k8s.io/code-generator/cmd/client-gen/generators/generator_for_type.go

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -271,78 +271,72 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
271271
m["inputType"] = &inputType
272272
m["resultType"] = &resultType
273273
m["subresourcePath"] = e.SubResourcePath
274+
m["verb"] = e.VerbName
274275
if e.HasVerb("apply") {
275276
m["inputApplyConfig"] = types.Ref(path.Join(g.applyConfigurationPackage, inputGVString), inputType.Name.Name+"ApplyConfiguration")
276277
}
277278

278279
if e.HasVerb("get") {
279280
if e.IsSubresource() {
280-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, getSubresourceTemplate), m)
281+
sw.Do(getSubresourceTemplate, m)
281282
} else {
282-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, getTemplate), m)
283+
sw.Do(getTemplate, m)
283284
}
284285
}
285286

286287
if e.HasVerb("list") {
287288
if e.IsSubresource() {
288-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, listSubresourceTemplate), m)
289+
sw.Do(listSubresourceTemplate, m)
289290
} else {
290-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, listTemplate), m)
291-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, privateListTemplate), m)
292-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, watchListTemplate), m)
291+
sw.Do(listTemplate, m)
292+
sw.Do(privateListTemplate, m)
293+
sw.Do(watchListTemplate, m)
293294
}
294295
}
295296

296297
// TODO: Figure out schemantic for watching a sub-resource.
297298
if e.HasVerb("watch") {
298-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, watchTemplate), m)
299+
sw.Do(watchTemplate, m)
299300
}
300301

301302
if e.HasVerb("create") {
302303
if e.IsSubresource() {
303-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, createSubresourceTemplate), m)
304+
sw.Do(createSubresourceTemplate, m)
304305
} else {
305-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, createTemplate), m)
306+
sw.Do(createTemplate, m)
306307
}
307308
}
308309

309310
if e.HasVerb("update") {
310311
if e.IsSubresource() {
311-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, updateSubresourceTemplate), m)
312+
sw.Do(updateSubresourceTemplate, m)
312313
} else {
313-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, updateTemplate), m)
314+
sw.Do(updateTemplate, m)
314315
}
315316
}
316317

317318
// TODO: Figure out schemantic for deleting a sub-resource (what arguments
318319
// are passed, does it need two names? etc.
319320
if e.HasVerb("delete") {
320-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, deleteTemplate), m)
321+
sw.Do(deleteTemplate, m)
321322
}
322323

323324
if e.HasVerb("patch") {
324-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, patchTemplate), m)
325+
sw.Do(patchTemplate, m)
325326
}
326327

327328
if e.HasVerb("apply") {
328329
if e.IsSubresource() {
329-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, applySubresourceTemplate), m)
330+
sw.Do(applySubresourceTemplate, m)
330331
} else {
331-
sw.Do(adjustTemplate(e.VerbName, e.VerbType, applyTemplate), m)
332+
sw.Do(applyTemplate, m)
332333
}
333334
}
334335
}
335336

336337
return sw.Error()
337338
}
338339

339-
// adjustTemplate adjust the origin verb template using the expansion name.
340-
// TODO: Make the verbs in templates parametrized so the strings.Replace() is
341-
// not needed.
342-
func adjustTemplate(name, verbType, template string) string {
343-
return strings.ReplaceAll(template, " "+titler.String(verbType), " "+name)
344-
}
345-
346340
func generateInterface(defaultVerbTemplates map[string]string, tags util.Tags) string {
347341
// need an ordered list here to guarantee order of generated methods.
348342
out := []string{}
@@ -583,8 +577,8 @@ var newStruct = []string{
583577
}
584578

585579
var listTemplate = `
586-
// List takes label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
587-
func (c *$.type|privatePlural$) List(ctx $.context|raw$, opts $.ListOptions|raw$) (*$.resultType|raw$List, error) {
580+
// $.verb$ takes label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
581+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, opts $.ListOptions|raw$) (*$.resultType|raw$List, error) {
588582
if watchListOptions, hasWatchListOptionsPrepared, watchListOptionsErr := $.PrepareWatchListOptionsFromListOptions|raw$(opts); watchListOptionsErr != nil {
589583
klog.Warningf("Failed preparing watchlist options for $.type|resource$, falling back to the standard LIST semantics, err = %v", watchListOptionsErr )
590584
} else if hasWatchListOptionsPrepared {
@@ -623,8 +617,8 @@ func (c *$.type|privatePlural$) list(ctx $.context|raw$, opts $.ListOptions|raw$
623617
`
624618

625619
var listSubresourceTemplate = `
626-
// List takes $.type|raw$ name, label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
627-
func (c *$.type|privatePlural$) List(ctx $.context|raw$, $.type|private$Name string, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) {
620+
// $.verb$ takes $.type|raw$ name, label and field selectors, and returns the list of $.resultType|publicPlural$ that match those selectors.
621+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.type|private$Name string, opts $.ListOptions|raw$) (result *$.resultType|raw$List, err error) {
628622
var timeout time.Duration
629623
if opts.TimeoutSeconds != nil{
630624
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
@@ -644,8 +638,8 @@ func (c *$.type|privatePlural$) List(ctx $.context|raw$, $.type|private$Name str
644638
`
645639

646640
var getTemplate = `
647-
// Get takes name of the $.type|private$, and returns the corresponding $.resultType|private$ object, and an error if there is any.
648-
func (c *$.type|privatePlural$) Get(ctx $.context|raw$, name string, options $.GetOptions|raw$) (result *$.resultType|raw$, err error) {
641+
// $.verb$ takes name of the $.type|private$, and returns the corresponding $.resultType|private$ object, and an error if there is any.
642+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, name string, options $.GetOptions|raw$) (result *$.resultType|raw$, err error) {
649643
result = &$.resultType|raw${}
650644
err = c.GetClient().Get().
651645
$if .namespaced$Namespace(c.GetNamespace()).$end$
@@ -659,8 +653,8 @@ func (c *$.type|privatePlural$) Get(ctx $.context|raw$, name string, options $.G
659653
`
660654

661655
var getSubresourceTemplate = `
662-
// Get takes name of the $.type|private$, and returns the corresponding $.resultType|raw$ object, and an error if there is any.
663-
func (c *$.type|privatePlural$) Get(ctx $.context|raw$, $.type|private$Name string, options $.GetOptions|raw$) (result *$.resultType|raw$, err error) {
656+
// $.verb$ takes name of the $.type|private$, and returns the corresponding $.resultType|raw$ object, and an error if there is any.
657+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.type|private$Name string, options $.GetOptions|raw$) (result *$.resultType|raw$, err error) {
664658
result = &$.resultType|raw${}
665659
err = c.GetClient().Get().
666660
$if .namespaced$Namespace(c.GetNamespace()).$end$
@@ -675,8 +669,8 @@ func (c *$.type|privatePlural$) Get(ctx $.context|raw$, $.type|private$Name stri
675669
`
676670

677671
var deleteTemplate = `
678-
// Delete takes name of the $.type|private$ and deletes it. Returns an error if one occurs.
679-
func (c *$.type|privatePlural$) Delete(ctx $.context|raw$, name string, opts $.DeleteOptions|raw$) error {
672+
// $.verb$ takes name of the $.type|private$ and deletes it. Returns an error if one occurs.
673+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, name string, opts $.DeleteOptions|raw$) error {
680674
return c.GetClient().Delete().
681675
$if .namespaced$Namespace(c.GetNamespace()).$end$
682676
Resource("$.type|resource$").
@@ -688,8 +682,8 @@ func (c *$.type|privatePlural$) Delete(ctx $.context|raw$, name string, opts $.D
688682
`
689683

690684
var createSubresourceTemplate = `
691-
// Create takes the representation of a $.inputType|private$ and creates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
692-
func (c *$.type|privatePlural$) Create(ctx $.context|raw$, $.type|private$Name string, $.inputType|private$ *$.inputType|raw$, opts $.CreateOptions|raw$) (result *$.resultType|raw$, err error) {
685+
// $.verb$ takes the representation of a $.inputType|private$ and creates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
686+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.type|private$Name string, $.inputType|private$ *$.inputType|raw$, opts $.CreateOptions|raw$) (result *$.resultType|raw$, err error) {
693687
result = &$.resultType|raw${}
694688
err = c.GetClient().Post().
695689
$if .namespaced$Namespace(c.GetNamespace()).$end$
@@ -705,8 +699,8 @@ func (c *$.type|privatePlural$) Create(ctx $.context|raw$, $.type|private$Name s
705699
`
706700

707701
var createTemplate = `
708-
// Create takes the representation of a $.inputType|private$ and creates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
709-
func (c *$.type|privatePlural$) Create(ctx $.context|raw$, $.inputType|private$ *$.inputType|raw$, opts $.CreateOptions|raw$) (result *$.resultType|raw$, err error) {
702+
// $.verb$ takes the representation of a $.inputType|private$ and creates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
703+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.inputType|private$ *$.inputType|raw$, opts $.CreateOptions|raw$) (result *$.resultType|raw$, err error) {
710704
result = &$.resultType|raw${}
711705
err = c.GetClient().Post().
712706
$if .namespaced$Namespace(c.GetNamespace()).$end$
@@ -720,8 +714,8 @@ func (c *$.type|privatePlural$) Create(ctx $.context|raw$, $.inputType|private$
720714
`
721715

722716
var updateSubresourceTemplate = `
723-
// Update takes the top resource name and the representation of a $.inputType|private$ and updates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
724-
func (c *$.type|privatePlural$) Update(ctx $.context|raw$, $.type|private$Name string, $.inputType|private$ *$.inputType|raw$, opts $.UpdateOptions|raw$) (result *$.resultType|raw$, err error) {
717+
// $.verb$ takes the top resource name and the representation of a $.inputType|private$ and updates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
718+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.type|private$Name string, $.inputType|private$ *$.inputType|raw$, opts $.UpdateOptions|raw$) (result *$.resultType|raw$, err error) {
725719
result = &$.resultType|raw${}
726720
err = c.GetClient().Put().
727721
$if .namespaced$Namespace(c.GetNamespace()).$end$
@@ -737,8 +731,8 @@ func (c *$.type|privatePlural$) Update(ctx $.context|raw$, $.type|private$Name s
737731
`
738732

739733
var updateTemplate = `
740-
// Update takes the representation of a $.inputType|private$ and updates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
741-
func (c *$.type|privatePlural$) Update(ctx $.context|raw$, $.inputType|private$ *$.inputType|raw$, opts $.UpdateOptions|raw$) (result *$.resultType|raw$, err error) {
734+
// $.verb$ takes the representation of a $.inputType|private$ and updates it. Returns the server's representation of the $.resultType|private$, and an error, if there is any.
735+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.inputType|private$ *$.inputType|raw$, opts $.UpdateOptions|raw$) (result *$.resultType|raw$, err error) {
742736
result = &$.resultType|raw${}
743737
err = c.GetClient().Put().
744738
$if .namespaced$Namespace(c.GetNamespace()).$end$
@@ -753,8 +747,8 @@ func (c *$.type|privatePlural$) Update(ctx $.context|raw$, $.inputType|private$
753747
`
754748

755749
var watchTemplate = `
756-
// Watch returns a $.watchInterface|raw$ that watches the requested $.type|privatePlural$.
757-
func (c *$.type|privatePlural$) Watch(ctx $.context|raw$, opts $.ListOptions|raw$) ($.watchInterface|raw$, error) {
750+
// $.verb$ returns a $.watchInterface|raw$ that watches the requested $.type|privatePlural$.
751+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, opts $.ListOptions|raw$) ($.watchInterface|raw$, error) {
758752
var timeout time.Duration
759753
if opts.TimeoutSeconds != nil{
760754
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
@@ -788,8 +782,8 @@ func (c *$.type|privatePlural$) watchList(ctx $.context|raw$, opts $.ListOptions
788782
`
789783

790784
var patchTemplate = `
791-
// Patch applies the patch and returns the patched $.resultType|private$.
792-
func (c *$.type|privatePlural$) Patch(ctx $.context|raw$, name string, pt $.PatchType|raw$, data []byte, opts $.PatchOptions|raw$, subresources ...string) (result *$.resultType|raw$, err error) {
785+
// $.verb$ applies the patch and returns the patched $.resultType|private$.
786+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, name string, pt $.PatchType|raw$, data []byte, opts $.PatchOptions|raw$, subresources ...string) (result *$.resultType|raw$, err error) {
793787
result = &$.resultType|raw${}
794788
err = c.GetClient().Patch(pt).
795789
$if .namespaced$Namespace(c.GetNamespace()).$end$
@@ -805,10 +799,10 @@ func (c *$.type|privatePlural$) Patch(ctx $.context|raw$, name string, pt $.Patc
805799
`
806800

807801
var applyTemplate = `
808-
// Apply takes the given apply declarative configuration, applies it and returns the applied $.resultType|private$.
809-
func (c *$.type|privatePlural$) Apply(ctx $.context|raw$, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
802+
// $.verb$ takes the given apply declarative configuration, applies it and returns the applied $.resultType|private$.
803+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
810804
if $.inputType|private$ == nil {
811-
return nil, $.fmtErrorf|raw$("$.inputType|private$ provided to Apply must not be nil")
805+
return nil, $.fmtErrorf|raw$("$.inputType|private$ provided to $.verb$ must not be nil")
812806
}
813807
patchOpts := opts.ToPatchOptions()
814808
data, err := $.jsonMarshal|raw$($.inputType|private$)
@@ -817,7 +811,7 @@ func (c *$.type|privatePlural$) Apply(ctx $.context|raw$, $.inputType|private$ *
817811
}
818812
name := $.inputType|private$.Name
819813
if name == nil {
820-
return nil, $.fmtErrorf|raw$("$.inputType|private$.Name must be provided to Apply")
814+
return nil, $.fmtErrorf|raw$("$.inputType|private$.Name must be provided to $.verb$")
821815
}
822816
result = &$.resultType|raw${}
823817
err = c.GetClient().Patch($.ApplyPatchType|raw$).
@@ -833,11 +827,11 @@ func (c *$.type|privatePlural$) Apply(ctx $.context|raw$, $.inputType|private$ *
833827
`
834828

835829
var applySubresourceTemplate = `
836-
// Apply takes top resource name and the apply declarative configuration for $.subresourcePath$,
830+
// $.verb$ takes top resource name and the apply declarative configuration for $.subresourcePath$,
837831
// applies it and returns the applied $.resultType|private$, and an error, if there is any.
838-
func (c *$.type|privatePlural$) Apply(ctx $.context|raw$, $.type|private$Name string, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
832+
func (c *$.type|privatePlural$) $.verb$(ctx $.context|raw$, $.type|private$Name string, $.inputType|private$ *$.inputApplyConfig|raw$, opts $.ApplyOptions|raw$) (result *$.resultType|raw$, err error) {
839833
if $.inputType|private$ == nil {
840-
return nil, $.fmtErrorf|raw$("$.inputType|private$ provided to Apply must not be nil")
834+
return nil, $.fmtErrorf|raw$("$.inputType|private$ provided to $.verb$ must not be nil")
841835
}
842836
patchOpts := opts.ToPatchOptions()
843837
data, err := $.jsonMarshal|raw$($.inputType|private$)

0 commit comments

Comments
 (0)