From b39a2d00f8c817616fa6d7067d09ffcbbd027771 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Thu, 20 May 2021 16:13:45 +0200 Subject: [PATCH] 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 Reviewed-by: Marcel van Lohuizen --- cmd/cue/cmd/eval.go | 21 ++++++++++---- cmd/cue/cmd/testdata/script/issue986.txt | 36 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 cmd/cue/cmd/testdata/script/issue986.txt diff --git a/cmd/cue/cmd/eval.go b/cmd/cue/cmd/eval.go index fcebb7f9648..d5159ef4439 100644 --- a/cmd/cue/cmd/eval.go +++ b/cmd/cue/cmd/eval.go @@ -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" @@ -117,6 +118,20 @@ 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)) } @@ -124,12 +139,6 @@ func runEval(cmd *Command, args []string) error { 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) diff --git a/cmd/cue/cmd/testdata/script/issue986.txt b/cmd/cue/cmd/testdata/script/issue986.txt new file mode 100644 index 00000000000..e3f7e40f728 --- /dev/null +++ b/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