mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Kindsys: Replace DefForGen with kindsys.Kind (#62642)
* Kindsys: Replace DeclForGen with kindsys.Kind DeclForGen was always unnecessary - it just wasn't obvious on initial implementation, when we were focused on generating unique types for each core kind. This removes it, considerably simplifying interactions with kindsys - virtually everything now just relies on kindsys.Kind and its derived interfaces. * Removed unused jenny * Rename params in jennies
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package {{ .Properties.MachineName }}
|
||||
package {{ .Props.MachineName }}
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/kindsys"
|
||||
@@ -10,95 +10,59 @@ import (
|
||||
// directory containing the .cue files in which this kind is defined. Necessary
|
||||
// for runtime errors related to the definition and/or lineage to provide
|
||||
// a real path to the correct .cue file.
|
||||
const rootrel string = "kinds/{{ .Properties.MachineName }}"
|
||||
const rootrel string = "kinds/{{ .Props.MachineName }}"
|
||||
|
||||
// TODO standard generated docs
|
||||
type Kind struct {
|
||||
lin thema.ConvergentLineage[*{{ .Properties.Name }}]
|
||||
kindsys.Core
|
||||
lin thema.ConvergentLineage[*{{ .Props.Name }}]
|
||||
jcodec vmux.Codec
|
||||
valmux vmux.ValueMux[*{{ .Properties.Name }}]
|
||||
def kindsys.Def[kindsys.CoreProperties]
|
||||
valmux vmux.ValueMux[*{{ .Props.Name }}]
|
||||
}
|
||||
|
||||
// type guard
|
||||
// type guard - ensure generated Kind type satisfies the kindsys.Core interface
|
||||
var _ kindsys.Core = &Kind{}
|
||||
|
||||
// TODO standard generated docs
|
||||
func NewKind(rt *thema.Runtime, opts ...thema.BindOption) (*Kind, error) {
|
||||
def, err := kindsys.LoadCoreKind(rootrel, rt.Context(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
k := &Kind{
|
||||
def: def,
|
||||
}
|
||||
|
||||
lin, err := def.Some().BindKindLineage(rt, opts...)
|
||||
def, err := kindsys.LoadCoreKindDef(rootrel, rt.Context(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k := &Kind{}
|
||||
k.Core, err = kindsys.BindCore(rt, def, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Get the thema.Schema that the meta says is in the current version (which
|
||||
// codegen ensures is always the latest)
|
||||
cursch := thema.SchemaP(lin, k.def.Properties.CurrentVersion)
|
||||
tsch, err := thema.BindType[*{{ .Properties.Name }}](cursch, &{{ .Properties.Name }}{})
|
||||
cursch := thema.SchemaP(k.Core.Lineage(), def.Properties.CurrentVersion)
|
||||
tsch, err := thema.BindType[*{{ .Props.Name }}](cursch, &{{ .Props.Name }}{})
|
||||
if err != nil {
|
||||
// Should be unreachable, modulo bugs in the Thema->Go code generator
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k.jcodec = vmux.NewJSONCodec("{{ .Properties.MachineName }}.json")
|
||||
k.jcodec = vmux.NewJSONCodec("{{ .Props.MachineName }}.json")
|
||||
k.lin = tsch.ConvergentLineage()
|
||||
k.valmux = vmux.NewValueMux(k.lin.TypedSchema(), k.jcodec)
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// TODO standard generated docs
|
||||
func (k *Kind) Name() string {
|
||||
return "{{ .Properties.MachineName }}"
|
||||
}
|
||||
|
||||
// TODO standard generated docs
|
||||
func (k *Kind) MachineName() string {
|
||||
return "{{ .Properties.MachineName }}"
|
||||
}
|
||||
|
||||
// TODO standard generated docs
|
||||
func (k *Kind) Lineage() thema.Lineage {
|
||||
return k.lin
|
||||
}
|
||||
|
||||
// TODO standard generated docs
|
||||
func (k *Kind) ConvergentLineage() thema.ConvergentLineage[*{{ .Properties.Name }}] {
|
||||
// ConvergentLineage returns the same [thema.Lineage] as Lineage, but bound (see [thema.BindType])
|
||||
// to the the {{ .Props.Name }} type generated from the current schema, v{{ .Props.CurrentVersion }}.
|
||||
func (k *Kind) ConvergentLineage() thema.ConvergentLineage[*{{ .Props.Name }}] {
|
||||
return k.lin
|
||||
}
|
||||
|
||||
// JSONValueMux is a version multiplexer that maps a []byte containing JSON data
|
||||
// at any schematized dashboard version to an instance of {{ .Properties.Name }}.
|
||||
// at any schematized dashboard version to an instance of {{ .Props.Name }}.
|
||||
//
|
||||
// Validation and translation errors emitted from this func will identify the
|
||||
// input bytes as "dashboard.json".
|
||||
//
|
||||
// This is a thin wrapper around Thema's [vmux.ValueMux].
|
||||
func (k *Kind) JSONValueMux(b []byte) (*{{ .Properties.Name }}, thema.TranslationLacunas, error) {
|
||||
func (k *Kind) JSONValueMux(b []byte) (*{{ .Props.Name }}, thema.TranslationLacunas, error) {
|
||||
return k.valmux(b)
|
||||
}
|
||||
|
||||
// TODO standard generated docs
|
||||
func (k *Kind) Maturity() kindsys.Maturity {
|
||||
return k.def.Properties.Maturity
|
||||
}
|
||||
|
||||
// Def returns the [kindsys.Def] containing both CUE and Go representations of the
|
||||
// {{ .Properties.MachineName }} declaration in .cue files.
|
||||
func (k *Kind) Def() kindsys.Def[kindsys.CoreProperties] {
|
||||
return k.def
|
||||
}
|
||||
|
||||
// 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 Def().Props. It is provided to satisfy [kindsys.Interface].
|
||||
func (k *Kind) Props() kindsys.SomeKindProperties {
|
||||
return k.def.Properties
|
||||
}
|
||||
|
||||
@@ -5,38 +5,43 @@ import (
|
||||
"sync"
|
||||
|
||||
{{range .Kinds }}
|
||||
"{{ $.KindPackagePrefix }}/{{ .Properties.MachineName }}"{{end}}
|
||||
"{{ $.KindPackagePrefix }}/{{ .Props.MachineName }}"{{end}}
|
||||
"github.com/grafana/grafana/pkg/cuectx"
|
||||
"github.com/grafana/grafana/pkg/kindsys"
|
||||
"github.com/grafana/thema"
|
||||
)
|
||||
|
||||
// Base is a registry of kindsys.Interface. It provides two modes for accessing
|
||||
// kinds: individually via literal named methods, or as a slice returned from
|
||||
// an All*() method.
|
||||
// Base is a registry of all Grafana core kinds. It is designed for use both inside
|
||||
// of Grafana itself, and for import by external Go programs wanting to work with Grafana's
|
||||
// kind system.
|
||||
//
|
||||
// The registry provides two modes for accessing core kinds:
|
||||
// * Per-kind methods, which return the kind-specific type, e.g. Dashboard() returns [dashboard.Dashboard].
|
||||
// * All(), which returns a slice of [kindsys.Core].
|
||||
//
|
||||
// Prefer the individual named methods for use cases where the particular kind(s) that
|
||||
// are needed are known to the caller. For example, a dashboard linter can know that it
|
||||
// specifically wants the dashboard kind.
|
||||
//
|
||||
// Prefer All*() methods when performing operations generically across all kinds.
|
||||
// For example, a validation HTTP middleware for any kind-schematized object type.
|
||||
// Prefer All() when performing operations generically across all kinds. For example,
|
||||
// a generic HTTP middleware for validating request bodies expected to contain some
|
||||
// kind-schematized type.
|
||||
type Base struct {
|
||||
all []kindsys.Core
|
||||
{{- range .Kinds }}
|
||||
{{ .Properties.MachineName }} *{{ .Properties.MachineName }}.Kind{{end}}
|
||||
{{ .Props.MachineName }} *{{ .Props.MachineName }}.Kind{{end}}
|
||||
}
|
||||
|
||||
// type guards
|
||||
var (
|
||||
{{- range .Kinds }}
|
||||
_ kindsys.Core = &{{ .Properties.MachineName }}.Kind{}{{end}}
|
||||
_ kindsys.Core = &{{ .Props.MachineName }}.Kind{}{{end}}
|
||||
)
|
||||
|
||||
{{range .Kinds }}
|
||||
// {{ .Properties.Name }} returns the [kindsys.Interface] implementation for the {{ .Properties.MachineName }} kind.
|
||||
func (b *Base) {{ .Properties.Name }}() *{{ .Properties.MachineName }}.Kind {
|
||||
return b.{{ .Properties.MachineName }}
|
||||
// {{ .Props.Name }} returns the [kindsys.Interface] implementation for the {{ .Props.MachineName }} kind.
|
||||
func (b *Base) {{ .Props.Name }}() *{{ .Props.MachineName }}.Kind {
|
||||
return b.{{ .Props.MachineName }}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
@@ -45,11 +50,11 @@ func doNewBase(rt *thema.Runtime) *Base {
|
||||
reg := &Base{}
|
||||
|
||||
{{range .Kinds }}
|
||||
reg.{{ .Properties.MachineName }}, err = {{ .Properties.MachineName }}.NewKind(rt)
|
||||
reg.{{ .Props.MachineName }}, err = {{ .Props.MachineName }}.NewKind(rt)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error while initializing the {{ .Properties.MachineName }} Kind: %s", err))
|
||||
panic(fmt.Sprintf("error while initializing the {{ .Props.MachineName }} Kind: %s", err))
|
||||
}
|
||||
reg.all = append(reg.all, reg.{{ .Properties.MachineName }})
|
||||
reg.all = append(reg.all, reg.{{ .Props.MachineName }})
|
||||
{{end}}
|
||||
|
||||
return reg
|
||||
|
||||
Reference in New Issue
Block a user