Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[codegen] Generate exported constants for group and version. #563

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions codegen/cuekind/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func ResourceGenerator(groupKinds bool) *codejen.JennyList[codegen.Kind] {
&jennies.CodecGenerator{
GroupByKind: !groupKinds,
},
&jennies.Constants{
GroupByKind: !groupKinds,
},
)
return g
}
Expand Down
10 changes: 5 additions & 5 deletions codegen/cuekind/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func TestResourceGenerator(t *testing.T) {
files, err := ResourceGenerator(false).Generate(kinds...)
require.Nil(t, err)
// Check number of files generated
// 12 (6 -> object, spec, metadata, status, schema, codec) * 2 versions
assert.Len(t, files, 12)
// 14 (6 -> object, spec, metadata, status, schema, codec, constants) * 2 versions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 14 (6 -> object, spec, metadata, status, schema, codec, constants) * 2 versions
// 14 (7 -> object, spec, metadata, status, schema, codec, constants) * 2 versions

assert.Len(t, files, 14, "should be 14 files generated, got %d", len(files))
// Check content against the golden files
compareToGolden(t, files, "go/groupbykind")
})
Expand All @@ -70,8 +70,8 @@ func TestResourceGenerator(t *testing.T) {
files, err := ResourceGenerator(true).Generate(kinds...)
require.Nil(t, err)
// Check number of files generated
// 12 (6 -> object, spec, metadata, status, schema, codec) * 2 versions
assert.Len(t, files, 12)
// 14 (6 -> object, spec, metadata, status, schema, codec, constants) * 2 versions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 14 (6 -> object, spec, metadata, status, schema, codec, constants) * 2 versions
// 14 (7 -> object, spec, metadata, status, schema, codec, constants) * 2 versions

assert.Len(t, files, 14, "should be 14 files generated, got %d", len(files))
// Check content against the golden files
compareToGolden(t, files, "go/groupbygroup")
})
Expand All @@ -80,7 +80,7 @@ func TestResourceGenerator(t *testing.T) {
files, err := ResourceGenerator(true).Generate(sameGroupKinds...)
require.Nil(t, err)
// Check number of files generated
assert.Len(t, files, 18)
assert.Len(t, files, 20, "should be 20 files generated, got %d", len(files))
// Check content against the golden files
compareToGolden(t, files, "go/groupbygroup")
})
Expand Down
66 changes: 66 additions & 0 deletions codegen/jennies/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package jennies

import (
"bytes"
"go/format"
"path/filepath"

"github.com/grafana/codejen"

Check failure on line 8 in codegen/jennies/constants.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/grafana/grafana-app-sdk (goimports)
"github.com/grafana/grafana-app-sdk/codegen"
"github.com/grafana/grafana-app-sdk/codegen/templates"
)

// Constants is a Jenny which creates package-wide exported constants
type Constants struct {
// GroupByKind determines whether kinds are grouped by GroupVersionKind or just GroupVersion.
// If GroupByKind is true, generated paths are <kind>/<version>/<file>, instead of the default <version>/<file>.
GroupByKind bool
}

func (c *Constants) JennyName() string {

Check warning on line 20 in codegen/jennies/constants.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'c' is not referenced in method's body, consider removing or renaming it as _ (revive)
return "ConstantsGenerator"
}

type constantsFileParams struct {
group string
version string
path string
}

func (c *Constants) Generate(kinds ...codegen.Kind) (codejen.Files, error) {
m := make(map[string]constantsFileParams)
for _, k := range kinds {
for _, v := range k.Versions() {
path := GetGeneratedPath(c.GroupByKind, k, v.Version)
if _, ok := m[path]; !ok {
m[path] = constantsFileParams{
group: k.Properties().Group,
version: v.Version,
path: filepath.Join(path, "constants.go"),
}
}
}
}
files := make(codejen.Files, 0)
for _, v := range m {
b := bytes.Buffer{}
err := templates.WriteConstantsFile(templates.ConstantsMetadata{
Package: ToPackageName(v.version),
Group: v.group,
Version: v.version,
}, &b)
if err != nil {
return nil, err
}
formatted, err := format.Source(b.Bytes())
if err != nil {
return nil, err
}
files = append(files, codejen.File{
RelativePath: v.path,
From: []codejen.NamedJenny{c},
Data: formatted,
})
}
return files, nil
}
18 changes: 18 additions & 0 deletions codegen/templates/constants.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package {{ .Package }}

import "k8s.io/apimachinery/pkg/runtime/schema"

const (
// Group is the API group used by all kinds in this package
Group = "{{ .Group }}"
// Version is the API version used by all kinds in this package
Version = "{{ .Version }}"
)

var (
// GroupVersion is a schema.GroupVersion consisting of the Group and Version constants for this package
GroupVersion = schema.GroupVersion{
Group: Group,
Version: Version,
}
)
11 changes: 11 additions & 0 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
templateThemaCodec, _ = template.ParseFS(templates, "themacodec.tmpl")
templateWrappedType, _ = template.ParseFS(templates, "wrappedtype.tmpl")
templateTSType, _ = template.ParseFS(templates, "tstype.tmpl")
templateConstants, _ = template.ParseFS(templates, "constants.tmpl")

templateBackendPluginRouter, _ = template.ParseFS(templates, "plugin/plugin.tmpl")
templateBackendPluginResourceHandler, _ = template.ParseFS(templates, "plugin/handler_resource.tmpl")
Expand Down Expand Up @@ -468,6 +469,16 @@ func WriteAppGoFile(metadata AppMetadata, out io.Writer) error {
return templateApp.Execute(out, md)
}

type ConstantsMetadata struct {
Package string
Group string
Version string
}

func WriteConstantsFile(metadata ConstantsMetadata, out io.Writer) error {
return templateConstants.Execute(out, metadata)
}

// ToPackageName sanitizes an input into a deterministic allowed go package name.
// It is used to turn kind names or versions into package names when performing go code generation.
func ToPackageName(input string) string {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v0_0

import "k8s.io/apimachinery/pkg/runtime/schema"

const (
// Group is the API group used by all kinds in this package
Group = "customapp.ext.grafana.com"
// Version is the API version used by all kinds in this package
Version = "v0-0"
)

var (
// GroupVersion is a schema.GroupVersion consisting of the Group and Version constants for this package
GroupVersion = schema.GroupVersion{
Group: Group,
Version: Version,
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v1_0

import "k8s.io/apimachinery/pkg/runtime/schema"

const (
// Group is the API group used by all kinds in this package
Group = "customapp.ext.grafana.com"
// Version is the API version used by all kinds in this package
Version = "v1-0"
)

var (
// GroupVersion is a schema.GroupVersion consisting of the Group and Version constants for this package
GroupVersion = schema.GroupVersion{
Group: Group,
Version: Version,
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v1

import "k8s.io/apimachinery/pkg/runtime/schema"

const (
// Group is the API group used by all kinds in this package
Group = "testapp.ext.grafana.com"
// Version is the API version used by all kinds in this package
Version = "v1"
)

var (
// GroupVersion is a schema.GroupVersion consisting of the Group and Version constants for this package
GroupVersion = schema.GroupVersion{
Group: Group,
Version: Version,
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v2

import "k8s.io/apimachinery/pkg/runtime/schema"

const (
// Group is the API group used by all kinds in this package
Group = "testapp.ext.grafana.com"
// Version is the API version used by all kinds in this package
Version = "v2"
)

var (
// GroupVersion is a schema.GroupVersion consisting of the Group and Version constants for this package
GroupVersion = schema.GroupVersion{
Group: Group,
Version: Version,
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v0_0

import "k8s.io/apimachinery/pkg/runtime/schema"

const (
// Group is the API group used by all kinds in this package
Group = "customapp.ext.grafana.com"
// Version is the API version used by all kinds in this package
Version = "v0-0"
)

var (
// GroupVersion is a schema.GroupVersion consisting of the Group and Version constants for this package
GroupVersion = schema.GroupVersion{
Group: Group,
Version: Version,
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v1_0

import "k8s.io/apimachinery/pkg/runtime/schema"

const (
// Group is the API group used by all kinds in this package
Group = "customapp.ext.grafana.com"
// Version is the API version used by all kinds in this package
Version = "v1-0"
)

var (
// GroupVersion is a schema.GroupVersion consisting of the Group and Version constants for this package
GroupVersion = schema.GroupVersion{
Group: Group,
Version: Version,
}
)
Loading