coremodels: Always take runtime arg for NewBase() (#56677)

This commit is contained in:
sam boyer 2022-10-11 05:39:29 -04:00 committed by GitHub
parent 10a34a041c
commit 5641029a4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 31 deletions

View File

@ -64,7 +64,7 @@ func TestGetHomeDashboard(t *testing.T) {
SQLStore: mockstore.NewSQLStoreMock(), SQLStore: mockstore.NewSQLStoreMock(),
preferenceService: prefService, preferenceService: prefService,
dashboardVersionService: dashboardVersionService, dashboardVersionService: dashboardVersionService,
Coremodels: registry.NewBase(), Coremodels: registry.NewBase(nil),
} }
tests := []struct { tests := []struct {
@ -148,7 +148,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
Features: featuremgmt.WithFeatures(), Features: featuremgmt.WithFeatures(),
DashboardService: dashboardService, DashboardService: dashboardService,
dashboardVersionService: fakeDashboardVersionService, dashboardVersionService: fakeDashboardVersionService,
Coremodels: registry.NewBase(), Coremodels: registry.NewBase(nil),
} }
setUp := func() { setUp := func() {
@ -270,7 +270,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
DashboardService: dashboardService, DashboardService: dashboardService,
dashboardVersionService: fakeDashboardVersionService, dashboardVersionService: fakeDashboardVersionService,
Features: featuremgmt.WithFeatures(), Features: featuremgmt.WithFeatures(),
Coremodels: registry.NewBase(), Coremodels: registry.NewBase(nil),
} }
setUp := func() { setUp := func() {
@ -913,7 +913,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
AccessControl: accesscontrolmock.New(), AccessControl: accesscontrolmock.New(),
DashboardService: dashboardService, DashboardService: dashboardService,
Features: featuremgmt.WithFeatures(), Features: featuremgmt.WithFeatures(),
Coremodels: registry.NewBase(), Coremodels: registry.NewBase(nil),
} }
hs.callGetDashboard(sc) hs.callGetDashboard(sc)
@ -968,7 +968,7 @@ func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, pr
), ),
DashboardService: dashboardService, DashboardService: dashboardService,
Features: featuremgmt.WithFeatures(), Features: featuremgmt.WithFeatures(),
Coremodels: registry.NewBase(), Coremodels: registry.NewBase(nil),
} }
hs.callGetDashboard(sc) hs.callGetDashboard(sc)
@ -1034,7 +1034,7 @@ func postDashboardScenario(t *testing.T, desc string, url string, routePattern s
DashboardService: dashboardService, DashboardService: dashboardService,
folderService: folderService, folderService: folderService,
Features: featuremgmt.WithFeatures(), Features: featuremgmt.WithFeatures(),
Coremodels: registry.NewBase(), Coremodels: registry.NewBase(nil),
} }
sc := setupScenarioContext(t, url) sc := setupScenarioContext(t, url)
@ -1067,7 +1067,7 @@ func postDiffScenario(t *testing.T, desc string, url string, routePattern string
SQLStore: sqlmock, SQLStore: sqlmock,
dashboardVersionService: fakeDashboardVersionService, dashboardVersionService: fakeDashboardVersionService,
Features: featuremgmt.WithFeatures(), Features: featuremgmt.WithFeatures(),
Coremodels: registry.NewBase(), Coremodels: registry.NewBase(nil),
} }
sc := setupScenarioContext(t, url) sc := setupScenarioContext(t, url)
@ -1106,7 +1106,7 @@ func restoreDashboardVersionScenario(t *testing.T, desc string, url string, rout
SQLStore: sqlStore, SQLStore: sqlStore,
Features: featuremgmt.WithFeatures(), Features: featuremgmt.WithFeatures(),
dashboardVersionService: fakeDashboardVersionService, dashboardVersionService: fakeDashboardVersionService,
Coremodels: registry.NewBase(), Coremodels: registry.NewBase(nil),
} }
sc := setupScenarioContext(t, url) sc := setupScenarioContext(t, url)

View File

@ -8,7 +8,7 @@ import (
) )
func TestSchemaAssignability(t *testing.T) { func TestSchemaAssignability(t *testing.T) {
reg := registry.NewBase() reg := registry.NewBase(nil)
for _, cm := range reg.All() { for _, cm := range reg.All() {
tcm := cm tcm := cm

View File

@ -14,34 +14,22 @@ var CoremodelSet = wire.NewSet(
NewBase, NewBase,
) )
// NewBase provides a registry of all coremodels, without any composition of
// plugin-defined schemas.
//
// The returned registry will use Grafana's singleton [thema.Runtime],
// returned from [cuectx.GrafanaThemaRuntime].
func NewBase() *Base {
return provideBase(nil)
}
// NewBaseWithRuntime is the same as NewBase, but allows control over the
// [thema.Runtime] used to initialize the underlying coremodels.
//
// Prefer NewBase unless you absolutely need this control.
//
// TODO it's OK to export this if it's ever actually needed
func NewBaseWithRuntime(rt *thema.Runtime) *Base {
return provideBase(rt)
}
var ( var (
baseOnce sync.Once baseOnce sync.Once
defaultBase *Base defaultBase *Base
) )
func provideBase(rt *thema.Runtime) *Base { // NewBase provides a registry of all coremodels, without any composition of
if rt == nil { // plugin-defined schemas.
//
// All calling code within grafana/grafana is expected to use Grafana's
// singleton [thema.Runtime], returned from [cuectx.GrafanaThemaRuntime]. If nil
// is passed, the singleton will be used.
func NewBase(rt *thema.Runtime) *Base {
allrt := cuectx.GrafanaThemaRuntime()
if rt == nil || rt == allrt {
baseOnce.Do(func() { baseOnce.Do(func() {
defaultBase = doProvideBase(cuectx.GrafanaThemaRuntime()) defaultBase = doProvideBase(allrt)
}) })
return defaultBase return defaultBase
} }