Skip to content

Commit

Permalink
cmd/cue/cmd: avoid roundtrip when printing non-CUE in eval
Browse files Browse the repository at this point in the history
Fixes #986

Change-Id: I85961ab2fc23828ad44814479e7057723aed03de
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9862
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed May 21, 2021
1 parent a4e0f52 commit b39a2d0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
21 changes: 15 additions & 6 deletions cmd/cue/cmd/eval.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/cobra"

"cuelang.org/go/cue"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/format"
"cuelang.org/go/internal"
"cuelang.org/go/internal/encoding"
Expand Down Expand Up @@ -117,19 +118,27 @@ func runEval(cmd *Command, args []string) error {
}
v := iter.value()

errHeader := func() {
if id != "" {
fmt.Fprintf(cmd.OutOrStderr(), "// %s\n", id)
}
}
if b.outFile.Encoding != build.CUE {
err := e.Encode(v)
if err != nil {
errHeader()
exitOnErr(cmd, err, false)
}
continue
}

if flagConcrete.Bool(cmd) {
syn = append(syn, cue.Concrete(true))
}
if flagHidden.Bool(cmd) || flagAll.Bool(cmd) {
syn = append(syn, cue.Hidden(true))
}

errHeader := func() {
if id != "" {
fmt.Fprintf(cmd.OutOrStderr(), "// %s\n", id)
}
}

if len(b.expressions) > 1 {
b, _ := format.Node(b.expressions[i%len(b.expressions)])
id = string(b)
Expand Down
36 changes: 36 additions & 0 deletions cmd/cue/cmd/testdata/script/issue986.txt
@@ -0,0 +1,36 @@

# Hidden values are dropped when outputting CUE. This is fine in eval for
# debugging, but not when the final result needs to be compiled again to be
# converted to another format.

cue eval in.cue --out yaml
cmp stdout expect-stdout

-- in.cue --
#Foo: {
a: string
b: string
ab: "\(a)\(b)"
}

{
a: "aaa"
b: "bbb"
} & #Foo

#Bar: {
_c: string
_d: string
cd: "\(_c)\(_d)"
}

{
_c: "ccc"
_d: "ddd"
} & #Bar

-- expect-stdout --
a: aaa
b: bbb
ab: aaabbb
cd: cccddd

0 comments on commit b39a2d0

Please sign in to comment.