From 5641029a4afe3c368ecf6d38ee36c998083acc07 Mon Sep 17 00:00:00 2001 From: sam boyer Date: Tue, 11 Oct 2022 05:39:29 -0400 Subject: [PATCH] coremodels: Always take runtime arg for NewBase() (#56677) --- pkg/api/dashboard_test.go | 16 +++++----- .../coremodel/registry/assignability_test.go | 2 +- pkg/framework/coremodel/registry/provide.go | 32 ++++++------------- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/pkg/api/dashboard_test.go b/pkg/api/dashboard_test.go index 7ce47fccb90..e0099a39337 100644 --- a/pkg/api/dashboard_test.go +++ b/pkg/api/dashboard_test.go @@ -64,7 +64,7 @@ func TestGetHomeDashboard(t *testing.T) { SQLStore: mockstore.NewSQLStoreMock(), preferenceService: prefService, dashboardVersionService: dashboardVersionService, - Coremodels: registry.NewBase(), + Coremodels: registry.NewBase(nil), } tests := []struct { @@ -148,7 +148,7 @@ func TestDashboardAPIEndpoint(t *testing.T) { Features: featuremgmt.WithFeatures(), DashboardService: dashboardService, dashboardVersionService: fakeDashboardVersionService, - Coremodels: registry.NewBase(), + Coremodels: registry.NewBase(nil), } setUp := func() { @@ -270,7 +270,7 @@ func TestDashboardAPIEndpoint(t *testing.T) { DashboardService: dashboardService, dashboardVersionService: fakeDashboardVersionService, Features: featuremgmt.WithFeatures(), - Coremodels: registry.NewBase(), + Coremodels: registry.NewBase(nil), } setUp := func() { @@ -913,7 +913,7 @@ func TestDashboardAPIEndpoint(t *testing.T) { AccessControl: accesscontrolmock.New(), DashboardService: dashboardService, Features: featuremgmt.WithFeatures(), - Coremodels: registry.NewBase(), + Coremodels: registry.NewBase(nil), } hs.callGetDashboard(sc) @@ -968,7 +968,7 @@ func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, pr ), DashboardService: dashboardService, Features: featuremgmt.WithFeatures(), - Coremodels: registry.NewBase(), + Coremodels: registry.NewBase(nil), } hs.callGetDashboard(sc) @@ -1034,7 +1034,7 @@ func postDashboardScenario(t *testing.T, desc string, url string, routePattern s DashboardService: dashboardService, folderService: folderService, Features: featuremgmt.WithFeatures(), - Coremodels: registry.NewBase(), + Coremodels: registry.NewBase(nil), } sc := setupScenarioContext(t, url) @@ -1067,7 +1067,7 @@ func postDiffScenario(t *testing.T, desc string, url string, routePattern string SQLStore: sqlmock, dashboardVersionService: fakeDashboardVersionService, Features: featuremgmt.WithFeatures(), - Coremodels: registry.NewBase(), + Coremodels: registry.NewBase(nil), } sc := setupScenarioContext(t, url) @@ -1106,7 +1106,7 @@ func restoreDashboardVersionScenario(t *testing.T, desc string, url string, rout SQLStore: sqlStore, Features: featuremgmt.WithFeatures(), dashboardVersionService: fakeDashboardVersionService, - Coremodels: registry.NewBase(), + Coremodels: registry.NewBase(nil), } sc := setupScenarioContext(t, url) diff --git a/pkg/framework/coremodel/registry/assignability_test.go b/pkg/framework/coremodel/registry/assignability_test.go index 38f51f8a30c..8d83e6284d7 100644 --- a/pkg/framework/coremodel/registry/assignability_test.go +++ b/pkg/framework/coremodel/registry/assignability_test.go @@ -8,7 +8,7 @@ import ( ) func TestSchemaAssignability(t *testing.T) { - reg := registry.NewBase() + reg := registry.NewBase(nil) for _, cm := range reg.All() { tcm := cm diff --git a/pkg/framework/coremodel/registry/provide.go b/pkg/framework/coremodel/registry/provide.go index 9b71925ddad..e64e314987f 100644 --- a/pkg/framework/coremodel/registry/provide.go +++ b/pkg/framework/coremodel/registry/provide.go @@ -14,34 +14,22 @@ var CoremodelSet = wire.NewSet( 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 ( baseOnce sync.Once defaultBase *Base ) -func provideBase(rt *thema.Runtime) *Base { - if rt == nil { +// NewBase provides a registry of all coremodels, without any composition of +// 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() { - defaultBase = doProvideBase(cuectx.GrafanaThemaRuntime()) + defaultBase = doProvideBase(allrt) }) return defaultBase }