Kindsys: Remove Raw kind category (#60992)

* Remove Raw references

* Remove more raws

* Re-generate files

* Remove raw folder from veneer

* Fix import

* Fix lint

* Bring back raw folder in grafana-schema

* Another lint

* Remove use of "Structured" word in kinds

* Delete unused function and remove some structured words

* Bunch more removals of structured name

Co-authored-by: sam boyer <sdboyer@grafana.com>
This commit is contained in:
Selene
2023-01-05 13:54:42 -05:00
committed by GitHub
co-authored by sam boyer
parent db369fc5b2
commit 8f29450594
34 changed files with 148 additions and 560 deletions
-23
View File
@@ -29,29 +29,6 @@ func ForGen(rt *thema.Runtime, decl *kindsys.SomeDecl) (*DeclForGen, error) {
}, nil
}
// RawForGen produces a [DeclForGen] from a [kindsys.Raw] kind.
//
// Useful for grafana-external code generators, which depend on the Kind
// representation in registries produced by grafana core (such as
// ["github.com/grafana/grafana/pkg/registry/corekind".NewBase]).
func RawForGen(k kindsys.Raw) *DeclForGen {
return &DeclForGen{
SomeDecl: k.Decl().Some(),
}
}
// StructuredForGen produces a [DeclForGen] from a [kindsys.Structured] kind.
//
// Useful for grafana-external code generators, which depend on the Kind
// representation in registries produced by grafana core (such as
// ["github.com/grafana/grafana/pkg/registry/corekind".NewBase]).
func StructuredForGen(k kindsys.Structured) *DeclForGen {
return &DeclForGen{
SomeDecl: k.Decl().Some(),
lin: k.Lineage(),
}
}
// DeclForGen wraps [kindsys.SomeDecl] to provide trivial caching of
// the lineage declared by the kind (nil for raw kinds).
type DeclForGen struct {
+1 -9
View File
@@ -32,17 +32,9 @@ func (gen *genBaseRegistry) JennyName() string {
}
func (gen *genBaseRegistry) Generate(decls ...*DeclForGen) (*codejen.File, error) {
var numRaw int
for _, k := range decls {
if k.IsRaw() {
numRaw++
}
}
buf := new(bytes.Buffer)
if err := tmpls.Lookup("kind_registry.tmpl").Execute(buf, tvars_kind_registry{
NumRaw: numRaw,
NumStructured: len(decls) - numRaw,
NumStructured: len(decls),
PackageName: filepath.Base(gen.path),
KindPackagePrefix: filepath.ToSlash(filepath.Join("github.com/grafana/grafana", gen.kindrelroot)),
Kinds: decls,
@@ -8,14 +8,14 @@ import (
"github.com/grafana/codejen"
)
// CoreStructuredKindJenny generates the implementation of
// [kindsys.Structured] for the provided kind declaration.
// CoreKindJenny generates the implementation of [kindsys.Core] for the provided
// kind declaration.
//
// gokindsdir should be the relative path to the parent directory that contains
// all generated kinds.
//
// This generator only has output for core structured kinds.
func CoreStructuredKindJenny(gokindsdir string, cfg *CoreStructuredKindGeneratorConfig) OneToOne {
func CoreKindJenny(gokindsdir string, cfg *CoreStructuredKindGeneratorConfig) OneToOne {
if cfg == nil {
cfg = new(CoreStructuredKindGeneratorConfig)
}
@@ -25,39 +25,39 @@ func CoreStructuredKindJenny(gokindsdir string, cfg *CoreStructuredKindGenerator
}
}
return &genCoreStructuredKind{
return &coreKindJenny{
gokindsdir: gokindsdir,
cfg: cfg,
}
}
// CoreStructuredKindGeneratorConfig holds configuration options for [CoreStructuredKindJenny].
// CoreStructuredKindGeneratorConfig holds configuration options for [CoreKindJenny].
type CoreStructuredKindGeneratorConfig struct {
// GenDirName returns the name of the directory in which the file should be
// generated. Defaults to DeclForGen.Lineage().Name() if nil.
GenDirName func(*DeclForGen) string
}
type genCoreStructuredKind struct {
type coreKindJenny struct {
gokindsdir string
cfg *CoreStructuredKindGeneratorConfig
}
var _ OneToOne = &genCoreStructuredKind{}
var _ OneToOne = &coreKindJenny{}
func (gen *genCoreStructuredKind) JennyName() string {
return "CoreStructuredKindJenny"
func (gen *coreKindJenny) JennyName() string {
return "CoreKindJenny"
}
func (gen *genCoreStructuredKind) Generate(decl *DeclForGen) (*codejen.File, error) {
if !decl.IsCoreStructured() {
func (gen *coreKindJenny) Generate(decl *DeclForGen) (*codejen.File, error) {
if !decl.IsCore() {
return nil, nil
}
path := filepath.Join(gen.gokindsdir, gen.cfg.GenDirName(decl), decl.Properties.Common().MachineName+"_kind_gen.go")
buf := new(bytes.Buffer)
if err := tmpls.Lookup("kind_corestructured.tmpl").Execute(buf, decl); err != nil {
return nil, fmt.Errorf("failed executing kind_corestructured template for %s: %w", path, err)
if err := tmpls.Lookup("kind_core.tmpl").Execute(buf, decl); err != nil {
return nil, fmt.Errorf("failed executing kind_core template for %s: %w", path, err)
}
b, err := postprocessGoFile(genGoFile{
path: path,
-3
View File
@@ -30,9 +30,6 @@ func (j *lmox) JennyName() string {
}
func (j *lmox) Generate(decl *DeclForGen) (codejen.Files, error) {
if decl.IsRaw() {
return nil, nil
}
comm := decl.Properties.Common()
sfg := SchemaForGen{
Name: comm.Name,
-68
View File
@@ -1,68 +0,0 @@
package codegen
import (
"bytes"
"fmt"
"path/filepath"
"github.com/grafana/codejen"
)
// RawKindJenny generates the implementation of [kindsys.Raw] for the
// provided kind declaration.
//
// gokindsdir should be the relative path to the parent directory that contains
// all generated kinds.
//
// This generator only has output for raw kinds.
func RawKindJenny(gokindsdir string, cfg *RawKindGeneratorConfig) OneToOne {
if cfg == nil {
cfg = new(RawKindGeneratorConfig)
}
if cfg.GenDirName == nil {
cfg.GenDirName = func(decl *DeclForGen) string {
return decl.Properties.Common().MachineName
}
}
return &genRawKind{
gokindsdir: gokindsdir,
cfg: cfg,
}
}
type genRawKind struct {
gokindsdir string
cfg *RawKindGeneratorConfig
}
type RawKindGeneratorConfig struct {
// GenDirName returns the name of the directory in which the file should be
// generated. Defaults to DeclForGen.Lineage().Name() if nil.
GenDirName func(*DeclForGen) string
}
func (gen *genRawKind) JennyName() string {
return "RawKindJenny"
}
func (gen *genRawKind) Generate(decl *DeclForGen) (*codejen.File, error) {
if !decl.IsRaw() {
return nil, nil
}
path := filepath.Join(gen.gokindsdir, gen.cfg.GenDirName(decl), decl.Properties.Common().MachineName+"_kind_gen.go")
buf := new(bytes.Buffer)
if err := tmpls.Lookup("kind_raw.tmpl").Execute(buf, decl); err != nil {
return nil, fmt.Errorf("failed executing kind_raw template for %s: %w", path, err)
}
b, err := postprocessGoFile(genGoFile{
path: path,
in: buf.Bytes(),
})
if err != nil {
return nil, err
}
return codejen.NewFile(path, b, gen), nil
}
-4
View File
@@ -42,10 +42,6 @@ func (gen *genTSVeneerIndex) JennyName() string {
func (gen *genTSVeneerIndex) Generate(decls ...*DeclForGen) (*codejen.File, error) {
tsf := new(ast.File)
for _, decl := range decls {
if decl.IsRaw() {
continue
}
sch := decl.Lineage().Latest()
f, err := typescript.GenerateTypes(sch, &typescript.TypeConfig{
RootName: decl.Properties.Common().Name,
-3
View File
@@ -32,9 +32,6 @@ func (j *latestj) JennyName() string {
}
func (j *latestj) Generate(decl *DeclForGen) (*codejen.File, error) {
if decl.IsRaw() {
return nil, nil
}
comm := decl.Properties.Common()
sfg := SchemaForGen{
Name: comm.Name,
+4 -4
View File
@@ -38,10 +38,10 @@ type (
}
tvars_kind_registry struct {
// Header tvars_autogen_header
NumRaw, NumStructured int
PackageName string
KindPackagePrefix string
Kinds []*DeclForGen
NumStructured int
PackageName string
KindPackagePrefix string
Kinds []*DeclForGen
}
tvars_coremodel_imports struct {
PackageName string
@@ -10,22 +10,22 @@ import (
// directory containing the .cue files in which this kind is declared. Necessary
// for runtime errors related to the declaration and/or lineage to provide
// a real path to the correct .cue file.
const rootrel string = "kinds/structured/{{ .Properties.MachineName }}"
const rootrel string = "kinds/{{ .Properties.MachineName }}"
// TODO standard generated docs
type Kind struct {
lin thema.ConvergentLineage[*{{ .Properties.Name }}]
jcodec vmux.Codec
valmux vmux.ValueMux[*{{ .Properties.Name }}]
decl kindsys.Decl[kindsys.CoreStructuredProperties]
decl kindsys.Decl[kindsys.CoreProperties]
}
// type guard
var _ kindsys.Structured = &Kind{}
var _ kindsys.Core = &Kind{}
// TODO standard generated docs
func NewKind(rt *thema.Runtime, opts ...thema.BindOption) (*Kind, error) {
decl, err := kindsys.LoadCoreKind[kindsys.CoreStructuredProperties](rootrel, rt.Context(), nil)
decl, err := kindsys.LoadCoreKind(rootrel, rt.Context(), nil)
if err != nil {
return nil, err
}
@@ -91,12 +91,12 @@ func (k *Kind) Maturity() kindsys.Maturity {
// Decl returns the [kindsys.Decl] containing both CUE and Go representations of the
// {{ .Properties.MachineName }} declaration in .cue files.
func (k *Kind) Decl() *kindsys.Decl[kindsys.CoreStructuredProperties] {
func (k *Kind) Decl() *kindsys.Decl[kindsys.CoreProperties] {
d := k.decl
return &d
}
// Props returns a [kindsys.SomeKindProps], with underlying type [kindsys.CoreStructuredProperties],
// Props returns a [kindsys.SomeKindProps], with underlying type [kindsys.CoreProperties],
// representing the static properties declared in the {{ .Properties.MachineName }} kind.
//
// This method is identical to calling Decl().Props. It is provided to satisfy [kindsys.Interface].
-57
View File
@@ -1,57 +0,0 @@
package {{ .Properties.MachineName }}
import (
"github.com/grafana/grafana/pkg/kindsys"
"github.com/grafana/thema"
"github.com/grafana/thema/vmux"
)
// TODO standard generated docs
type Kind struct {
decl kindsys.Decl[kindsys.RawProperties]
}
// type guard
var _ kindsys.Raw = &Kind{}
// TODO standard generated docs
func NewKind() (*Kind, error) {
decl, err := kindsys.LoadCoreKind[kindsys.RawProperties]("kinds/raw/{{ .Properties.MachineName }}", nil, nil)
if err != nil {
return nil, err
}
return &Kind{
decl: *decl,
}, nil
}
// TODO standard generated docs
func (k *Kind) Name() string {
return "{{ .Properties.Name }}"
}
// TODO standard generated docs
func (k *Kind) MachineName() string {
return "{{ .Properties.MachineName }}"
}
// TODO standard generated docs
func (k *Kind) Maturity() kindsys.Maturity {
return k.decl.Properties.Maturity
}
// Decl returns the [kindsys.Decl] containing both CUE and Go representations of the
// {{ .Properties.MachineName }} declaration in .cue files.
func (k *Kind) Decl() *kindsys.Decl[kindsys.RawProperties] {
d := k.decl
return &d
}
// Props returns a [kindsys.SomeKindProps], with underlying type [kindsys.RawProperties],
// representing the static properties declared in the {{ .Properties.MachineName }} kind.
//
// This method is identical to calling Decl().Props. It is provided to satisfy [kindsys.Interface].
func (k *Kind) Props() kindsys.SomeKindProperties {
return k.decl.Properties
}
+4 -8
View File
@@ -22,8 +22,7 @@ import (
// Prefer All*() methods when performing operations generically across all kinds.
// For example, a validation HTTP middleware for any kind-schematized object type.
type Base struct {
all []kindsys.Interface
numRaw, numStructured int
all []kindsys.Core
{{- range .Kinds }}
{{ .Properties.MachineName }} *{{ .Properties.MachineName }}.Kind{{end}}
}
@@ -31,7 +30,7 @@ type Base struct {
// type guards
var (
{{- range .Kinds }}
_ kindsys.{{ if .IsRaw }}Raw{{ else }}Structured{{ end }} = &{{ .Properties.MachineName }}.Kind{}{{end}}
_ kindsys.Core = &{{ .Properties.MachineName }}.Kind{}{{end}}
)
{{range .Kinds }}
@@ -43,13 +42,10 @@ func (b *Base) {{ .Properties.Name }}() *{{ .Properties.MachineName }}.Kind {
func doNewBase(rt *thema.Runtime) *Base {
var err error
reg := &Base{
numRaw: {{ .NumRaw }},
numStructured: {{ .NumStructured }},
}
reg := &Base{}
{{range .Kinds }}
reg.{{ .Properties.MachineName }}, err = {{ .Properties.MachineName }}.NewKind({{ if .IsCoreStructured }}rt{{ end }})
reg.{{ .Properties.MachineName }}, err = {{ .Properties.MachineName }}.NewKind(rt)
if err != nil {
panic(fmt.Sprintf("error while initializing the {{ .Properties.MachineName }} Kind: %s", err))
}