cue: add Raw option
This allows Syntax to produce evaluatable
output. The format is still not ideal, but it
is something and useful in its own right.
Change-Id: I9c320f22cacb22ee1eb2652903e8f2ee02886973
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2712
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/export_test.go b/cue/export_test.go
index de354eb..1646c5c 100644
--- a/cue/export_test.go
+++ b/cue/export_test.go
@@ -410,11 +410,7 @@
if err != nil {
t.Fatal(err)
}
- v := inst.Value()
- ctx := r.index().newContext()
-
- opts := options{raw: true}
- b, err := format.Node(export(ctx, v.path.v, opts), format.Simplify())
+ b, err := format.Node(inst.Value().Syntax(Raw()), format.Simplify())
if err != nil {
log.Fatal(err)
}
diff --git a/cue/gen.go b/cue/gen.go
index 89394c0..9d22f41 100644
--- a/cue/gen.go
+++ b/cue/gen.go
@@ -201,7 +201,7 @@
return
}
- n := instances[0].Value().Syntax(cue.Hidden(true), cue.Concrete(false))
+ n := instances[0].Value().Syntax(cue.Raw())
b, err := cueformat.Node(n)
if err != nil {
log.Fatal(err)
diff --git a/cue/instance.go b/cue/instance.go
index 44a2ccb..39dc797 100644
--- a/cue/instance.go
+++ b/cue/instance.go
@@ -94,14 +94,12 @@
}
func (inst *Instance) eval(ctx *context) evaluated {
- // TODO: remove manifest here? It may be good here, though not consistent.
+ // TODO: remove manifest here?
v := ctx.manifest(inst.rootValue)
if s, ok := v.(*structLit); ok && s.emit != nil {
- v = ctx.manifest(s.emit)
+ return s.emit.evalPartial(ctx)
}
- // manifest to benefit from validation step.
- // TODO: consider not doing manifest.
- return ctx.manifest(v)
+ return v
}
func init() {
diff --git a/cue/types.go b/cue/types.go
index 9133853..bb40acf 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -725,6 +725,9 @@
// Syntax converts the possibly partially evaluated value into syntax. This
// can use used to print the value with package format.
func (v Value) Syntax(opts ...Option) ast.Node {
+ // TODO: the default should ideally be simplified representation that
+ // exactly represents the value. The latter can currently only be
+ // ensured with Raw().
if v.path == nil || v.path.cache == nil {
return nil
}
@@ -1375,8 +1378,6 @@
if !p.hasHidden {
p.omitHidden = true
}
- } else {
- p.raw = true
}
}
}
@@ -1387,6 +1388,11 @@
return func(p *options) { p.disallowCycles = disallow }
}
+// Raw tells Syntax to generate the value as is without any simplifications.
+func Raw() Option {
+ return func(p *options) { p.raw = true }
+}
+
// All indicates that all fields and values should be included in processing
// even if they can be elided or omitted.
func All() Option {