Schemas: Reduce duplicated jenny code (#84061)
* Remove jenny_ts_resources and use jenny_ts_types for all cases
* Unify TS generated files into one jenny
* Add missing imports to versioned files
* Update Parca plugin
* Fix loki
* Use LokiQuery
* Fix pyroscope tests
* Fix prettier
* 😒 fix default pyroscope value name
* Set the LokiQuery
* Update Elasticsearch and TestData
* Missed files from testdata
* Order imports
This commit is contained in:
@@ -11,22 +11,20 @@ import (
|
||||
// LatestMajorsOrXJenny returns a jenny that repeats the input for the latest in each major version.
|
||||
//
|
||||
// TODO remove forceGroup option, it's a temporary hack to accommodate core kinds
|
||||
func LatestMajorsOrXJenny(parentdir string, forceGroup bool, inner codejen.OneToOne[SchemaForGen]) OneToMany {
|
||||
func LatestMajorsOrXJenny(parentdir string, inner codejen.OneToOne[SchemaForGen]) OneToMany {
|
||||
if inner == nil {
|
||||
panic("inner jenny must not be nil")
|
||||
}
|
||||
|
||||
return &lmox{
|
||||
parentdir: parentdir,
|
||||
inner: inner,
|
||||
forceGroup: forceGroup,
|
||||
parentdir: parentdir,
|
||||
inner: inner,
|
||||
}
|
||||
}
|
||||
|
||||
type lmox struct {
|
||||
parentdir string
|
||||
inner codejen.OneToOne[SchemaForGen]
|
||||
forceGroup bool
|
||||
parentdir string
|
||||
inner codejen.OneToOne[SchemaForGen]
|
||||
}
|
||||
|
||||
func (j *lmox) JennyName() string {
|
||||
@@ -42,49 +40,19 @@ func (j *lmox) Generate(kind kindsys.Kind) (codejen.Files, error) {
|
||||
comm := kind.Props().Common()
|
||||
sfg := SchemaForGen{
|
||||
Name: comm.Name,
|
||||
IsGroup: comm.LineageIsGroup,
|
||||
IsGroup: true,
|
||||
Schema: kind.Lineage().Latest(),
|
||||
}
|
||||
|
||||
if j.forceGroup {
|
||||
sfg.IsGroup = true
|
||||
f, err := j.inner.Generate(sfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s jenny failed on %s schema for %s: %w", j.inner.JennyName(), sfg.Schema.Version(), kind.Props().Common().Name, err)
|
||||
}
|
||||
if f == nil || !f.Exists() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
do := func(sfg SchemaForGen, infix string) (codejen.Files, error) {
|
||||
f, err := j.inner.Generate(sfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s jenny failed on %s schema for %s: %w", j.inner.JennyName(), sfg.Schema.Version(), kind.Props().Common().Name, err)
|
||||
}
|
||||
if f == nil || !f.Exists() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
f.RelativePath = filepath.Join(j.parentdir, comm.MachineName, infix, f.RelativePath)
|
||||
f.From = append(f.From, j)
|
||||
return codejen.Files{*f}, nil
|
||||
}
|
||||
|
||||
if comm.Maturity.Less(kindsys.MaturityStable) {
|
||||
sfg.Schema = kind.Lineage().Latest()
|
||||
return do(sfg, "x")
|
||||
}
|
||||
|
||||
var fl codejen.Files
|
||||
major := -1
|
||||
for sch := kind.Lineage().First(); sch != nil; sch = sch.Successor() {
|
||||
if int(sch.Version()[0]) == major {
|
||||
continue
|
||||
}
|
||||
major = int(sch.Version()[0])
|
||||
|
||||
sfg.Schema = sch.LatestInMajor()
|
||||
files, err := do(sfg, fmt.Sprintf("v%v", sch.Version()[0]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fl = append(fl, files...)
|
||||
}
|
||||
if fl.Validate() != nil {
|
||||
return nil, fl.Validate()
|
||||
}
|
||||
return fl, nil
|
||||
f.RelativePath = filepath.Join(j.parentdir, comm.MachineName, "x", f.RelativePath)
|
||||
f.From = append(f.From, j)
|
||||
return codejen.Files{*f}, nil
|
||||
}
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
package codegen
|
||||
|
||||
import (
|
||||
"github.com/grafana/codejen"
|
||||
"github.com/grafana/cuetsy"
|
||||
"github.com/grafana/cuetsy/ts"
|
||||
"github.com/grafana/cuetsy/ts/ast"
|
||||
"github.com/grafana/grafana/pkg/cuectx"
|
||||
"github.com/grafana/thema/encoding/typescript"
|
||||
)
|
||||
|
||||
// TSResourceJenny is a [OneToOne] that produces TypeScript types and
|
||||
// defaults for a Thema schema.
|
||||
//
|
||||
// Thema's generic TS jenny will be able to replace this one once
|
||||
// https://github.com/grafana/thema/issues/89 is complete.
|
||||
type TSResourceJenny struct{}
|
||||
|
||||
var _ codejen.OneToOne[SchemaForGen] = &TSResourceJenny{}
|
||||
|
||||
func (j TSResourceJenny) JennyName() string {
|
||||
return "TSResourceJenny"
|
||||
}
|
||||
|
||||
func (j TSResourceJenny) Generate(sfg SchemaForGen) (*codejen.File, error) {
|
||||
// TODO allow using name instead of machine name in thema generator
|
||||
f, err := typescript.GenerateTypes(sfg.Schema, &typescript.TypeConfig{
|
||||
RootName: sfg.Name,
|
||||
Group: sfg.IsGroup,
|
||||
CuetsyConfig: &cuetsy.Config{
|
||||
Export: true,
|
||||
ImportMapper: cuectx.MapCUEImportToTS,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
renameSpecNode(sfg.Name, f)
|
||||
|
||||
return codejen.NewFile(sfg.Schema.Lineage().Name()+"_types.gen.ts", []byte(f.String()), j), nil
|
||||
}
|
||||
|
||||
func renameSpecNode(name string, tf *ast.File) {
|
||||
specidx, specdefidx := -1, -1
|
||||
for idx, def := range tf.Nodes {
|
||||
// Peer through export keywords
|
||||
if ex, is := def.(ast.ExportKeyword); is {
|
||||
def = ex.Decl
|
||||
}
|
||||
|
||||
switch x := def.(type) {
|
||||
case ast.TypeDecl:
|
||||
if x.Name.Name == "spec" {
|
||||
specidx = idx
|
||||
x.Name.Name = name
|
||||
tf.Nodes[idx] = x
|
||||
}
|
||||
case ast.VarDecl:
|
||||
// Before:
|
||||
// export const defaultspec: Partial<spec> = {
|
||||
// After:
|
||||
/// export const defaultPlaylist: Partial<Playlist> = {
|
||||
if x.Names.Idents[0].Name == "defaultspec" {
|
||||
specdefidx = idx
|
||||
x.Names.Idents[0].Name = "default" + name
|
||||
tt := x.Type.(ast.TypeTransformExpr)
|
||||
tt.Expr = ts.Ident(name)
|
||||
x.Type = tt
|
||||
tf.Nodes[idx] = x
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if specidx != -1 {
|
||||
decl := tf.Nodes[specidx]
|
||||
tf.Nodes = append(append(tf.Nodes[:specidx], tf.Nodes[specidx+1:]...), decl)
|
||||
}
|
||||
if specdefidx != -1 {
|
||||
if specdefidx > specidx {
|
||||
specdefidx--
|
||||
}
|
||||
decl := tf.Nodes[specdefidx]
|
||||
tf.Nodes = append(append(tf.Nodes[:specdefidx], tf.Nodes[specdefidx+1:]...), decl)
|
||||
}
|
||||
}
|
||||
@@ -3,16 +3,21 @@ package codegen
|
||||
import (
|
||||
"github.com/grafana/codejen"
|
||||
"github.com/grafana/cuetsy"
|
||||
"github.com/grafana/cuetsy/ts/ast"
|
||||
"github.com/grafana/grafana/pkg/cuectx"
|
||||
"github.com/grafana/thema/encoding/typescript"
|
||||
)
|
||||
|
||||
type ApplyFunc func(sfg SchemaForGen, file *ast.File)
|
||||
|
||||
// TSTypesJenny is a [OneToOne] that produces TypeScript types and
|
||||
// defaults for a Thema schema.
|
||||
//
|
||||
// Thema's generic TS jenny will be able to replace this one once
|
||||
// https://github.com/grafana/thema/issues/89 is complete.
|
||||
type TSTypesJenny struct{}
|
||||
type TSTypesJenny struct {
|
||||
ApplyFuncs []ApplyFunc
|
||||
}
|
||||
|
||||
var _ codejen.OneToOne[SchemaForGen] = &TSTypesJenny{}
|
||||
|
||||
@@ -30,6 +35,11 @@ func (j TSTypesJenny) Generate(sfg SchemaForGen) (*codejen.File, error) {
|
||||
RootName: sfg.Name,
|
||||
Group: sfg.IsGroup,
|
||||
})
|
||||
|
||||
for _, renameFunc := range j.ApplyFuncs {
|
||||
renameFunc(sfg, f)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
package codegen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/grafana/codejen"
|
||||
tsast "github.com/grafana/cuetsy/ts/ast"
|
||||
"github.com/grafana/grafana/pkg/build"
|
||||
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
|
||||
}
|
||||
|
||||
version := "export const pluginVersion = \"%s\";"
|
||||
if decl.PluginMeta.Version != nil {
|
||||
version = fmt.Sprintf(version, *decl.PluginMeta.Version)
|
||||
} else {
|
||||
version = fmt.Sprintf(version, getGrafanaVersion())
|
||||
}
|
||||
|
||||
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: version,
|
||||
})
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
func getGrafanaVersion() string {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
pkg, err := build.OpenPackageJSON(path.Join(dir, "../../../"))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return pkg.Version
|
||||
}
|
||||
@@ -2,19 +2,25 @@ package codegen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/codejen"
|
||||
tsast "github.com/grafana/cuetsy/ts/ast"
|
||||
"github.com/grafana/grafana/pkg/build"
|
||||
"github.com/grafana/grafana/pkg/codegen"
|
||||
"github.com/grafana/grafana/pkg/cuectx"
|
||||
"github.com/grafana/grafana/pkg/plugins/pfs"
|
||||
)
|
||||
|
||||
func PluginTSTypesJenny(root string, inner codejen.OneToOne[*pfs.PluginDecl]) codejen.OneToOne[*pfs.PluginDecl] {
|
||||
var versionedPluginPath = filepath.Join("packages", "grafana-schema", "src", "raw", "composable")
|
||||
|
||||
func PluginTSTypesJenny(root string) codejen.OneToMany[*pfs.PluginDecl] {
|
||||
return &ptsJenny{
|
||||
root: root,
|
||||
inner: inner,
|
||||
inner: adaptToPipeline(codegen.TSTypesJenny{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,21 +30,23 @@ type ptsJenny struct {
|
||||
}
|
||||
|
||||
func (j *ptsJenny) JennyName() string {
|
||||
return "PluginTSTypesJenny"
|
||||
return "PluginTsTypesJenny"
|
||||
}
|
||||
|
||||
func (j *ptsJenny) Generate(decl *pfs.PluginDecl) (*codejen.File, error) {
|
||||
func (j *ptsJenny) Generate(decl *pfs.PluginDecl) (codejen.Files, error) {
|
||||
if !decl.HasSchema() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
tsf := &tsast.File{}
|
||||
genFile := &tsast.File{}
|
||||
versionedFile := &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)
|
||||
genFile.Imports = append(genFile.Imports, tsim)
|
||||
versionedFile.Imports = append(versionedFile.Imports, tsim)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,13 +55,67 @@ func (j *ptsJenny) Generate(decl *pfs.PluginDecl) (*codejen.File, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tsf.Nodes = append(tsf.Nodes, tsast.Raw{
|
||||
Data: string(jf.Data),
|
||||
})
|
||||
rawData := tsast.Raw{Data: string(jf.Data)}
|
||||
rawVersion := tsast.Raw{
|
||||
Data: getPluginVersion(decl.PluginMeta.Version),
|
||||
}
|
||||
|
||||
path := filepath.Join(j.root, decl.PluginPath, fmt.Sprintf("%s.gen.ts", strings.ToLower(decl.SchemaInterface.Name)))
|
||||
data := []byte(tsf.String())
|
||||
genFile.Nodes = append(genFile.Nodes, rawData)
|
||||
|
||||
genPath := filepath.Join(j.root, decl.PluginPath, fmt.Sprintf("%s.gen.ts", strings.ToLower(decl.SchemaInterface.Name)))
|
||||
data := []byte(genFile.String())
|
||||
data = data[:len(data)-1] // remove the additional line break added by the inner jenny
|
||||
|
||||
return codejen.NewFile(path, data, append(jf.From, j)...), nil
|
||||
files := make(codejen.Files, 2)
|
||||
files[0] = *codejen.NewFile(genPath, data, append(jf.From, j)...)
|
||||
|
||||
versionedFile.Nodes = append(versionedFile.Nodes, rawVersion, rawData)
|
||||
|
||||
versionedData := []byte(versionedFile.String())
|
||||
versionedData = versionedData[:len(versionedData)-1]
|
||||
|
||||
pluginFolder := strings.ReplaceAll(strings.ToLower(decl.PluginMeta.Name), " ", "")
|
||||
versionedPath := filepath.Join(versionedPluginPath, pluginFolder, strings.ToLower(decl.SchemaInterface.Name), "x", jf.RelativePath)
|
||||
files[1] = *codejen.NewFile(versionedPath, versionedData, append(jf.From, j)...)
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func getPluginVersion(pluginVersion *string) string {
|
||||
version := "export const pluginVersion = \"%s\";"
|
||||
if pluginVersion != nil {
|
||||
version = fmt.Sprintf(version, *pluginVersion)
|
||||
} else {
|
||||
version = fmt.Sprintf(version, getGrafanaVersion())
|
||||
}
|
||||
|
||||
return version
|
||||
}
|
||||
|
||||
func adaptToPipeline(j codejen.OneToOne[codegen.SchemaForGen]) codejen.OneToOne[*pfs.PluginDecl] {
|
||||
return codejen.AdaptOneToOne(j, func(pd *pfs.PluginDecl) codegen.SchemaForGen {
|
||||
name := strings.ReplaceAll(pd.PluginMeta.Name, " ", "")
|
||||
if pd.SchemaInterface.Name == "DataQuery" {
|
||||
name = name + "DataQuery"
|
||||
}
|
||||
return codegen.SchemaForGen{
|
||||
Name: name,
|
||||
Schema: pd.Lineage.Latest(),
|
||||
IsGroup: pd.SchemaInterface.IsGroup,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func getGrafanaVersion() string {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
pkg, err := build.OpenPackageJSON(path.Join(dir, "../../../"))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return pkg.Version
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user