Skip to content

Commit

Permalink
Codegen: Parse the gvk once (#399)
Browse files Browse the repository at this point in the history
minor PR mostly to see if I can do it :)

Rather than parsing and creating a GVK for each property, this gets it
once and shares the results.
  • Loading branch information
ryantxu authored Sep 17, 2024
1 parent 8ee5fae commit f8e2175
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 28 deletions.
7 changes: 4 additions & 3 deletions codegen/templates/resourceobject.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ func ({{.ObjectShortName}} *{{.TypeName}}) SetSubresource(name string, value any
}

func ({{.ObjectShortName}} *{{.TypeName}}) GetStaticMetadata() resource.StaticMetadata {
gvk := {{.ObjectShortName}}.GroupVersionKind()
return resource.StaticMetadata{
Name: {{.ObjectShortName}}.ObjectMeta.Name,
Namespace: {{.ObjectShortName}}.ObjectMeta.Namespace,
Group: {{.ObjectShortName}}.GroupVersionKind().Group,
Version: {{.ObjectShortName}}.GroupVersionKind().Version,
Kind: {{.ObjectShortName}}.GroupVersionKind().Kind,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
}
}

Expand Down
5 changes: 4 additions & 1 deletion codegen/testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

The contents of [golden_generated] are expected output for inputs of `customKind` and `customKind2`.
The way these objects can be defined depends on the parser, but any shared output (for example, go types)
should always be the same regardless of parser, as they use the same shared jennies.
should always be the same regardless of parser, as they use the same shared jennies.

There is no helpful utility to re-create the golden files. Either manually update them or setup a parallel
project and copy the cue files manually.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func (o *CustomKind) SetSubresource(name string, value any) error {
}

func (o *CustomKind) GetStaticMetadata() resource.StaticMetadata {
gvk := o.GroupVersionKind()
return resource.StaticMetadata{
Name: o.ObjectMeta.Name,
Namespace: o.ObjectMeta.Namespace,
Group: o.GroupVersionKind().Group,
Version: o.GroupVersionKind().Version,
Kind: o.GroupVersionKind().Kind,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func (o *CustomKind) SetSubresource(name string, value any) error {
}

func (o *CustomKind) GetStaticMetadata() resource.StaticMetadata {
gvk := o.GroupVersionKind()
return resource.StaticMetadata{
Name: o.ObjectMeta.Name,
Namespace: o.ObjectMeta.Namespace,
Group: o.GroupVersionKind().Group,
Version: o.GroupVersionKind().Version,
Kind: o.GroupVersionKind().Kind,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func (o *TestKind2) SetSubresource(name string, value any) error {
}

func (o *TestKind2) GetStaticMetadata() resource.StaticMetadata {
gvk := o.GroupVersionKind()
return resource.StaticMetadata{
Name: o.ObjectMeta.Name,
Namespace: o.ObjectMeta.Namespace,
Group: o.GroupVersionKind().Group,
Version: o.GroupVersionKind().Version,
Kind: o.GroupVersionKind().Kind,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func (o *TestKind) SetSubresource(name string, value any) error {
}

func (o *TestKind) GetStaticMetadata() resource.StaticMetadata {
gvk := o.GroupVersionKind()
return resource.StaticMetadata{
Name: o.ObjectMeta.Name,
Namespace: o.ObjectMeta.Namespace,
Group: o.GroupVersionKind().Group,
Version: o.GroupVersionKind().Version,
Kind: o.GroupVersionKind().Kind,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func (o *TestKind) SetSubresource(name string, value any) error {
}

func (o *TestKind) GetStaticMetadata() resource.StaticMetadata {
gvk := o.GroupVersionKind()
return resource.StaticMetadata{
Name: o.ObjectMeta.Name,
Namespace: o.ObjectMeta.Namespace,
Group: o.GroupVersionKind().Group,
Version: o.GroupVersionKind().Version,
Kind: o.GroupVersionKind().Kind,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func (o *CustomKind) SetSubresource(name string, value any) error {
}

func (o *CustomKind) GetStaticMetadata() resource.StaticMetadata {
gvk := o.GroupVersionKind()
return resource.StaticMetadata{
Name: o.ObjectMeta.Name,
Namespace: o.ObjectMeta.Namespace,
Group: o.GroupVersionKind().Group,
Version: o.GroupVersionKind().Version,
Kind: o.GroupVersionKind().Kind,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func (o *CustomKind) SetSubresource(name string, value any) error {
}

func (o *CustomKind) GetStaticMetadata() resource.StaticMetadata {
gvk := o.GroupVersionKind()
return resource.StaticMetadata{
Name: o.ObjectMeta.Name,
Namespace: o.ObjectMeta.Namespace,
Group: o.GroupVersionKind().Group,
Version: o.GroupVersionKind().Version,
Kind: o.GroupVersionKind().Kind,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func (o *CustomKind) SetSubresource(name string, value any) error {
}

func (o *CustomKind) GetStaticMetadata() resource.StaticMetadata {
gvk := o.GroupVersionKind()
return resource.StaticMetadata{
Name: o.ObjectMeta.Name,
Namespace: o.ObjectMeta.Namespace,
Group: o.GroupVersionKind().Group,
Version: o.GroupVersionKind().Version,
Kind: o.GroupVersionKind().Kind,
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
}
}

Expand Down

0 comments on commit f8e2175

Please sign in to comment.