Skip to content

Commit e47dda7

Browse files
committed
cue: don't print definitions in concrete mode
it is especially important to omit them when printing data. Issue #40 Change-Id: Ib6ce21cbaf4987d43036605b0e2fa3a62ec2ef63 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2873 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 5134dee commit e47dda7

File tree

8 files changed

+25
-10
lines changed

8 files changed

+25
-10
lines changed

cmd/cue/cmd/testdata/export/export.cue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package export
22

3-
def: *1 | int
3+
One :: 1
4+
5+
def: *One | int
46

57
a: string
68
a: "foo"

cmd/cue/cmd/testdata/hello/data.cue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package hello
22

3-
who: "World"
3+
who :: "World"

cmd/cue/cmd/testdata/hello/eval.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
who: "World"
1+
who :: "World"
22
message: "Hello World!"
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
who: "World"
21
message: "Hello World!"

cmd/cue/cmd/testdata/hello/export.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"who": "World",
32
"message": "Hello World!"
43
}

cue/export.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,9 @@ func (p *exporter) structure(x *structLit, addTempl bool) (ret *ast.StructLit, e
621621
f.Optional = token.NoSpace.Pos()
622622
}
623623
if a.definition {
624+
if p.mode.omitHidden || p.mode.concrete {
625+
continue
626+
}
624627
f.Token = token.ISA
625628
}
626629
if a.feature&hidden != 0 && p.mode.concrete && p.mode.omitHidden {

cue/format/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (f *formatter) decl(decl ast.Decl) {
173173
if lastSize != len(f.labelBuf) {
174174
f.print(formfeed)
175175
}
176-
if !regular && first.Pos().RelPos() < token.Newline {
176+
if !regular && first.Pos().RelPos() < token.Newline && len(f.output) > 0 {
177177
f.print(newline, nooverride)
178178
}
179179

cue/types.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,13 @@ func (v Value) structValOpts(ctx *context, o options) (structValue, *bottom) {
10601060
f := label(0)
10611061
for _, a := range obj.arcs {
10621062
f |= a.feature
1063-
if o.omitOptional && a.optional {
1063+
if a.optional && o.omitOptional {
1064+
needFilter = true
1065+
break
1066+
}
1067+
if a.definition && (o.omitHidden || o.concrete) {
10641068
needFilter = true
1069+
break
10651070
}
10661071
}
10671072
needFilter = needFilter || f&hidden != 0
@@ -1071,10 +1076,17 @@ func (v Value) structValOpts(ctx *context, o options) (structValue, *bottom) {
10711076
arcs := make([]arc, len(obj.arcs))
10721077
k := 0
10731078
for _, a := range obj.arcs {
1074-
if a.feature&hidden == 0 && !a.optional {
1075-
arcs[k] = a
1076-
k++
1079+
if a.definition && (o.omitHidden || o.concrete) {
1080+
continue
1081+
}
1082+
if a.feature&hidden != 0 && o.omitHidden {
1083+
continue
1084+
}
1085+
if o.omitOptional && a.optional {
1086+
continue
10771087
}
1088+
arcs[k] = a
1089+
k++
10781090
}
10791091
arcs = arcs[:k]
10801092
obj = &structLit{

0 commit comments

Comments
 (0)