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] Generated Copy Code #424

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion cmd/grafana-app-sdk/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Allowed values are 'group' and 'kind'. Dictates the packaging of go kinds, where
generateCmd.Flags().Lookup("postprocess").NoOptDefVal = "true"
generateCmd.Flags().Bool("nomanifest", false, "Whether to disable generating the app manifest")
generateCmd.Flags().Lookup("nomanifest").NoOptDefVal = "true"
generateCmd.Flags().Bool("simplecopy", false, "Governs whether the generated go resource.Object implementations use a generated deep copy method, or the reflection-based resource.CopyObject. Set this to true if you are having problems with the generated deep copy code.")
generateCmd.Flags().Lookup("simplecopy").NoOptDefVal = "true"

// Don't show "usage" information when an error is returned form the command,
// because our errors are not command-usage-based
Expand Down Expand Up @@ -112,6 +114,10 @@ func generateCmdFunc(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
simpleCopy, err := cmd.Flags().GetBool("simplecopy")
if err != nil {
return err
}

var files codejen.Files
switch format {
Expand All @@ -136,6 +142,7 @@ func generateCmdFunc(cmd *cobra.Command, _ []string) error {
CRDPath: crdPath,
GroupKinds: grouping == kindGroupingGroup,
GenerateManifest: !noManifest,
GenericCopy: simpleCopy,
}, selectors...)
if err != nil {
return err
Expand Down Expand Up @@ -190,6 +197,7 @@ type kindGenConfig struct {
CRDPath string
GroupKinds bool
GenerateManifest bool
GenericCopy bool
}

//nolint:goconst
Expand Down Expand Up @@ -317,7 +325,7 @@ func generateKindsCue(modFS fs.FS, cfg kindGenConfig, selectors ...string) (code
return nil, err
}
// Resource
resourceFiles, err := generator.FilteredGenerate(cuekind.ResourceGenerator(true, cfg.GroupKinds), func(kind codegen.Kind) bool {
resourceFiles, err := generator.FilteredGenerate(cuekind.ResourceGenerator(true, cfg.GroupKinds, cfg.GenericCopy), func(kind codegen.Kind) bool {
return kind.Properties().APIResource != nil
}, selectors...)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion codegen/cuekind/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func CRDGenerator(outputEncoder jennies.CRDOutputEncoder, outputExtension string
// If `groupKinds` is true, kinds within the same group will exist in the same package.
// When combined with `versioned`, each version package will contain all kinds in the group
// which have a schema for that version.
func ResourceGenerator(versioned bool, groupKinds bool) *codejen.JennyList[codegen.Kind] {
func ResourceGenerator(versioned bool, groupKinds bool, genericCopy bool) *codejen.JennyList[codegen.Kind] {
g := codejen.JennyListWithNamer(namerFunc)
g.Append(
&jennies.GoTypes{
Expand All @@ -36,6 +36,7 @@ func ResourceGenerator(versioned bool, groupKinds bool) *codejen.JennyList[codeg
OnlyUseCurrentVersion: !versioned,
SubresourceTypesArePrefixed: groupKinds,
GroupByKind: !groupKinds,
GenericCopy: genericCopy,
},
&jennies.SchemaGenerator{
OnlyUseCurrentVersion: !versioned,
Expand Down
8 changes: 4 additions & 4 deletions codegen/cuekind/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestResourceGenerator(t *testing.T) {
require.Nil(t, err)

t.Run("unversioned", func(t *testing.T) {
files, err := ResourceGenerator(false, false).Generate(kinds...)
files, err := ResourceGenerator(false, false, true).Generate(kinds...)
require.Nil(t, err)
// Check number of files generated
// 6 -> object, spec, metadata, status, schema, codec
Expand All @@ -68,7 +68,7 @@ func TestResourceGenerator(t *testing.T) {
})

t.Run("group by kind", func(t *testing.T) {
files, err := ResourceGenerator(true, false).Generate(kinds...)
files, err := ResourceGenerator(true, false, false).Generate(kinds...)
require.Nil(t, err)
// Check number of files generated
// 12 (6 -> object, spec, metadata, status, schema, codec) * 2 versions
Expand All @@ -78,7 +78,7 @@ func TestResourceGenerator(t *testing.T) {
})

t.Run("group by group", func(t *testing.T) {
files, err := ResourceGenerator(true, true).Generate(kinds...)
files, err := ResourceGenerator(true, true, false).Generate(kinds...)
require.Nil(t, err)
// Check number of files generated
// 12 (6 -> object, spec, metadata, status, schema, codec) * 2 versions
Expand All @@ -88,7 +88,7 @@ func TestResourceGenerator(t *testing.T) {
})

t.Run("group by group, multiple kinds", func(t *testing.T) {
files, err := ResourceGenerator(true, true).Generate(sameGroupKinds...)
files, err := ResourceGenerator(true, true, false).Generate(sameGroupKinds...)
require.Nil(t, err)
// Check number of files generated
assert.Len(t, files, 18)
Expand Down
2 changes: 1 addition & 1 deletion codegen/jennies/gotypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func GoTypesFromCUE(v cue.Value, cfg CUEGoConfig, maxNamingDepth int) ([]byte, e
if i > 0 {
path = cue.MakePath(path.Selectors()[i:]...)
}
return cfg.NamePrefix + strings.Trim(path.String(), "?#")
return cfg.NamePrefix + exportField(strings.Trim(path.String(), "?#"))
},
}

Expand Down
Loading
Loading