Skip to content

Commit

Permalink
Schemas: Delete unused code from the previous refactors (grafana#84254)
Browse files Browse the repository at this point in the history
* Delete unused code from the previous refactors

* Sort imports
  • Loading branch information
spinillos authored Mar 13, 2024
1 parent 2a1a514 commit 75ea33e
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 368 deletions.
51 changes: 1 addition & 50 deletions kinds/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"cuelang.org/go/cue/errors"
"github.com/grafana/codejen"
"github.com/grafana/cuetsy"
"github.com/grafana/cuetsy/ts"
"github.com/grafana/cuetsy/ts/ast"
"github.com/grafana/kindsys"

"github.com/grafana/grafana/pkg/codegen"
Expand All @@ -44,9 +42,7 @@ func main() {
&codegen.GoSpecJenny{},
codegen.CoreKindJenny(cuectx.GoCoreKindParentPath, nil),
codegen.BaseCoreRegistryJenny(filepath.Join("pkg", "registry", "corekind"), cuectx.GoCoreKindParentPath),
codegen.LatestMajorsOrXJenny(
cuectx.TSCoreKindParentPath,
codegen.TSTypesJenny{ApplyFuncs: []codegen.ApplyFunc{renameSpecNode}}),
codegen.LatestMajorsOrXJenny(cuectx.TSCoreKindParentPath),
codegen.TSVeneerIndexJenny(filepath.Join("packages", "grafana-schema", "src")),
)

Expand Down Expand Up @@ -233,48 +229,3 @@ func loadCueFiles(dirs []os.DirEntry) []cue.Value {

return values
}

// renameSpecNode rename spec node from the TS file result
func renameSpecNode(sfg codegen.SchemaForGen, 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 = sfg.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" + sfg.Name
tt := x.Type.(ast.TypeTransformExpr)
tt.Expr = ts.Ident(sfg.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)
}
}
13 changes: 0 additions & 13 deletions pkg/codegen/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ type OneToMany codejen.OneToMany[kindsys.Kind]
type ManyToOne codejen.ManyToOne[kindsys.Kind]
type ManyToMany codejen.ManyToMany[kindsys.Kind]

// ForLatestSchema returns a [SchemaForGen] for the latest schema in the
// provided [kindsys.Kind]'s lineage.
//
// TODO this will be replaced by thema-native constructs
func ForLatestSchema(k kindsys.Kind) SchemaForGen {
comm := k.Props().Common()
return SchemaForGen{
Name: comm.Name,
Schema: k.Lineage().Latest(),
IsGroup: comm.LineageIsGroup,
}
}

// SlashHeaderMapper produces a FileMapper that injects a comment header onto
// a [codejen.File] indicating the main generator that produced it (via the provided
// maingen, which should be a path) and the jenny or jennies that constructed the
Expand Down
57 changes: 49 additions & 8 deletions pkg/codegen/jenny_eachmajor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ import (
"path/filepath"

"github.com/grafana/codejen"
"github.com/grafana/cuetsy/ts"
"github.com/grafana/cuetsy/ts/ast"
"github.com/grafana/kindsys"
)

// 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, inner codejen.OneToOne[SchemaForGen]) OneToMany {
if inner == nil {
panic("inner jenny must not be nil")
}

func LatestMajorsOrXJenny(parentdir string) OneToMany {
return &lmox{
parentdir: parentdir,
inner: inner,
inner: TSTypesJenny{ApplyFuncs: []ApplyFunc{renameSpecNode}},
}
}

Expand Down Expand Up @@ -56,3 +52,48 @@ func (j *lmox) Generate(kind kindsys.Kind) (codejen.Files, error) {
f.From = append(f.From, j)
return codejen.Files{*f}, nil
}

// renameSpecNode rename spec node from the TS file result
func renameSpecNode(sfg SchemaForGen, 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 = sfg.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" + sfg.Name
tt := x.Type.(ast.TypeTransformExpr)
tt.Expr = ts.Ident(sfg.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)
}
}
42 changes: 0 additions & 42 deletions pkg/codegen/jenny_go_types.go

This file was deleted.

54 changes: 0 additions & 54 deletions pkg/codegen/latest_jenny.go

This file was deleted.

64 changes: 0 additions & 64 deletions pkg/codegen/tmpl/addenda.tmpl

This file was deleted.

28 changes: 0 additions & 28 deletions pkg/codegen/tmpl/autogen_header.tmpl

This file was deleted.

10 changes: 0 additions & 10 deletions pkg/codegen/tmpl/coremodel_imports.tmpl

This file was deleted.

2 changes: 0 additions & 2 deletions pkg/codegen/tmpl/cuetsy_multi.tmpl

This file was deleted.

12 changes: 0 additions & 12 deletions pkg/codegen/tmpl/plugin_lineage_binding.tmpl

This file was deleted.

Loading

0 comments on commit 75ea33e

Please sign in to comment.