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:
@@ -20,11 +20,10 @@ import (
|
||||
"github.com/grafana/cuetsy/ts"
|
||||
"github.com/grafana/cuetsy/ts/ast"
|
||||
gcgen "github.com/grafana/grafana/pkg/codegen"
|
||||
"github.com/grafana/grafana/pkg/cuectx"
|
||||
"github.com/grafana/thema"
|
||||
)
|
||||
|
||||
var lib = thema.NewLibrary(cuecontext.New())
|
||||
|
||||
const sep = string(filepath.Separator)
|
||||
|
||||
var tsroot, cmroot, groot string
|
||||
@@ -46,6 +45,7 @@ func init() {
|
||||
// Generate Go and Typescript implementations for all coremodels, and populate the
|
||||
// coremodel static registry.
|
||||
func main() {
|
||||
rt := cuectx.GrafanaThemaRuntime()
|
||||
if len(os.Args) > 1 {
|
||||
fmt.Fprintf(os.Stderr, "coremodel code generator does not currently accept any arguments\n, got %q", os.Args)
|
||||
os.Exit(1)
|
||||
@@ -60,7 +60,7 @@ func main() {
|
||||
var lins []*gcgen.CoremodelDeclaration
|
||||
for _, item := range items {
|
||||
if item.IsDir() {
|
||||
lin, err := gcgen.ExtractLineage(filepath.Join(cmroot, item.Name(), "coremodel.cue"), lib)
|
||||
lin, err := gcgen.ExtractLineage(filepath.Join(cmroot, item.Name(), "coremodel.cue"), rt)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "could not process coremodel dir %s: %s\n", filepath.Join(cmroot, item.Name()), err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"cuelang.org/go/cue/load"
|
||||
"github.com/grafana/thema/kernel"
|
||||
tload "github.com/grafana/thema/load"
|
||||
|
||||
"github.com/grafana/grafana/pkg/cuectx"
|
||||
@@ -24,7 +23,7 @@ var defaultFramework cue.Value
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
defaultFramework, err = doLoadFrameworkCUE(cuectx.ProvideCUEContext())
|
||||
defaultFramework, err = doLoadFrameworkCUE(cuectx.GrafanaCUEContext())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -106,81 +105,3 @@ func CUEFrameworkWithContext(ctx *cue.Context) cue.Value {
|
||||
v, _ := doLoadFrameworkCUE(ctx) // nolint:errcheck
|
||||
return v
|
||||
}
|
||||
|
||||
// Mux takes a coremodel and returns a Thema version muxer that, given a byte
|
||||
// slice containing any version of schema for that coremodel, will translate it
|
||||
// to the Interface.CurrentSchema() version, and optionally decode it onto the
|
||||
// Interface.GoType().
|
||||
//
|
||||
// By default, JSON decoding will be used, and the filename given to any input
|
||||
// bytes (shown in errors, which may be user-facing) will be
|
||||
// "<name>.<encoding>", e.g. dashboard.json.
|
||||
func Mux(cm Interface, opts ...MuxOption) kernel.InputKernel {
|
||||
c := &muxConfig{}
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
cfg := kernel.InputKernelConfig{
|
||||
Typ: cm.GoType(),
|
||||
Lineage: cm.Lineage(),
|
||||
To: cm.CurrentSchema().Version(),
|
||||
}
|
||||
|
||||
switch c.decodetyp {
|
||||
case "", "json": // json by default
|
||||
if c.filename == "" {
|
||||
c.filename = fmt.Sprintf("%s.json", cm.Lineage().Name())
|
||||
}
|
||||
cfg.Loader = kernel.NewJSONDecoder(c.filename)
|
||||
case "yaml":
|
||||
if c.filename == "" {
|
||||
c.filename = fmt.Sprintf("%s.yaml", cm.Lineage().Name())
|
||||
}
|
||||
cfg.Loader = kernel.NewYAMLDecoder(c.filename)
|
||||
default:
|
||||
panic("")
|
||||
}
|
||||
|
||||
mux, err := kernel.NewInputKernel(cfg)
|
||||
if err != nil {
|
||||
// Barring a fundamental bug in Thema's schema->Go type assignability checker or
|
||||
// a direct attempt by a Grafana dev to get around the invariants of coremodel codegen,
|
||||
// this should be unreachable. (And even the latter case should be caught elsewhere
|
||||
// by tests).
|
||||
panic(err)
|
||||
}
|
||||
return mux
|
||||
}
|
||||
|
||||
// A MuxOption defines options that may be specified only at initial
|
||||
// construction of a Lineage via BindLineage.
|
||||
type MuxOption muxOption
|
||||
|
||||
// Internal representation of MuxOption.
|
||||
type muxOption func(c *muxConfig)
|
||||
|
||||
type muxConfig struct {
|
||||
filename string
|
||||
decodetyp string
|
||||
}
|
||||
|
||||
// YAML indicates that the resulting Mux should look for YAML in input bytes,
|
||||
// rather than the default JSON.
|
||||
func YAML() MuxOption {
|
||||
return func(c *muxConfig) {
|
||||
c.decodetyp = "yaml"
|
||||
}
|
||||
}
|
||||
|
||||
// Filename specifies the filename that is given to input bytes passing through
|
||||
// the mux.
|
||||
//
|
||||
// The filename has no impact on mux behavior, but is used in user-facing error
|
||||
// output, such as schema validation failures. Thus, it is recommended to pick a
|
||||
// name that will make sense in the context a user is expected to see the error.
|
||||
func Filename(name string) MuxOption {
|
||||
return func(c *muxConfig) {
|
||||
c.filename = name
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,19 +17,20 @@ var CoremodelSet = wire.NewSet(
|
||||
// NewBase provides a registry of all coremodels, without any composition of
|
||||
// plugin-defined schemas.
|
||||
//
|
||||
// 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.
|
||||
// The returned registry will use Grafana's singleton [thema.Runtime],
|
||||
// returned from [cuectx.GrafanaThemaRuntime].
|
||||
func NewBase() *Base {
|
||||
return provideBase(nil)
|
||||
}
|
||||
|
||||
// NewBaseWithLib is the same as NewBase, but allows control over the
|
||||
// thema.Library used to initialize the underlying coremodels.
|
||||
// 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.
|
||||
func NewBaseWithLib(lib thema.Library) *Base {
|
||||
return provideBase(&lib)
|
||||
//
|
||||
// TODO it's OK to export this if it's ever actually needed
|
||||
func NewBaseWithRuntime(rt *thema.Runtime) *Base {
|
||||
return provideBase(rt)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -37,15 +38,15 @@ var (
|
||||
defaultBase *Base
|
||||
)
|
||||
|
||||
func provideBase(lib *thema.Library) *Base {
|
||||
if lib == nil {
|
||||
func provideBase(rt *thema.Runtime) *Base {
|
||||
if rt == nil {
|
||||
baseOnce.Do(func() {
|
||||
defaultBase = doProvideBase(cuectx.ProvideThemaLibrary())
|
||||
defaultBase = doProvideBase(cuectx.GrafanaThemaRuntime())
|
||||
})
|
||||
return defaultBase
|
||||
}
|
||||
|
||||
return doProvideBase(*lib)
|
||||
return doProvideBase(rt)
|
||||
}
|
||||
|
||||
// All returns a slice of all registered coremodels.
|
||||
|
||||
@@ -57,23 +57,23 @@ func (b *Base) Pluginmeta() *pluginmeta.Coremodel {
|
||||
return b.pluginmeta
|
||||
}
|
||||
|
||||
func doProvideBase(lib thema.Library) *Base {
|
||||
func doProvideBase(rt *thema.Runtime) *Base {
|
||||
var err error
|
||||
reg := &Base{}
|
||||
|
||||
reg.dashboard, err = dashboard.New(lib)
|
||||
reg.dashboard, err = dashboard.New(rt)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error while initializing dashboard coremodel: %s", err))
|
||||
}
|
||||
reg.all = append(reg.all, reg.dashboard)
|
||||
|
||||
reg.playlist, err = playlist.New(lib)
|
||||
reg.playlist, err = playlist.New(rt)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error while initializing playlist coremodel: %s", err))
|
||||
}
|
||||
reg.all = append(reg.all, reg.playlist)
|
||||
|
||||
reg.pluginmeta, err = pluginmeta.New(lib)
|
||||
reg.pluginmeta, err = pluginmeta.New(rt)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error while initializing pluginmeta coremodel: %s", err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user