[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 11:09:09 -04: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)
}
+16 -15
View File
@@ -8,11 +8,12 @@ import (
"strings"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
errs "cuelang.org/go/cue/errors"
cuejson "cuelang.org/go/pkg/encoding/json"
)
var rt = &cue.Runtime{}
var ctx = cuecontext.New()
// CueError wraps Errors caused by malformed cue files.
type CueError struct {
@@ -281,11 +282,11 @@ func ApplyDefaults(r Resource, scue cue.Value) (Resource, error) {
if name == "" {
name = "resource"
}
rv, err := rt.Compile(name, r.Value)
if err != nil {
return r, err
rv := ctx.CompileString(r.Value.(string), cue.Filename(name))
if rv.Err() != nil {
return r, rv.Err()
}
rvUnified := rv.Value().Unify(scue)
rvUnified := rv.Unify(scue)
re, err := convertCUEValueToString(rvUnified)
if err != nil {
return r, err
@@ -314,11 +315,11 @@ func TrimDefaults(r Resource, scue cue.Value) (Resource, error) {
if name == "" {
name = "resource"
}
rvInstance, err := rt.Compile(name, r.Value)
if err != nil {
return r, err
rvInstance := ctx.CompileString(r.Value.(string), cue.Filename(name))
if rvInstance.Err() != nil {
return r, rvInstance.Err()
}
rv, _, err := removeDefaultHelper(scue, rvInstance.Value())
rv, _, err := removeDefaultHelper(scue, rvInstance)
if err != nil {
return r, err
}
@@ -337,9 +338,9 @@ func isCueValueEqual(inputdef cue.Value, input cue.Value) bool {
func removeDefaultHelper(inputdef cue.Value, input cue.Value) (cue.Value, bool, error) {
// To include all optional fields, we need to use inputdef for iteration,
// since the lookuppath with optional field doesn't work very well
rvInstance, err := rt.Compile("helper", []byte{})
if err != nil {
return input, false, err
rvInstance := ctx.CompileString("", cue.Filename("helper"))
if rvInstance.Err() != nil {
return input, false, rvInstance.Err()
}
rv := rvInstance.Value()
@@ -404,11 +405,11 @@ func removeDefaultHelper(inputdef cue.Value, input cue.Value) (cue.Value, bool,
}
}
iterlistContent := fmt.Sprintf("[%s]", strings.Join(iterlist, ","))
liInstance, err := rt.Compile("resource", []byte(iterlistContent))
if err != nil {
liInstance := ctx.CompileString(iterlistContent, cue.Filename("resource"))
if liInstance.Err() != nil {
return rv, false, err
}
return liInstance.Value(), false, nil
return liInstance, false, nil
default:
if isCueValueEqual(inputdef, input) {
return input, true, nil
+8 -10
View File
@@ -30,13 +30,12 @@ func TestGenerate(t *testing.T) {
for _, c := range cases {
t.Run(c.Name+" apply default value", func(t *testing.T) {
var r cue.Runtime
scmInstance, err := r.Compile(c.Name+".cue", c.CUE)
if err != nil {
t.Fatal(err)
scmInstance := ctx.CompileString(c.CUE, cue.Filename(c.Name+".cue"))
if scmInstance.Err() != nil {
t.Fatal(scmInstance.Err())
}
inputResource := Resource{Value: c.Trimmed}
out, err := ApplyDefaults(inputResource, scmInstance.Value())
out, err := ApplyDefaults(inputResource, scmInstance)
if err != nil {
t.Fatal(err)
}
@@ -50,13 +49,12 @@ func TestGenerate(t *testing.T) {
for _, c := range cases {
t.Run(c.Name+" trim default value", func(t *testing.T) {
var r cue.Runtime
scmInstance, err := r.Compile(c.Name+".cue", c.CUE)
if err != nil {
t.Fatal(err)
scmInstance := ctx.CompileString(c.CUE, cue.Filename(c.Name+".cue"))
if scmInstance.Err() != nil {
t.Fatal(scmInstance.Err())
}
inputResource := Resource{Value: c.Full}
out, err := TrimDefaults(inputResource, scmInstance.Value())
out, err := TrimDefaults(inputResource, scmInstance)
if err != nil {
t.Fatal(err)
}