[Chore] update to cue v0.4.0 (#39641)

* change global flag to flagset

* update pr with comments

* replace flag.args by flagset

* fix build

* migrate the schema package to use cue 4.0

* fix the load package
This commit is contained in:
ying-jeanne
2021-09-28 17:09:09 +02:00
committed by GitHub
parent 05bb451a1d
commit 580cdc46fc
10 changed files with 85 additions and 74 deletions
+2 -2
View File
@@ -6,12 +6,12 @@ import (
"io/fs"
"path/filepath"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load"
"github.com/grafana/grafana"
)
var rt = &cue.Runtime{}
var ctx = cuecontext.New()
// Families can have variants, where more typing information narrows the
// possible values for certain keys in schemas. These are a meta-property
+9 -9
View File
@@ -56,15 +56,15 @@ func baseDashboardFamily(p BaseLoadPaths) (cue.Value, error) {
Module: "github.com/grafana/grafana",
Dir: filepath.Join(prefix, dashboardDir),
}
inst, err := rt.Build(load.Instances(nil, cfg)[0])
if err != nil {
cueError := schema.WrapCUEError(err)
if err != nil {
inst := ctx.BuildInstance(load.Instances(nil, cfg)[0])
if inst.Err() != nil {
cueError := schema.WrapCUEError(inst.Err())
if inst.Err() != nil {
return cue.Value{}, cueError
}
}
famval := inst.Value().LookupPath(cue.MakePath(cue.Str("Family")))
famval := inst.LookupPath(cue.MakePath(cue.Str("Family")))
if !famval.Exists() {
return cue.Value{}, errors.New("dashboard schema family did not exist at expected path in expected file")
}
@@ -148,11 +148,11 @@ func (cds *compositeDashboardSchema) Validate(r schema.Resource) error {
if name == "" {
name = "resource"
}
rv, err := rt.Compile(name, r.Value)
if err != nil {
return err
rv := ctx.CompileString(r.Value.(string), cue.Filename(name))
if rv.Err() != nil {
return rv.Err()
}
return cds.actual.Unify(rv.Value()).Validate(cue.Concrete(true))
return cds.actual.Unify(rv).Validate(cue.Concrete(true))
}
// CUE returns the cue.Value representing the actual schema.
+9 -7
View File
@@ -12,11 +12,11 @@ import (
// definitions on which all Grafana scuemata rely.
//
// TODO probably cache this or something
func getBaseScuemata(p BaseLoadPaths) (*cue.Instance, error) {
func getBaseScuemata(p BaseLoadPaths) (cue.Value, error) {
overlay := make(map[string]load.Source)
if err := toOverlay(filepath.Join(prefix, "grafana"), p.BaseCueFS, overlay); err != nil {
return nil, err
return cue.Value{}, err
}
cfg := &load.Config{
@@ -37,10 +37,12 @@ func getBaseScuemata(p BaseLoadPaths) (*cue.Instance, error) {
// this "/".
Dir: prefix,
}
return rt.Build(load.Instances([]string{
res := ctx.BuildInstance(load.Instances([]string{
filepath.Join(prefix, "grafana", "cue", "scuemata", "scuemata.cue"),
filepath.Join(prefix, "grafana", "cue", "scuemata", "panel-plugin.cue"),
}, cfg)[0])
return res, res.Err()
}
func buildGenericScuemata(famval cue.Value) (schema.VersionedCueSchema, error) {
@@ -106,11 +108,11 @@ func (gvs *genericVersionedSchema) Validate(r schema.Resource) error {
if name == "" {
name = "resource"
}
rv, err := rt.Compile(name, r.Value)
if err != nil {
return err
rv := ctx.CompileString(r.Value.(string), cue.Filename(name))
if rv.Err() != nil {
return rv.Err()
}
return gvs.actual.Unify(rv.Value()).Validate(cue.Concrete(true))
return gvs.actual.Unify(rv).Validate(cue.Concrete(true))
}
// CUE returns the cue.Value representing the actual schema.
+7 -7
View File
@@ -91,7 +91,7 @@ func TestDevenvDashboardValidity(t *testing.T) {
}
t.Run(filepath.Base(path), func(t *testing.T) {
err := sch.Validate(schema.Resource{Value: byt, Name: path})
err := sch.Validate(schema.Resource{Value: string(byt), Name: path})
if err != nil {
// Testify trims errors to short length. We want the full text
errstr := errors.Details(err, nil)
@@ -191,16 +191,16 @@ func TestAllPluginsInDist(t *testing.T) {
Dir: filepath.Join(prefix, dashboardDir, "dist"),
Package: "dist",
}
inst, err := rt.Build(load.Instances(nil, cfg)[0])
require.NoError(t, err)
inst := ctx.BuildInstance(load.Instances(nil, cfg)[0])
require.NoError(t, inst.Err())
dinst, err := rt.Compile("str", `
dinst := ctx.CompileString(`
Family: compose: Panel: {}
typs: [for typ, _ in Family.compose.Panel {typ}]
`)
require.NoError(t, err)
`, cue.Filename("str"))
require.NoError(t, dinst.Err())
typs := dinst.Value().Unify(inst.Value()).LookupPath(cue.MakePath(cue.Str("typs")))
typs := dinst.Unify(inst).LookupPath(cue.MakePath(cue.Str("typs")))
j, err := cuejson.Marshal(typs)
require.NoError(t, err)
+8 -8
View File
@@ -20,7 +20,7 @@ import (
func mapPanelModel(id string, vcs schema.VersionedCueSchema) cue.Value {
maj, min := vcs.Version()
// Ignore err return, this can't fail to compile
inter, _ := rt.Compile(fmt.Sprintf("%s-glue-panelComposition", id), fmt.Sprintf(`
inter := ctx.CompileString(fmt.Sprintf(`
in: {
type: %q
v: {
@@ -39,10 +39,10 @@ func mapPanelModel(id string, vcs schema.VersionedCueSchema) cue.Value {
fieldConfig: defaults: custom: in.model.PanelFieldConfig
}
}
`, id, maj, min))
`, id, maj, min), cue.Filename(fmt.Sprintf("%s-glue-panelComposition", id)))
// TODO validate, especially with #PanelModel
return inter.Value().FillPath(cue.MakePath(cue.Str("in"), cue.Str("model")), vcs.CUE()).LookupPath(cue.MakePath(cue.Str(("result"))))
return inter.FillPath(cue.MakePath(cue.Str("in"), cue.Str("model")), vcs.CUE()).LookupPath(cue.MakePath(cue.Str(("result"))))
}
func loadPanelScuemata(p BaseLoadPaths) (map[string]cue.Value, error) {
@@ -60,7 +60,7 @@ func loadPanelScuemata(p BaseLoadPaths) (map[string]cue.Value, error) {
return nil, err
}
pmf := base.Value().LookupPath(cue.MakePath(cue.Def("#PanelFamily")))
pmf := base.LookupPath(cue.MakePath(cue.Def("#PanelFamily")))
if !pmf.Exists() {
return nil, errors.New("could not locate #PanelFamily definition")
}
@@ -108,13 +108,13 @@ func loadPanelScuemata(p BaseLoadPaths) (map[string]cue.Value, error) {
}
li := load.Instances([]string{filepath.Join("/", dpath, "models.cue")}, cfg)
imod, err := rt.Build(li[0])
if err != nil {
return err
imod := ctx.BuildInstance(li[0])
if imod.Err() != nil {
return imod.Err()
}
// Get the Family declaration in the models.cue file...
pmod := imod.Value().LookupPath(cue.MakePath(cue.Str("Panel")))
pmod := imod.LookupPath(cue.MakePath(cue.Str("Panel")))
if !pmod.Exists() {
return fmt.Errorf("%s does not contain a declaration of its models at path 'Family'", path)
}