mirror of https://github.com/grafana/grafana
coremodels: Automatically generate coremodel registries (#50057)
* coremodel: Generate static registry * Actually make codegen work Also, remove the per-coremodel assignability test from generator set. * Make wire gen depend on cue gen This is necessary now that we're generating a wire set as part of coremodel registry generation. * Add wire inject bits to http server * s/staticregistry/registry/ * move to static and dynamic wording * Move registry type into registry package * Use static registry in http handler * Oi commentspull/50889/head
parent
8a6ed3d81b
commit
4c4aa95d38
@ -1,28 +0,0 @@ |
||||
// This file is autogenerated. DO NOT EDIT.
|
||||
//
|
||||
// To regenerate, run "make gen-cue" from repository root.
|
||||
//
|
||||
// Derived from the Thema lineage at pkg/coremodel/dashboard
|
||||
|
||||
package dashboard |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/cuectx" |
||||
"github.com/grafana/thema" |
||||
) |
||||
|
||||
func TestSchemaAssignability(t *testing.T) { |
||||
lin, err := Lineage(cuectx.ProvideThemaLibrary()) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
sch := thema.SchemaP(lin, currentVersion) |
||||
|
||||
err = thema.AssignableTo(sch, &Model{}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
package registry_test |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/framework/coremodel/registry" |
||||
"github.com/grafana/thema" |
||||
) |
||||
|
||||
func TestSchemaAssignability(t *testing.T) { |
||||
reg, err := registry.ProvideGeneric() |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
for _, cm := range reg.List() { |
||||
tcm := cm |
||||
t.Run(tcm.Lineage().Name(), func(t *testing.T) { |
||||
err := thema.AssignableTo(tcm.CurrentSchema(), tcm.GoType()) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
}) |
||||
} |
||||
} |
@ -0,0 +1,40 @@ |
||||
package registry |
||||
|
||||
import ( |
||||
"github.com/grafana/thema" |
||||
) |
||||
|
||||
// ProvideStatic provides access to individual coremodels via explicit method calls.
|
||||
//
|
||||
// Prefer this to the ProvideGeneric type when your code works with known,
|
||||
// specific coremodels(s), rather than generically across all of them. This
|
||||
// allows standard Go static analysis tools to determine which code is depending
|
||||
// on particular coremodels.
|
||||
//
|
||||
// This will use the default Grafana thema.Library, defined in pkg/cuectx, which
|
||||
// will avoid duplicate parsing of Thema CUE schemas. If you need control over the
|
||||
// thema.Library in use, use ProvideStaticWithLib instead.
|
||||
func ProvideStatic() (*Static, error) { |
||||
return provideStatic(nil) |
||||
} |
||||
|
||||
// ProvideStaticWithLib is the same as ProvideStatic, but
|
||||
// allows control over the thema.Library used to initialize the underlying
|
||||
// coremodels.
|
||||
//
|
||||
// Prefer ProvideStatic unless you absolutely need this control.
|
||||
func ProvideStaticWithLib(lib thema.Library) (*Static, error) { |
||||
return provideStatic(&lib) |
||||
} |
||||
|
||||
// ProvideGeneric provides a simple Generic registry of all coremodels.
|
||||
//
|
||||
// Prefer this to the static ProvideStatic when your code needs to
|
||||
// work with all coremodels generically, rather than specific coremodels.
|
||||
func ProvideGeneric() (*Generic, error) { |
||||
return provideGeneric() |
||||
} |
||||
|
||||
// NOTE - no ProvideRegistryWithLib is defined because there are no anticipated
|
||||
// cases where a caller would need to operate generically across all coremodels,
|
||||
// and control the library they're initialized with. If that changes, add one.
|
@ -0,0 +1,91 @@ |
||||
// This file is autogenerated. DO NOT EDIT.
|
||||
//
|
||||
// Generated by pkg/framework/coremodel/gen.go
|
||||
// Run "make gen-cue" from repository root to regenerate.
|
||||
|
||||
package registry |
||||
|
||||
import ( |
||||
"sync" |
||||
|
||||
"github.com/google/wire" |
||||
|
||||
"github.com/grafana/grafana/pkg/coremodel/dashboard" |
||||
"github.com/grafana/grafana/pkg/cuectx" |
||||
"github.com/grafana/grafana/pkg/framework/coremodel" |
||||
"github.com/grafana/thema" |
||||
) |
||||
|
||||
// CoremodelSet contains all of the wire-style providers related to coremodels.
|
||||
var CoremodelSet = wire.NewSet( |
||||
ProvideStatic, |
||||
ProvideGeneric, |
||||
) |
||||
|
||||
var ( |
||||
staticOnce sync.Once |
||||
defaultStatic *Static |
||||
defaultStaticErr error |
||||
|
||||
genericOnce sync.Once |
||||
defaultGeneric *Generic |
||||
defaultGenericErr error |
||||
) |
||||
|
||||
// Static is a registry that provides access to individual coremodels via
|
||||
// explicit method calls, to aid with static analysis.
|
||||
type Static struct { |
||||
dashboard *dashboard.Coremodel |
||||
} |
||||
|
||||
// type guards
|
||||
var ( |
||||
_ coremodel.Interface = &dashboard.Coremodel{} |
||||
) |
||||
|
||||
// Dashboard returns the dashboard coremodel. The return value is guaranteed to
|
||||
// implement coremodel.Interface.
|
||||
func (s *Static) Dashboard() *dashboard.Coremodel { |
||||
return s.dashboard |
||||
} |
||||
|
||||
func provideStatic(lib *thema.Library) (*Static, error) { |
||||
if lib == nil { |
||||
staticOnce.Do(func() { |
||||
defaultStatic, defaultStaticErr = doProvideStatic(cuectx.ProvideThemaLibrary()) |
||||
}) |
||||
return defaultStatic, defaultStaticErr |
||||
} |
||||
|
||||
return doProvideStatic(*lib) |
||||
} |
||||
|
||||
func doProvideStatic(lib thema.Library) (*Static, error) { |
||||
var err error |
||||
reg := &Static{} |
||||
|
||||
reg.dashboard, err = dashboard.New(lib) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return reg, nil |
||||
} |
||||
|
||||
func provideGeneric() (*Generic, error) { |
||||
ereg, err := provideStatic(nil) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
genericOnce.Do(func() { |
||||
defaultGeneric, defaultGenericErr = doProvideGeneric(ereg) |
||||
}) |
||||
return defaultGeneric, defaultGenericErr |
||||
} |
||||
|
||||
func doProvideGeneric(ereg *Static) (*Generic, error) { |
||||
return NewRegistry( |
||||
ereg.Dashboard(), |
||||
) |
||||
} |
@ -1,19 +0,0 @@ |
||||
package staticregistry |
||||
|
||||
import ( |
||||
"github.com/grafana/grafana/pkg/coremodel/dashboard" |
||||
"github.com/grafana/grafana/pkg/framework/coremodel" |
||||
) |
||||
|
||||
// ProvideRegistry provides a simple static Registry for coremodels.
|
||||
// Coremodels have to be manually added.
|
||||
// TODO dynamism
|
||||
func ProvideRegistry( |
||||
dashboard *dashboard.Coremodel, |
||||
) (*coremodel.Registry, error) { |
||||
cmlist := []coremodel.Interface{ |
||||
dashboard, |
||||
} |
||||
|
||||
return coremodel.NewRegistry(cmlist...) |
||||
} |
Loading…
Reference in new issue