mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user