Chore: Hacky first pass on generating composable kinds within grafana/schema (#64723)

* Super hacky first pass on gen types

* First sketchy pass at generated compo kinds in TS, grok-style

* Merge fix

* Create jenny for MajorsOrX for plugins

* Re-generate files with imports

* Delete invalid generated file

* Fix cue

* Update go.mod

* Update schemas

* Fix go-imports

* Regenerate cue files

---------

Co-authored-by: spinillos <selenepinillos@gmail.com>
This commit is contained in:
sam boyer
2023-05-30 08:56:18 -04:00
committed by GitHub
parent 12dc56ad0c
commit 09895c26b6
77 changed files with 6052 additions and 33 deletions

View File

@@ -0,0 +1,72 @@
package codegen
import (
"path/filepath"
"github.com/grafana/codejen"
tsast "github.com/grafana/cuetsy/ts/ast"
corecodegen "github.com/grafana/grafana/pkg/codegen"
"github.com/grafana/grafana/pkg/cuectx"
"github.com/grafana/grafana/pkg/plugins/pfs"
"github.com/grafana/kindsys"
"github.com/grafana/thema"
)
func PluginTSEachMajor(rt *thema.Runtime) codejen.OneToMany[*pfs.PluginDecl] {
latestMajorsOrX := corecodegen.LatestMajorsOrXJenny(filepath.Join("packages", "grafana-schema", "src", "raw", "composable"), false, corecodegen.TSTypesJenny{})
return &pleJenny{
inner: kinds2pd(rt, latestMajorsOrX),
}
}
type pleJenny struct {
inner codejen.OneToMany[*pfs.PluginDecl]
}
func (*pleJenny) JennyName() string {
return "PluginEachMajorJenny"
}
func (j *pleJenny) Generate(decl *pfs.PluginDecl) (codejen.Files, error) {
if !decl.HasSchema() {
return nil, nil
}
jf, err := j.inner.Generate(decl)
if err != nil {
return nil, err
}
files := make(codejen.Files, len(jf))
for i, file := range jf {
tsf := &tsast.File{}
for _, im := range decl.Imports {
if tsim, err := cuectx.ConvertImport(im); err != nil {
return nil, err
} else if tsim.From.Value != "" {
tsf.Imports = append(tsf.Imports, tsim)
}
}
tsf.Nodes = append(tsf.Nodes, tsast.Raw{
Data: string(file.Data),
})
data := []byte(tsf.String())
data = data[:len(data)-1] // remove the additional line break added by the inner jenny
files[i] = *codejen.NewFile(file.RelativePath, data, append(file.From, j)...)
}
return files, nil
}
func kinds2pd(rt *thema.Runtime, j codejen.OneToMany[kindsys.Kind]) codejen.OneToMany[*pfs.PluginDecl] {
return codejen.AdaptOneToMany(j, func(pd *pfs.PluginDecl) kindsys.Kind {
kd, err := kindsys.BindComposable(rt, pd.KindDecl)
if err != nil {
return nil
}
return kd
})
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/grafana/codejen"
tsast "github.com/grafana/cuetsy/ts/ast"
"github.com/grafana/grafana/pkg/cuectx"
"github.com/grafana/grafana/pkg/plugins/pfs"
)
@@ -34,7 +35,7 @@ func (j *ptsJenny) Generate(decl *pfs.PluginDecl) (*codejen.File, error) {
tsf := &tsast.File{}
for _, im := range decl.Imports {
if tsim, err := convertImport(im); err != nil {
if tsim, err := cuectx.ConvertImport(im); err != nil {
return nil, err
} else if tsim.From.Value != "" {
tsf.Imports = append(tsf.Imports, tsim)

View File

@@ -1,77 +0,0 @@
package codegen
import (
"fmt"
"sort"
"strings"
"cuelang.org/go/cue/ast"
tsast "github.com/grafana/cuetsy/ts/ast"
"github.com/grafana/grafana/pkg/plugins/pfs"
)
// CUE import paths, mapped to corresponding TS import paths. An empty value
// indicates the import path should be dropped in the conversion to TS. Imports
// not present in the list are not not allowed, and code generation will fail.
var importMap = map[string]string{
"github.com/grafana/thema": "",
"github.com/grafana/kindsys": "",
"github.com/grafana/grafana/pkg/plugins/pfs": "",
"github.com/grafana/grafana/packages/grafana-schema/src/common": "@grafana/schema",
}
func init() {
allow := pfs.PermittedCUEImports()
strsl := make([]string, 0, len(importMap))
for p := range importMap {
strsl = append(strsl, p)
}
sort.Strings(strsl)
sort.Strings(allow)
if strings.Join(strsl, "") != strings.Join(allow, "") {
panic("CUE import map is not the same as permitted CUE import list - these files must be kept in sync!")
}
}
// mapCUEImportToTS maps the provided CUE import path to the corresponding
// TypeScript import path in generated code.
//
// Providing an import path that is not allowed results in an error. If a nil
// error and empty string are returned, the import path should be dropped in
// generated code.
func mapCUEImportToTS(path string) (string, error) {
i, has := importMap[path]
if !has {
return "", fmt.Errorf("import %q in models.cue is not allowed", path)
}
return i, nil
}
// TODO convert this to use cuetsy ts types, once import * form is supported
func convertImport(im *ast.ImportSpec) (tsast.ImportSpec, error) {
tsim := tsast.ImportSpec{}
pkg, err := mapCUEImportToTS(strings.Trim(im.Path.Value, "\""))
if err != nil || pkg == "" {
// err should be unreachable if paths has been verified already
// Empty string mapping means skip it
return tsim, err
}
tsim.From = tsast.Str{Value: pkg}
if im.Name != nil && im.Name.String() != "" {
tsim.AsName = im.Name.String()
} else {
sl := strings.Split(strings.Trim(im.Path.Value, "\""), "/")
final := sl[len(sl)-1]
if idx := strings.Index(final, ":"); idx != -1 {
tsim.AsName = final[idx:]
} else {
tsim.AsName = final
}
}
return tsim, nil
}

View File

@@ -53,16 +53,7 @@ func loadGP(ctx *cue.Context) cue.Value {
// PermittedCUEImports returns the list of import paths that may be used in a
// plugin's grafanaplugin cue package.
//
// TODO probably move this into kindsys
func PermittedCUEImports() []string {
return []string{
"github.com/grafana/thema",
"github.com/grafana/kindsys",
"github.com/grafana/grafana/pkg/plugins/pfs",
"github.com/grafana/grafana/packages/grafana-schema/src/common",
}
}
var PermittedCUEImports = cuectx.PermittedCUEImports
func importAllowed(path string) bool {
for _, p := range PermittedCUEImports() {