Kindsys: Replace "Declaration" with "Definition" (#62515)

* s/Declaration/Definition/g

* s/DeclForGen/DefForGen/g

* Rename some local vars
This commit is contained in:
sam boyer
2023-01-31 04:50:08 -05:00
committed by GitHub
parent d48a8fd227
commit dbe2f0c8f6
34 changed files with 262 additions and 261 deletions

View File

@ -10,44 +10,45 @@ import (
"github.com/grafana/thema"
)
type OneToOne codejen.OneToOne[*DeclForGen]
type OneToMany codejen.OneToMany[*DeclForGen]
type ManyToOne codejen.ManyToOne[*DeclForGen]
type ManyToMany codejen.ManyToMany[*DeclForGen]
type OneToOne codejen.OneToOne[*DefForGen]
type OneToMany codejen.OneToMany[*DefForGen]
type ManyToOne codejen.ManyToOne[*DefForGen]
type ManyToMany codejen.ManyToMany[*DefForGen]
// ForGen is a codejen input transformer that converts a pure kindsys.SomeDecl into
// a DeclForGen by binding its contained lineage.
func ForGen(rt *thema.Runtime, decl kindsys.SomeDecl) (*DeclForGen, error) {
lin, err := decl.BindKindLineage(rt)
// ForGen is a codejen input transformer that converts a pure kindsys.SomeDef into
// a DefForGen by binding its contained lineage.
func ForGen(rt *thema.Runtime, def kindsys.SomeDef) (*DefForGen, error) {
lin, err := def.BindKindLineage(rt)
if err != nil {
return nil, err
}
return &DeclForGen{
SomeDecl: decl,
lin: lin,
return &DefForGen{
SomeDef: def,
lin: lin,
}, nil
}
// DeclForGen wraps [kindsys.SomeDecl] to provide trivial caching of
// DefForGen wraps [kindsys.SomeDef] to provide trivial caching of
// the lineage declared by the kind (nil for raw kinds).
type DeclForGen struct {
kindsys.SomeDecl
// TODO this type is unneeded - kindsys.Kind is sufficient.
type DefForGen struct {
kindsys.SomeDef
lin thema.Lineage
}
// Lineage returns the [thema.Lineage] for the underlying [kindsys.SomeDecl].
func (decl *DeclForGen) Lineage() thema.Lineage {
return decl.lin
// Lineage returns the [thema.Lineage] for the underlying [kindsys.SomeDef].
func (def *DefForGen) Lineage() thema.Lineage {
return def.lin
}
// ForLatestSchema returns a [SchemaForGen] for the latest schema in this
// DeclForGen's lineage.
func (decl *DeclForGen) ForLatestSchema() SchemaForGen {
comm := decl.Properties.Common()
// DefForGen's lineage.
func (def *DefForGen) ForLatestSchema() SchemaForGen {
comm := def.Properties.Common()
return SchemaForGen{
Name: comm.Name,
Schema: decl.Lineage().Latest(),
Schema: def.Lineage().Latest(),
IsGroup: comm.LineageIsGroup,
}
}

View File

@ -31,12 +31,12 @@ func (gen *genBaseRegistry) JennyName() string {
return "BaseCoreRegistryJenny"
}
func (gen *genBaseRegistry) Generate(decls ...*DeclForGen) (*codejen.File, error) {
func (gen *genBaseRegistry) Generate(defs ...*DefForGen) (*codejen.File, error) {
buf := new(bytes.Buffer)
if err := tmpls.Lookup("kind_registry.tmpl").Execute(buf, tvars_kind_registry{
PackageName: filepath.Base(gen.path),
KindPackagePrefix: filepath.ToSlash(filepath.Join("github.com/grafana/grafana", gen.kindrelroot)),
Kinds: decls,
Kinds: defs,
}); err != nil {
return nil, fmt.Errorf("failed executing kind registry template: %w", err)
}

View File

@ -20,8 +20,8 @@ func CoreKindJenny(gokindsdir string, cfg *CoreKindJennyConfig) OneToOne {
cfg = new(CoreKindJennyConfig)
}
if cfg.GenDirName == nil {
cfg.GenDirName = func(decl *DeclForGen) string {
return decl.Properties.Common().MachineName
cfg.GenDirName = func(def *DefForGen) string {
return def.Properties.Common().MachineName
}
}
@ -34,8 +34,8 @@ func CoreKindJenny(gokindsdir string, cfg *CoreKindJennyConfig) OneToOne {
// CoreKindJennyConfig holds configuration options for [CoreKindJenny].
type CoreKindJennyConfig 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
// generated. Defaults to DefForGen.Lineage().Name() if nil.
GenDirName func(*DefForGen) string
}
type coreKindJenny struct {
@ -49,14 +49,14 @@ func (gen *coreKindJenny) JennyName() string {
return "CoreKindJenny"
}
func (gen *coreKindJenny) Generate(decl *DeclForGen) (*codejen.File, error) {
if !decl.IsCore() {
func (gen *coreKindJenny) Generate(def *DefForGen) (*codejen.File, error) {
if !def.IsCore() {
return nil, nil
}
path := filepath.Join(gen.gokindsdir, gen.cfg.GenDirName(decl), decl.Properties.Common().MachineName+"_kind_gen.go")
path := filepath.Join(gen.gokindsdir, gen.cfg.GenDirName(def), def.Properties.Common().MachineName+"_kind_gen.go")
buf := new(bytes.Buffer)
if err := tmpls.Lookup("kind_core.tmpl").Execute(buf, decl); err != nil {
if err := tmpls.Lookup("kind_core.tmpl").Execute(buf, def); err != nil {
return nil, fmt.Errorf("failed executing kind_core template for %s: %w", path, err)
}
b, err := postprocessGoFile(genGoFile{

View File

@ -34,8 +34,8 @@ func (j docsJenny) JennyName() string {
return "DocsJenny"
}
func (j docsJenny) Generate(decl *DeclForGen) (*codejen.File, error) {
f, err := jsonschema.GenerateSchema(decl.Lineage().Latest())
func (j docsJenny) Generate(def *DefForGen) (*codejen.File, error) {
f, err := jsonschema.GenerateSchema(def.Lineage().Latest())
if err != nil {
return nil, fmt.Errorf("failed to generate json representation for the schema: %v", err)
}
@ -61,10 +61,10 @@ func (j docsJenny) Generate(decl *DeclForGen) (*codejen.File, error) {
// fixes the references between the types within a json after making components.schema.<types> the root of the json
kindJsonStr := strings.Replace(string(obj.Components.Schemas), "#/components/schemas/", "#/", -1)
kindProps := decl.Properties.Common()
kindProps := def.Properties.Common()
data := templateData{
KindName: kindProps.Name,
KindVersion: decl.Lineage().Latest().Version().String(),
KindVersion: def.Lineage().Latest().Version().String(),
KindMaturity: string(kindProps.Maturity),
Markdown: "{{ .Markdown 1 }}",
}

View File

@ -29,8 +29,8 @@ func (j *lmox) JennyName() string {
return "LatestMajorsOrXJenny"
}
func (j *lmox) Generate(decl *DeclForGen) (codejen.Files, error) {
comm := decl.Properties.Common()
func (j *lmox) Generate(def *DefForGen) (codejen.Files, error) {
comm := def.Properties.Common()
sfg := SchemaForGen{
Name: comm.Name,
IsGroup: comm.LineageIsGroup,
@ -39,7 +39,7 @@ func (j *lmox) Generate(decl *DeclForGen) (codejen.Files, error) {
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(), decl.Properties.Common().Name, err)
return nil, fmt.Errorf("%s jenny failed on %s schema for %s: %w", j.inner.JennyName(), sfg.Schema.Version(), def.Properties.Common().Name, err)
}
if f == nil || !f.Exists() {
return nil, nil
@ -51,12 +51,12 @@ func (j *lmox) Generate(decl *DeclForGen) (codejen.Files, error) {
}
if comm.Maturity.Less(kindsys.MaturityStable) {
sfg.Schema = decl.Lineage().Latest()
sfg.Schema = def.Lineage().Latest()
return do(sfg, "x")
}
var fl codejen.Files
for sch := decl.Lineage().First(); sch != nil; sch = sch.Successor() {
for sch := def.Lineage().First(); sch != nil; sch = sch.Successor() {
sfg.Schema = sch.LatestInMajor()
files, err := do(sfg, fmt.Sprintf("v%v", sch.Version()[0]))
if err != nil {

View File

@ -39,20 +39,20 @@ func (gen *genTSVeneerIndex) JennyName() string {
return "TSVeneerIndexJenny"
}
func (gen *genTSVeneerIndex) Generate(decls ...*DeclForGen) (*codejen.File, error) {
func (gen *genTSVeneerIndex) Generate(decls ...*DefForGen) (*codejen.File, error) {
tsf := new(ast.File)
for _, decl := range decls {
sch := decl.Lineage().Latest()
for _, def := range decls {
sch := def.Lineage().Latest()
f, err := typescript.GenerateTypes(sch, &typescript.TypeConfig{
RootName: decl.Properties.Common().Name,
Group: decl.Properties.Common().LineageIsGroup,
RootName: def.Properties.Common().Name,
Group: def.Properties.Common().LineageIsGroup,
})
if err != nil {
return nil, fmt.Errorf("%s: %w", decl.Properties.Common().Name, err)
return nil, fmt.Errorf("%s: %w", def.Properties.Common().Name, err)
}
elems, err := gen.extractTSIndexVeneerElements(decl, f)
elems, err := gen.extractTSIndexVeneerElements(def, f)
if err != nil {
return nil, fmt.Errorf("%s: %w", decl.Properties.Common().Name, err)
return nil, fmt.Errorf("%s: %w", def.Properties.Common().Name, err)
}
tsf.Nodes = append(tsf.Nodes, elems...)
}
@ -60,9 +60,9 @@ func (gen *genTSVeneerIndex) Generate(decls ...*DeclForGen) (*codejen.File, erro
return codejen.NewFile(filepath.Join(gen.dir, "index.gen.ts"), []byte(tsf.String()), gen), nil
}
func (gen *genTSVeneerIndex) extractTSIndexVeneerElements(decl *DeclForGen, tf *ast.File) ([]ast.Decl, error) {
lin := decl.Lineage()
comm := decl.Properties.Common()
func (gen *genTSVeneerIndex) extractTSIndexVeneerElements(def *DefForGen, tf *ast.File) ([]ast.Decl, error) {
lin := def.Lineage()
comm := def.Properties.Common()
// Check the root, then walk the tree
rootv := lin.Latest().Underlying()
@ -90,7 +90,7 @@ func (gen *genTSVeneerIndex) extractTSIndexVeneerElements(decl *DeclForGen, tf *
name = sels[0].String()
}
// Search the generated TS AST for the type and default decl nodes
// Search the generated TS AST for the type and default def nodes
pair := findDeclNode(name, tf)
if pair.T == nil {
// No generated type for this item, skip it
@ -139,7 +139,7 @@ func (gen *genTSVeneerIndex) extractTSIndexVeneerElements(decl *DeclForGen, tf *
}
vpath := fmt.Sprintf("v%v", thema.LatestVersion(lin)[0])
if decl.Properties.Common().Maturity.Less(kindsys.MaturityStable) {
if def.Properties.Common().Maturity.Less(kindsys.MaturityStable) {
vpath = "x"
}
@ -190,7 +190,7 @@ func (gen *genTSVeneerIndex) extractTSIndexVeneerElements(decl *DeclForGen, tf *
})
}
// TODO emit a decl in the index.gen.ts that ensures any custom veneer types are "compatible" with current version raw types
// TODO emit a def in the index.gen.ts that ensures any custom veneer types are "compatible" with current version raw types
return ret, nil
}
@ -205,13 +205,13 @@ type tsVeneerAttr struct {
func findDeclNode(name string, tf *ast.File) declPair {
var p declPair
for _, decl := range tf.Nodes {
for _, def := range tf.Nodes {
// Peer through export keywords
if ex, is := decl.(ast.ExportKeyword); is {
decl = ex.Decl
if ex, is := def.(ast.ExportKeyword); is {
def = ex.Decl
}
switch x := decl.(type) {
switch x := def.(type) {
case ast.TypeDecl:
if x.Name.Name == name {
p.T = &x.Name

View File

@ -8,7 +8,7 @@ import (
)
// LatestJenny returns a jenny that runs another jenny for only the latest
// schema in a DeclForGen, and prefixes the resulting file with the provided
// schema in a DefForGen, and prefixes the resulting file with the provided
// parentdir (e.g. "pkg/kinds/") and with a directory based on the kind's
// machine name (e.g. "dashboard/").
func LatestJenny(parentdir string, inner codejen.OneToOne[SchemaForGen]) OneToOne {
@ -31,17 +31,17 @@ func (j *latestj) JennyName() string {
return "LatestJenny"
}
func (j *latestj) Generate(decl *DeclForGen) (*codejen.File, error) {
comm := decl.Properties.Common()
func (j *latestj) Generate(def *DefForGen) (*codejen.File, error) {
comm := def.Properties.Common()
sfg := SchemaForGen{
Name: comm.Name,
Schema: decl.Lineage().Latest(),
Schema: def.Lineage().Latest(),
IsGroup: comm.LineageIsGroup,
}
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(), decl.Properties.Common().Name, err)
return nil, fmt.Errorf("%s jenny failed on %s schema for %s: %w", j.inner.JennyName(), sfg.Schema.Version(), def.Properties.Common().Name, err)
}
if f == nil || !f.Exists() {
return nil, nil

View File

@ -40,7 +40,7 @@ type (
// Header tvars_autogen_header
PackageName string
KindPackagePrefix string
Kinds []*DeclForGen
Kinds []*DefForGen
}
tvars_coremodel_imports struct {
PackageName string

View File

@ -7,8 +7,8 @@ import (
)
// rootrel is the relative path from the grafana repository root to the
// directory containing the .cue files in which this kind is declared. Necessary
// for runtime errors related to the declaration and/or lineage to provide
// 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 }}"
@ -17,7 +17,7 @@ type Kind struct {
lin thema.ConvergentLineage[*{{ .Properties.Name }}]
jcodec vmux.Codec
valmux vmux.ValueMux[*{{ .Properties.Name }}]
decl kindsys.Decl[kindsys.CoreProperties]
def kindsys.Def[kindsys.CoreProperties]
}
// type guard
@ -25,22 +25,22 @@ var _ kindsys.Core = &Kind{}
// TODO standard generated docs
func NewKind(rt *thema.Runtime, opts ...thema.BindOption) (*Kind, error) {
decl, err := kindsys.LoadCoreKind(rootrel, rt.Context(), nil)
def, err := kindsys.LoadCoreKind(rootrel, rt.Context(), nil)
if err != nil {
return nil, err
}
k := &Kind{
decl: decl,
def: def,
}
lin, err := decl.Some().BindKindLineage(rt, opts...)
lin, err := def.Some().BindKindLineage(rt, 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.decl.Properties.CurrentVersion)
cursch := thema.SchemaP(lin, k.def.Properties.CurrentVersion)
tsch, err := thema.BindType[*{{ .Properties.Name }}](cursch, &{{ .Properties.Name }}{})
if err != nil {
// Should be unreachable, modulo bugs in the Thema->Go code generator
@ -86,19 +86,19 @@ func (k *Kind) JSONValueMux(b []byte) (*{{ .Properties.Name }}, thema.Translatio
// TODO standard generated docs
func (k *Kind) Maturity() kindsys.Maturity {
return k.decl.Properties.Maturity
return k.def.Properties.Maturity
}
// Decl returns the [kindsys.Decl] containing both CUE and Go representations of the
// Def returns the [kindsys.Def] containing both CUE and Go representations of the
// {{ .Properties.MachineName }} declaration in .cue files.
func (k *Kind) Decl() kindsys.Decl[kindsys.CoreProperties] {
return k.decl
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 Decl().Props. It is provided to satisfy [kindsys.Interface].
// This method is identical to calling Def().Props. It is provided to satisfy [kindsys.Interface].
func (k *Kind) Props() kindsys.SomeKindProperties {
return k.decl.Properties
return k.def.Properties
}

View File

@ -126,17 +126,17 @@ func (d prefixmod) applyfunc(c *dstutil.Cursor) bool {
// field value specifications that reference those types.
d.handleExpr(x.Type)
case *dst.File:
for _, decl := range x.Decls {
comments := decl.Decorations().Start.All()
decl.Decorations().Start.Clear()
for _, def := range x.Decls {
comments := def.Decorations().Start.All()
def.Decorations().Start.Clear()
// For any reason, sometimes it retrieves the comment duplicated 🤷
commentMap := make(map[string]bool)
for _, c := range comments {
if _, ok := commentMap[c]; !ok {
commentMap[c] = true
decl.Decorations().Start.Append(d.rxpsuff.ReplaceAllString(c, "$1"))
def.Decorations().Start.Append(d.rxpsuff.ReplaceAllString(c, "$1"))
if d.replace != "" {
decl.Decorations().Start.Append(d.rxp.ReplaceAllString(c, d.replace+"$1"))
def.Decorations().Start.Append(d.rxp.ReplaceAllString(c, d.replace+"$1"))
}
}
}