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:
sam boyer
2023-01-31 19:40:15 -05:00
committed by GitHub
parent 0f0a53fbbb
commit 30b4205521
25 changed files with 273 additions and 543 deletions

View File

@ -10,45 +10,20 @@ import (
"github.com/grafana/thema"
)
type OneToOne codejen.OneToOne[*DefForGen]
type OneToMany codejen.OneToMany[*DefForGen]
type ManyToOne codejen.ManyToOne[*DefForGen]
type ManyToMany codejen.ManyToMany[*DefForGen]
type OneToOne codejen.OneToOne[kindsys.Kind]
type OneToMany codejen.OneToMany[kindsys.Kind]
type ManyToOne codejen.ManyToOne[kindsys.Kind]
type ManyToMany codejen.ManyToMany[kindsys.Kind]
// 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 &DefForGen{
SomeDef: def,
lin: lin,
}, nil
}
// DefForGen wraps [kindsys.SomeDef] to provide trivial caching of
// the lineage declared by the kind (nil for raw kinds).
// 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.SomeDef].
func (def *DefForGen) Lineage() thema.Lineage {
return def.lin
}
// ForLatestSchema returns a [SchemaForGen] for the latest schema in this
// DefForGen's lineage.
func (def *DefForGen) ForLatestSchema() SchemaForGen {
comm := def.Properties.Common()
// 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: def.Lineage().Latest(),
Schema: k.Lineage().Latest(),
IsGroup: comm.LineageIsGroup,
}
}
@ -81,6 +56,8 @@ func SlashHeaderMapper(maingen string) codejen.FileMapper {
// SchemaForGen is an intermediate values type for jennies that holds both a thema.Schema,
// and values relevant to generating the schema that should properly, eventually, be in
// thema itself.
//
// TODO this will be replaced by thema-native constructs
type SchemaForGen struct {
// The PascalCase name of the schematized type.
Name string