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] Fix Codegen for Optional selectableFields Values #409

Merged
merged 2 commits into from
Sep 17, 2024
Merged
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
1 change: 1 addition & 0 deletions cmd/grafana-app-sdk/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var generateCmd = &cobra.Command{
RunE: generateCmdFunc,
}

//nolint:goconst
func setupGenerateCmd() {
generateCmd.PersistentFlags().StringP("gogenpath", "g", "pkg/generated/",
"Path to directory where generated go code will reside")
Expand Down
59 changes: 55 additions & 4 deletions codegen/jennies/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"go/format"
"path/filepath"
"strings"

"cuelang.org/go/cue"
"github.com/grafana/codejen"

"github.com/grafana/grafana-app-sdk/codegen"
Expand Down Expand Up @@ -46,15 +48,19 @@ func (s *SchemaGenerator) Generate(kind codegen.Kind) (codejen.Files, error) {

files := make(codejen.Files, 0)
if s.OnlyUseCurrentVersion {
sf, err := s.getSelectableFields(kind.Version(meta.Current))
if err != nil {
return nil, err
}
b := bytes.Buffer{}
err := templates.WriteSchema(templates.SchemaMetadata{
err = templates.WriteSchema(templates.SchemaMetadata{
Package: meta.MachineName,
Group: meta.APIResource.Group,
Version: meta.Current,
Kind: meta.Kind,
Plural: meta.PluralMachineName,
Scope: meta.APIResource.Scope,
SelectableFields: kind.Version(meta.Current).SelectableFields,
SelectableFields: sf,
FuncPrefix: prefix,
}, &b)
if err != nil {
Expand All @@ -71,15 +77,19 @@ func (s *SchemaGenerator) Generate(kind codegen.Kind) (codejen.Files, error) {
})
} else {
for _, ver := range kind.Versions() {
sf, err := s.getSelectableFields(&ver)
if err != nil {
return nil, err
}
b := bytes.Buffer{}
err := templates.WriteSchema(templates.SchemaMetadata{
err = templates.WriteSchema(templates.SchemaMetadata{
Package: ToPackageName(ver.Version),
Group: meta.APIResource.Group,
Version: ver.Version,
Kind: meta.Kind,
Plural: meta.PluralMachineName,
Scope: meta.APIResource.Scope,
SelectableFields: ver.SelectableFields,
SelectableFields: sf,
FuncPrefix: prefix,
}, &b)
if err != nil {
Expand All @@ -99,3 +109,44 @@ func (s *SchemaGenerator) Generate(kind codegen.Kind) (codejen.Files, error) {

return files, nil
}

func (*SchemaGenerator) getSelectableFields(ver *codegen.KindVersion) ([]templates.SchemaMetadataSeletableField, error) {
fields := make([]templates.SchemaMetadataSeletableField, 0)
if len(ver.SelectableFields) == 0 {
return fields, nil
}
// Check each field in the CUE (TODO: make this OpenAPI instead?) to check if the field is optional
for _, s := range ver.SelectableFields {
fieldPath := s
if len(s) > 1 && s[0] == '.' {
fieldPath = s[1:]
}
parts := strings.Split(fieldPath, ".")
if len(parts) <= 1 {
return nil, fmt.Errorf("invalid selectable field path: %s", s)
}
field := parts[len(parts)-1]
parts = parts[:len(parts)-1]
path := make([]cue.Selector, 0)
for _, p := range parts {
path = append(path, cue.Str(p))
}
if val := ver.Schema.LookupPath(cue.MakePath(path...).Optional()); val.Err() == nil {
// Simplest way to check if it's an optional field is to try to look it up as non-optional, then try optional
if lookup := val.LookupPath(cue.MakePath(cue.Str(field))); lookup.Exists() {
fields = append(fields, templates.SchemaMetadataSeletableField{
Field: s,
Optional: false,
})
} else if optional := val.LookupPath(cue.MakePath(cue.Str(field).Optional())); optional.Exists() {
fields = append(fields, templates.SchemaMetadataSeletableField{
Field: s,
Optional: true,
})
} else {
return nil, fmt.Errorf("invalid selectable field path: %s", fieldPath)
}
}
}
return fields, nil
}
8 changes: 6 additions & 2 deletions codegen/templates/schema.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ import (
var (
schema{{.Kind}} = resource.NewSimpleSchema("{{.Group}}", "{{.Version}}", &{{.Kind}}{}, &{{.Kind}}List{}, resource.WithKind("{{.Kind}}"),
resource.WithPlural("{{.Plural}}"), resource.WithScope(resource.{{.Scope}}Scope) {{if gt $sfl 0}}, resource.WithSelectableFields([]resource.SelectableField{ {{ range .SelectableFields }}resource.SelectableField{
FieldSelector: "{{.}}",
FieldSelector: "{{.Field}}",
FieldValueFunc: func(o resource.Object) (string, error) {
cast, ok := o.(*{{$root.Kind}})
if !ok {
return "", fmt.Errorf("provided object must be of type *{{$root.Kind}}")
}{{ if .Optional }}
if cast.{{$root.ToObjectPath .Field}} == nil {
return "", nil
}
return cast.{{$root.ToObjectPath .}}, nil
return *cast.{{$root.ToObjectPath .Field}}, nil{{ else }}
return cast.{{$root.ToObjectPath .Field}}, nil{{ end }}
},
},
{{ end }} }){{ end }})
Expand Down
10 changes: 9 additions & 1 deletion codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,20 @@ type SchemaMetadata struct {
Kind string
Plural string
Scope string
SelectableFields []string
SelectableFields []SchemaMetadataSeletableField
FuncPrefix string
}

type SchemaMetadataSeletableField struct {
Field string
Optional bool
}

func (SchemaMetadata) ToObjectPath(s string) string {
parts := make([]string, 0)
if len(s) > 0 && s[0] == '.' {
s = s[1:]
}
for i, part := range strings.Split(s, ".") {
if i == 0 && part == "metadata" {
part = "ObjectMeta"
Expand Down
Loading