Skip to content

Commit b39a2d0

Browse files
committed
cmd/cue/cmd: avoid roundtrip when printing non-CUE in eval
Fixes #986 Change-Id: I85961ab2fc23828ad44814479e7057723aed03de Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9862 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent a4e0f52 commit b39a2d0

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

cmd/cue/cmd/eval.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/spf13/cobra"
2121

2222
"cuelang.org/go/cue"
23+
"cuelang.org/go/cue/build"
2324
"cuelang.org/go/cue/format"
2425
"cuelang.org/go/internal"
2526
"cuelang.org/go/internal/encoding"
@@ -117,19 +118,27 @@ func runEval(cmd *Command, args []string) error {
117118
}
118119
v := iter.value()
119120

121+
errHeader := func() {
122+
if id != "" {
123+
fmt.Fprintf(cmd.OutOrStderr(), "// %s\n", id)
124+
}
125+
}
126+
if b.outFile.Encoding != build.CUE {
127+
err := e.Encode(v)
128+
if err != nil {
129+
errHeader()
130+
exitOnErr(cmd, err, false)
131+
}
132+
continue
133+
}
134+
120135
if flagConcrete.Bool(cmd) {
121136
syn = append(syn, cue.Concrete(true))
122137
}
123138
if flagHidden.Bool(cmd) || flagAll.Bool(cmd) {
124139
syn = append(syn, cue.Hidden(true))
125140
}
126141

127-
errHeader := func() {
128-
if id != "" {
129-
fmt.Fprintf(cmd.OutOrStderr(), "// %s\n", id)
130-
}
131-
}
132-
133142
if len(b.expressions) > 1 {
134143
b, _ := format.Node(b.expressions[i%len(b.expressions)])
135144
id = string(b)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
# Hidden values are dropped when outputting CUE. This is fine in eval for
3+
# debugging, but not when the final result needs to be compiled again to be
4+
# converted to another format.
5+
6+
cue eval in.cue --out yaml
7+
cmp stdout expect-stdout
8+
9+
-- in.cue --
10+
#Foo: {
11+
a: string
12+
b: string
13+
ab: "\(a)\(b)"
14+
}
15+
16+
{
17+
a: "aaa"
18+
b: "bbb"
19+
} & #Foo
20+
21+
#Bar: {
22+
_c: string
23+
_d: string
24+
cd: "\(_c)\(_d)"
25+
}
26+
27+
{
28+
_c: "ccc"
29+
_d: "ddd"
30+
} & #Bar
31+
32+
-- expect-stdout --
33+
a: aaa
34+
b: bbb
35+
ab: aaabbb
36+
cd: cccddd

0 commit comments

Comments
 (0)