coremodels: Update to latest Thema with generics (#56602)

* Update thema to latest

* Deal with s/Library/*Runtime/

* Commit new, working results of codegen
This commit is contained in:
sam boyer
2022-10-11 09:45:07 +01:00
committed by GitHub
parent 668cb25b82
commit e5a6547a94
26 changed files with 171 additions and 247 deletions
+4 -4
View File
@@ -18,8 +18,8 @@ var currentVersion = thema.SV({{ .LatestSeqv }}, {{ .LatestSchv }})
{{- if .IsComposed }}//
// This is the base variant of the schema. It does not include any composed
// plugin schemas.{{ end }}
func Lineage(lib thema.Library, opts ...thema.BindOption) (thema.Lineage, error) {
return cuectx.LoadGrafanaInstancesWithThema(filepath.Join("pkg", "coremodel", "{{ .Name }}"), cueFS, lib, opts...)
func Lineage(rt *thema.Runtime, opts ...thema.BindOption) (thema.Lineage, error) {
return cuectx.LoadGrafanaInstancesWithThema(filepath.Join("pkg", "coremodel", "{{ .Name }}"), cueFS, rt, opts...)
}
var _ thema.LineageFactory = Lineage
@@ -52,8 +52,8 @@ func (c *Coremodel) GoType() interface{} {
// Note that this function does not cache, and initially loading a Thema lineage
// can be expensive. As such, the Grafana backend should prefer to access this
// coremodel through a registry (pkg/framework/coremodel/registry), which does cache.
func New(lib thema.Library) (*Coremodel, error) {
lin, err := Lineage(lib)
func New(rt *thema.Runtime) (*Coremodel, error) {
lin, err := Lineage(rt)
if err != nil {
return nil, err
}
+2 -2
View File
@@ -42,12 +42,12 @@ func (b *Base) {{ .TitleName }}() *{{ .Name }}.Coremodel {
}
{{end}}
func doProvideBase(lib thema.Library) *Base {
func doProvideBase(rt *thema.Runtime) *Base {
var err error
reg := &Base{}
{{range .Coremodels }}
reg.{{ .Name }}, err = {{ .Name }}.New(lib)
reg.{{ .Name }}, err = {{ .Name }}.New(rt)
if err != nil {
panic(fmt.Sprintf("error while initializing {{ .Name }} coremodel: %s", err))
}
+2 -2
View File
@@ -7,6 +7,6 @@ var currentVersion{{ .SlotName }} = thema.SV({{ .LatestSeqv }}, {{ .LatestSchv }
// {{ .SlotName }}Lineage returns the Thema lineage for the {{ .PluginID }} {{ .PluginType }} plugin's
// {{ .SlotName }} ["github.com/grafana/grafana/pkg/framework/coremodel".Slot] implementation.
func {{ .SlotName }}Lineage(lib thema.Library, opts ...thema.BindOption) (thema.Lineage, error) {
return cuectx.LoadGrafanaInstancesWithThema(filepath.Join("public", "app", "{{ .Name }}"), cueFS, lib, opts...)
func {{ .SlotName }}Lineage(rt *thema.Runtime, opts ...thema.BindOption) (thema.Lineage, error) {
return cuectx.LoadGrafanaInstancesWithThema(filepath.Join("public", "app", "{{ .Name }}"), cueFS, rt, opts...)
}
+6 -6
View File
@@ -19,14 +19,14 @@ var ptree *pfs.Tree
var plugFS embed.FS
// PluginTree returns the plugin tree representing the statically analyzable contents of the {{ .PluginID }} plugin.
func PluginTree(lib *thema.Library) *pfs.Tree {
func PluginTree(rt *thema.Runtime) *pfs.Tree {
var err error
if lib == nil {
if rt == nil || rt == cuectx.GrafanaThemaRuntime() {
parseOnce.Do(func() {
ptree, err = pfs.ParsePluginFS(plugFS, cuectx.ProvideThemaLibrary())
ptree, err = pfs.ParsePluginFS(plugFS, cuectx.GrafanaThemaRuntime())
})
} else {
ptree, err = pfs.ParsePluginFS(plugFS, cuectx.ProvideThemaLibrary())
ptree, err = pfs.ParsePluginFS(plugFS, rt)
}
if err != nil {
@@ -40,8 +40,8 @@ func PluginTree(lib *thema.Library) *pfs.Tree {
{{ $pluginfo := . }}{{ range $slot := .SlotImpls }}
// {{ .SlotName }}Lineage returns the Thema lineage for the {{ $pluginfo.PluginID }} {{ $pluginfo.PluginType }} plugin's
// {{ .SlotName }} ["github.com/grafana/grafana/pkg/framework/coremodel".Slot] implementation.
func {{ .SlotName }}Lineage(lib *thema.Library, opts ...thema.BindOption) (thema.Lineage, error) {
t := PluginTree(lib)
func {{ .SlotName }}Lineage(rt *thema.Runtime, opts ...thema.BindOption) (thema.Lineage, error) {
t := PluginTree(rt)
lin, has := t.RootPlugin().SlotImplementations()["{{ .SlotName }}"]
if !has {
panic("unreachable: lineage for {{ .SlotName }} does not exist, but code is only generated for existing lineages")
+4 -4
View File
@@ -10,22 +10,22 @@ import (
"github.com/grafana/thema"
)
func makeTreeOrPanic(path string, pkgname string, lib thema.Library) *pfs.Tree {
func makeTreeOrPanic(path string, pkgname string, rt *thema.Runtime) *pfs.Tree {
sub, err := fs.Sub(grafana.CueSchemaFS, path)
if err != nil {
panic("could not create fs sub to " + path)
}
tree, err := pfs.ParsePluginFS(sub, lib)
tree, err := pfs.ParsePluginFS(sub, rt)
if err != nil {
panic(fmt.Sprintf("error parsing plugin metadata for %s: %s", pkgname, err))
}
return tree
}
func coreTreeList(lib thema.Library) pfs.TreeList{
func coreTreeList(rt *thema.Runtime) pfs.TreeList{
return pfs.TreeList{
{{- range .Plugins }}
makeTreeOrPanic("{{ .Path }}", "{{ .PkgName }}", lib),
makeTreeOrPanic("{{ .Path }}", "{{ .PkgName }}", rt),
{{- end }}
}
}
+2 -2
View File
@@ -8,8 +8,8 @@ import (
{{ if .NoAlias }}{{ .PkgName }} {{end}}"{{ .Path }}"{{ end }}
)
func coreTreeLoaders() []func(*thema.Library) *pfs.Tree{
return []func(*thema.Library) *pfs.Tree{
func coreTreeLoaders() []func(*thema.Runtime) *pfs.Tree{
return []func(*thema.Runtime) *pfs.Tree{
{{- range .Plugins }}
{{ .PkgName }}.PluginTree,{{ end }}
}