coremodels: Combine static and generic registries (#53246)

* Stop generating non-dynamic registry code

* Remove generic, errors, s/static/base/

* Sort during codegen, not runtime

* Not a method call

* Precisiate a comment

* Remove generic registry, fix assignability test
This commit is contained in:
sam boyer
2022-08-04 00:04:54 +04:00
committed by GitHub
parent d54e55ea9a
commit b11f66b4bb
9 changed files with 101 additions and 247 deletions
+45 -26
View File
@@ -1,40 +1,59 @@
package registry
import (
"sync"
"github.com/google/wire"
"github.com/grafana/grafana/pkg/cuectx"
"github.com/grafana/grafana/pkg/framework/coremodel"
"github.com/grafana/thema"
)
// ProvideStatic provides access to individual coremodels via explicit method calls.
// CoremodelSet contains all of the wire-style providers related to coremodels.
var CoremodelSet = wire.NewSet(
NewBase,
)
// NewBase provides a registry of all coremodels, without any composition of
// plugin-defined schemas.
//
// 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)
// The returned registry will use the default Grafana thema.Library, defined in
// pkg/cuectx. If you need control over the thema.Library used by the coremodel
// lineages, use NewBaseWithLib instead.
func NewBase() *Base {
return provideBase(nil)
}
// ProvideStaticWithLib is the same as ProvideStatic, but
// allows control over the thema.Library used to initialize the underlying
// coremodels.
// NewBaseWithLib is the same as NewBase, 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)
// Prefer NewBase unless you absolutely need this control.
func NewBaseWithLib(lib thema.Library) *Base {
return provideBase(&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()
var (
baseOnce sync.Once
defaultBase *Base
)
func provideBase(lib *thema.Library) *Base {
if lib == nil {
baseOnce.Do(func() {
defaultBase = doProvideBase(cuectx.ProvideThemaLibrary())
})
return defaultBase
}
return doProvideBase(*lib)
}
// 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.
// All returns a slice of all registered coremodels.
//
// Prefer this method when operating generically across all coremodels.
//
// The returned slice is sorted lexicographically by coremodel name. It should
// not be modified.
func (s *Base) All() []coremodel.Interface {
return s.all
}