[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 85 additions and 74 deletions

4
go.mod
View File

@ -13,7 +13,7 @@ replace k8s.io/client-go => k8s.io/client-go v0.22.1
require ( require (
cloud.google.com/go/storage v1.14.0 cloud.google.com/go/storage v1.14.0
cuelang.org/go v0.3.2 cuelang.org/go v0.4.0
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0 github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.10.0 github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.10.0
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
@ -164,6 +164,7 @@ require (
github.com/gogo/googleapis v1.4.1 // indirect github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/status v1.0.3 // indirect github.com/gogo/status v1.0.3 // indirect
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect github.com/gomodule/redigo v2.0.0+incompatible // indirect
@ -210,6 +211,7 @@ require (
github.com/prometheus/exporter-toolkit v0.6.1 // indirect github.com/prometheus/exporter-toolkit v0.6.1 // indirect
github.com/prometheus/node_exporter v1.0.0-rc.0.0.20200428091818-01054558c289 // indirect github.com/prometheus/node_exporter v1.0.0-rc.0.0.20200428091818-01054558c289 // indirect
github.com/prometheus/procfs v0.6.0 // indirect github.com/prometheus/procfs v0.6.0 // indirect
github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc // indirect
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be // indirect github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be // indirect
github.com/rs/cors v1.8.0 // indirect github.com/rs/cors v1.8.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect github.com/russross/blackfriday/v2 v2.0.1 // indirect

7
go.sum
View File

@ -58,8 +58,8 @@ code.cloudfoundry.org/clock v1.0.0/go.mod h1:QD9Lzhd/ux6eNQVUDVRJX/RKTigpewimNYB
collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE=
contrib.go.opencensus.io/exporter/ocagent v0.6.0/go.mod h1:zmKjrJcdo0aYcVS7bmEeSEBLPA9YJp5bjrofdU3pIXs= contrib.go.opencensus.io/exporter/ocagent v0.6.0/go.mod h1:zmKjrJcdo0aYcVS7bmEeSEBLPA9YJp5bjrofdU3pIXs=
contrib.go.opencensus.io/exporter/prometheus v0.3.0/go.mod h1:rpCPVQKhiyH8oomWgm34ZmgIdZa8OVYO5WAIygPbBBE= contrib.go.opencensus.io/exporter/prometheus v0.3.0/go.mod h1:rpCPVQKhiyH8oomWgm34ZmgIdZa8OVYO5WAIygPbBBE=
cuelang.org/go v0.3.2 h1:/Am5yFDwqnaEi+g942OPM1M4/qtfVSm49wtkQbeh5Z4= cuelang.org/go v0.4.0 h1:GLJblw6m2WGGCA3k1v6Wbk9gTOt2qto48ahO2MmSd6I=
cuelang.org/go v0.3.2/go.mod h1:jvMO35Q4D2D3m2ujAmKESICaYkjMbu5+D+2zIGuWTpQ= cuelang.org/go v0.4.0/go.mod h1:tz/edkPi+T37AZcb5GlPY+WJkL6KiDlDVupKwL3vvjs=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8= gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
@ -1007,6 +1007,7 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZ
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI= github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@ -2026,6 +2027,8 @@ github.com/prometheus/prometheus v1.8.2-0.20210915140241-bd217c58a735/go.mod h1:
github.com/prometheus/statsd_exporter v0.20.0/go.mod h1:YL3FWCG8JBBtaUSxAg4Gz2ZYu22bS84XM89ZQXXTWmQ= github.com/prometheus/statsd_exporter v0.20.0/go.mod h1:YL3FWCG8JBBtaUSxAg4Gz2ZYu22bS84XM89ZQXXTWmQ=
github.com/prometheus/statsd_exporter v0.21.0/go.mod h1:rbT83sZq2V+p73lHhPZfMc3MLCHmSHelCh9hSGYNLTQ= github.com/prometheus/statsd_exporter v0.21.0/go.mod h1:rbT83sZq2V+p73lHhPZfMc3MLCHmSHelCh9hSGYNLTQ=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc h1:gSVONBi2HWMFXCa9jFdYvYk7IwW/mTLxWOF7rXS4LO0=
github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc/go.mod h1:KbKfKPy2I6ecOIGA9apfheFv14+P3RSmmQvshofQyMY=
github.com/rafaeljusto/redigomock v0.0.0-20190202135759-257e089e14a1/go.mod h1:JaY6n2sDr+z2WTsXkOmNRUfDy6FN0L6Nk7x06ndm4tY= github.com/rafaeljusto/redigomock v0.0.0-20190202135759-257e089e14a1/go.mod h1:JaY6n2sDr+z2WTsXkOmNRUfDy6FN0L6Nk7x06ndm4tY=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ= github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q= github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q=

View File

@ -39,27 +39,32 @@ type exitWithCode struct {
code int code int
} }
var serverFs = flag.NewFlagSet("server", flag.ContinueOnError)
func (e exitWithCode) Error() string { func (e exitWithCode) Error() string {
return e.reason return e.reason
} }
func RunServer(opt ServerOptions) int { func RunServer(opt ServerOptions) int {
var ( var (
configFile = flag.String("config", "", "path to config file") configFile = serverFs.String("config", "", "path to config file")
homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory") homePath = serverFs.String("homepath", "", "path to grafana install/home path, defaults to working directory")
pidFile = flag.String("pidfile", "", "path to pid file") pidFile = serverFs.String("pidfile", "", "path to pid file")
packaging = flag.String("packaging", "unknown", "describes the way Grafana was installed") packaging = serverFs.String("packaging", "unknown", "describes the way Grafana was installed")
v = flag.Bool("v", false, "prints current version and exits") v = serverFs.Bool("v", false, "prints current version and exits")
vv = flag.Bool("vv", false, "prints current version, all dependencies and exits") vv = serverFs.Bool("vv", false, "prints current version, all dependencies and exits")
profile = flag.Bool("profile", false, "Turn on pprof profiling") profile = serverFs.Bool("profile", false, "Turn on pprof profiling")
profileAddr = flag.String("profile-addr", "localhost", "Define custom address for profiling") profileAddr = serverFs.String("profile-addr", "localhost", "Define custom address for profiling")
profilePort = flag.Uint64("profile-port", 6060, "Define custom port for profiling") profilePort = serverFs.Uint64("profile-port", 6060, "Define custom port for profiling")
tracing = flag.Bool("tracing", false, "Turn on tracing") tracing = serverFs.Bool("tracing", false, "Turn on tracing")
tracingFile = flag.String("tracing-file", "trace.out", "Define tracing output file") tracingFile = serverFs.String("tracing-file", "trace.out", "Define tracing output file")
) )
flag.Parse() if err := serverFs.Parse(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return 1
}
if *v || *vv { if *v || *vv {
fmt.Printf("Version %s (commit: %s, branch: %s)\n", opt.Version, opt.Commit, opt.BuildBranch) fmt.Printf("Version %s (commit: %s, branch: %s)\n", opt.Version, opt.Commit, opt.BuildBranch)
@ -161,7 +166,7 @@ func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnos
} }
s, err := server.Initialize(setting.CommandLineArgs{ s, err := server.Initialize(setting.CommandLineArgs{
Config: configFile, HomePath: homePath, Args: flag.Args(), Config: configFile, HomePath: homePath, Args: serverFs.Args(),
}, server.Options{ }, server.Options{
PidFile: pidFile, Version: opt.Version, Commit: opt.Commit, BuildBranch: opt.BuildBranch, PidFile: pidFile, Version: opt.Version, Commit: opt.Commit, BuildBranch: opt.BuildBranch,
}, api.ServerOptions{}) }, api.ServerOptions{})

View File

@ -6,12 +6,12 @@ import (
"io/fs" "io/fs"
"path/filepath" "path/filepath"
"cuelang.org/go/cue" "cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load" "cuelang.org/go/cue/load"
"github.com/grafana/grafana" "github.com/grafana/grafana"
) )
var rt = &cue.Runtime{} var ctx = cuecontext.New()
// Families can have variants, where more typing information narrows the // Families can have variants, where more typing information narrows the
// possible values for certain keys in schemas. These are a meta-property // possible values for certain keys in schemas. These are a meta-property

View File

@ -56,15 +56,15 @@ func baseDashboardFamily(p BaseLoadPaths) (cue.Value, error) {
Module: "github.com/grafana/grafana", Module: "github.com/grafana/grafana",
Dir: filepath.Join(prefix, dashboardDir), Dir: filepath.Join(prefix, dashboardDir),
} }
inst, err := rt.Build(load.Instances(nil, cfg)[0]) inst := ctx.BuildInstance(load.Instances(nil, cfg)[0])
if err != nil { if inst.Err() != nil {
cueError := schema.WrapCUEError(err) cueError := schema.WrapCUEError(inst.Err())
if err != nil { if inst.Err() != nil {
return cue.Value{}, cueError return cue.Value{}, cueError
} }
} }
famval := inst.Value().LookupPath(cue.MakePath(cue.Str("Family"))) famval := inst.LookupPath(cue.MakePath(cue.Str("Family")))
if !famval.Exists() { if !famval.Exists() {
return cue.Value{}, errors.New("dashboard schema family did not exist at expected path in expected file") 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 == "" { if name == "" {
name = "resource" name = "resource"
} }
rv, err := rt.Compile(name, r.Value) rv := ctx.CompileString(r.Value.(string), cue.Filename(name))
if err != nil { if rv.Err() != nil {
return err 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. // CUE returns the cue.Value representing the actual schema.

View File

@ -12,11 +12,11 @@ import (
// definitions on which all Grafana scuemata rely. // definitions on which all Grafana scuemata rely.
// //
// TODO probably cache this or something // 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) overlay := make(map[string]load.Source)
if err := toOverlay(filepath.Join(prefix, "grafana"), p.BaseCueFS, overlay); err != nil { if err := toOverlay(filepath.Join(prefix, "grafana"), p.BaseCueFS, overlay); err != nil {
return nil, err return cue.Value{}, err
} }
cfg := &load.Config{ cfg := &load.Config{
@ -37,10 +37,12 @@ func getBaseScuemata(p BaseLoadPaths) (*cue.Instance, error) {
// this "/". // this "/".
Dir: prefix, 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", "scuemata.cue"),
filepath.Join(prefix, "grafana", "cue", "scuemata", "panel-plugin.cue"), filepath.Join(prefix, "grafana", "cue", "scuemata", "panel-plugin.cue"),
}, cfg)[0]) }, cfg)[0])
return res, res.Err()
} }
func buildGenericScuemata(famval cue.Value) (schema.VersionedCueSchema, error) { func buildGenericScuemata(famval cue.Value) (schema.VersionedCueSchema, error) {
@ -106,11 +108,11 @@ func (gvs *genericVersionedSchema) Validate(r schema.Resource) error {
if name == "" { if name == "" {
name = "resource" name = "resource"
} }
rv, err := rt.Compile(name, r.Value) rv := ctx.CompileString(r.Value.(string), cue.Filename(name))
if err != nil { if rv.Err() != nil {
return err 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. // CUE returns the cue.Value representing the actual schema.

View File

@ -91,7 +91,7 @@ func TestDevenvDashboardValidity(t *testing.T) {
} }
t.Run(filepath.Base(path), func(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 { if err != nil {
// Testify trims errors to short length. We want the full text // Testify trims errors to short length. We want the full text
errstr := errors.Details(err, nil) errstr := errors.Details(err, nil)
@ -191,16 +191,16 @@ func TestAllPluginsInDist(t *testing.T) {
Dir: filepath.Join(prefix, dashboardDir, "dist"), Dir: filepath.Join(prefix, dashboardDir, "dist"),
Package: "dist", Package: "dist",
} }
inst, err := rt.Build(load.Instances(nil, cfg)[0]) inst := ctx.BuildInstance(load.Instances(nil, cfg)[0])
require.NoError(t, err) require.NoError(t, inst.Err())
dinst, err := rt.Compile("str", ` dinst := ctx.CompileString(`
Family: compose: Panel: {} Family: compose: Panel: {}
typs: [for typ, _ in Family.compose.Panel {typ}] typs: [for typ, _ in Family.compose.Panel {typ}]
`) `, cue.Filename("str"))
require.NoError(t, err) 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) j, err := cuejson.Marshal(typs)
require.NoError(t, err) require.NoError(t, err)

View File

@ -20,7 +20,7 @@ import (
func mapPanelModel(id string, vcs schema.VersionedCueSchema) cue.Value { func mapPanelModel(id string, vcs schema.VersionedCueSchema) cue.Value {
maj, min := vcs.Version() maj, min := vcs.Version()
// Ignore err return, this can't fail to compile // 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: { in: {
type: %q type: %q
v: { v: {
@ -39,10 +39,10 @@ func mapPanelModel(id string, vcs schema.VersionedCueSchema) cue.Value {
fieldConfig: defaults: custom: in.model.PanelFieldConfig 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 // 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) { 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 return nil, err
} }
pmf := base.Value().LookupPath(cue.MakePath(cue.Def("#PanelFamily"))) pmf := base.LookupPath(cue.MakePath(cue.Def("#PanelFamily")))
if !pmf.Exists() { if !pmf.Exists() {
return nil, errors.New("could not locate #PanelFamily definition") 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) li := load.Instances([]string{filepath.Join("/", dpath, "models.cue")}, cfg)
imod, err := rt.Build(li[0]) imod := ctx.BuildInstance(li[0])
if err != nil { if imod.Err() != nil {
return err return imod.Err()
} }
// Get the Family declaration in the models.cue file... // 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() { if !pmod.Exists() {
return fmt.Errorf("%s does not contain a declaration of its models at path 'Family'", path) return fmt.Errorf("%s does not contain a declaration of its models at path 'Family'", path)
} }

View File

@ -8,11 +8,12 @@ import (
"strings" "strings"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
errs "cuelang.org/go/cue/errors" errs "cuelang.org/go/cue/errors"
cuejson "cuelang.org/go/pkg/encoding/json" cuejson "cuelang.org/go/pkg/encoding/json"
) )
var rt = &cue.Runtime{} var ctx = cuecontext.New()
// CueError wraps Errors caused by malformed cue files. // CueError wraps Errors caused by malformed cue files.
type CueError struct { type CueError struct {
@ -281,11 +282,11 @@ func ApplyDefaults(r Resource, scue cue.Value) (Resource, error) {
if name == "" { if name == "" {
name = "resource" name = "resource"
} }
rv, err := rt.Compile(name, r.Value) rv := ctx.CompileString(r.Value.(string), cue.Filename(name))
if err != nil { if rv.Err() != nil {
return r, err return r, rv.Err()
} }
rvUnified := rv.Value().Unify(scue) rvUnified := rv.Unify(scue)
re, err := convertCUEValueToString(rvUnified) re, err := convertCUEValueToString(rvUnified)
if err != nil { if err != nil {
return r, err return r, err
@ -314,11 +315,11 @@ func TrimDefaults(r Resource, scue cue.Value) (Resource, error) {
if name == "" { if name == "" {
name = "resource" name = "resource"
} }
rvInstance, err := rt.Compile(name, r.Value) rvInstance := ctx.CompileString(r.Value.(string), cue.Filename(name))
if err != nil { if rvInstance.Err() != nil {
return r, err return r, rvInstance.Err()
} }
rv, _, err := removeDefaultHelper(scue, rvInstance.Value()) rv, _, err := removeDefaultHelper(scue, rvInstance)
if err != nil { if err != nil {
return r, err 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) { func removeDefaultHelper(inputdef cue.Value, input cue.Value) (cue.Value, bool, error) {
// To include all optional fields, we need to use inputdef for iteration, // To include all optional fields, we need to use inputdef for iteration,
// since the lookuppath with optional field doesn't work very well // since the lookuppath with optional field doesn't work very well
rvInstance, err := rt.Compile("helper", []byte{}) rvInstance := ctx.CompileString("", cue.Filename("helper"))
if err != nil { if rvInstance.Err() != nil {
return input, false, err return input, false, rvInstance.Err()
} }
rv := rvInstance.Value() 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, ",")) iterlistContent := fmt.Sprintf("[%s]", strings.Join(iterlist, ","))
liInstance, err := rt.Compile("resource", []byte(iterlistContent)) liInstance := ctx.CompileString(iterlistContent, cue.Filename("resource"))
if err != nil { if liInstance.Err() != nil {
return rv, false, err return rv, false, err
} }
return liInstance.Value(), false, nil return liInstance, false, nil
default: default:
if isCueValueEqual(inputdef, input) { if isCueValueEqual(inputdef, input) {
return input, true, nil return input, true, nil

View File

@ -30,13 +30,12 @@ func TestGenerate(t *testing.T) {
for _, c := range cases { for _, c := range cases {
t.Run(c.Name+" apply default value", func(t *testing.T) { t.Run(c.Name+" apply default value", func(t *testing.T) {
var r cue.Runtime scmInstance := ctx.CompileString(c.CUE, cue.Filename(c.Name+".cue"))
scmInstance, err := r.Compile(c.Name+".cue", c.CUE) if scmInstance.Err() != nil {
if err != nil { t.Fatal(scmInstance.Err())
t.Fatal(err)
} }
inputResource := Resource{Value: c.Trimmed} inputResource := Resource{Value: c.Trimmed}
out, err := ApplyDefaults(inputResource, scmInstance.Value()) out, err := ApplyDefaults(inputResource, scmInstance)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -50,13 +49,12 @@ func TestGenerate(t *testing.T) {
for _, c := range cases { for _, c := range cases {
t.Run(c.Name+" trim default value", func(t *testing.T) { t.Run(c.Name+" trim default value", func(t *testing.T) {
var r cue.Runtime scmInstance := ctx.CompileString(c.CUE, cue.Filename(c.Name+".cue"))
scmInstance, err := r.Compile(c.Name+".cue", c.CUE) if scmInstance.Err() != nil {
if err != nil { t.Fatal(scmInstance.Err())
t.Fatal(err)
} }
inputResource := Resource{Value: c.Full} inputResource := Resource{Value: c.Full}
out, err := TrimDefaults(inputResource, scmInstance.Value()) out, err := TrimDefaults(inputResource, scmInstance)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }