Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
internal/core/export: fix optional output bug
Browse files Browse the repository at this point in the history
Fixes #662

Change-Id: Iac1198c0eabd9fcdbfa89db5c20d2c03d931afc5
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8325
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jan 28, 2021
1 parent b8b4892 commit 76a72c4
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 14 deletions.
2 changes: 2 additions & 0 deletions internal/core/export/testdata/adt.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,5 @@ _|_ // e3: index out of range [2] with length 2
[1, _|_]
}
}
== Eval
_|_ // e3: index out of range [2] with length 2
9 changes: 9 additions & 0 deletions internal/core/export/testdata/alias.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ X="d-2": E=[D="cue"]: C="foo\(baz)": {
Y="a-c": 5
"d-2": {}
}
== Eval
{
"a-b": 4
foo: 4
bar?: Y
baz: 3
Y="a-c": 5
"d-2": {}
}
27 changes: 23 additions & 4 deletions internal/core/export/testdata/def.txtar
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
-- in.cue --
a: int | *2
a: int | *2
b?: 4 | 5
c: [string]: int
-- out/definition --
a: int | *2
a: int | *2
b?: 4 | 5
c: {
[string]: int
}
-- out/doc --
[]
[a]
[c]
-- out/value --
== Simplified
{
a: *2 | int
c: {}
}
== Raw
{
a: *2 | int
a: *2 | int
b?: 4 | 5
c: {}
}
== Final
{
a: 2
c: {}
}
== All
{
a: *2 | int
a: *2 | int
b?: 4 | 5
c: {}
}
== Eval
{
a: 2
b?: 4 | 5
c: {}
}
23 changes: 23 additions & 0 deletions internal/core/export/testdata/docs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,26 @@ baz: {
field2: int
}
}
== Eval
{
Foo: {
field1: int
field2: int
dup3: int
}
foos: {
MyFoo: {
field1: 0
field2: 1
dup3: int
}
}
bar: {
field1: int
field2: int
}
baz: {
field1: int
field2: int
}
}
16 changes: 16 additions & 0 deletions internal/core/export/testdata/embedscalar.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,19 @@ sub: {
}
}
}
== Eval
{
#top: 4
sub: {
#sub: 5
a: {
3
#foo: 4
}
b: {
#bar: 5
#baz: "foo"
[1, 2]
}
}
}
4 changes: 4 additions & 0 deletions internal/core/export/testdata/hidden.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ a: {
_#val: 6
}
}
== Eval
{
a: {}
}
53 changes: 53 additions & 0 deletions internal/core/export/testdata/issue662.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

-- uplot.cue --
#LineConfig: {
lineColor?: string
}

#GraphFieldConfig: #LineConfig & {
drawStyle?: int
}
-- out/definition --
#LineConfig: {
lineColor?: string
}
#GraphFieldConfig: {
#LineConfig
drawStyle?: int
}
-- out/doc --
[]
[#LineConfig]
[#GraphFieldConfig]
-- out/value --
== Simplified
{}
== Raw
{
#LineConfig: {
lineColor?: string
}
#GraphFieldConfig: {
lineColor?: string
}
}
== Final
{}
== All
{
#LineConfig: {
lineColor?: string
}
#GraphFieldConfig: {
lineColor?: string
}
}
== Eval
{
#LineConfig: {
lineColor?: string
}
#GraphFieldConfig: {
lineColor?: string
}
}
25 changes: 25 additions & 0 deletions internal/core/export/testdata/let.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,28 @@ y: Y & Y_1
}
y: "foo"
}
== Eval
{
#Foo: 2
x: "foo"
cfgs: [{
metadata: {
name: "one"
}
}, {
metadata: {
name: "two"
}
}]
let filepath = "kind-\(cfg.name)"
files: {
"\(filepath)": {
patches: cfg
}
} & {
"\(filepath)": {
patches: cfg
}
}
y: "foo"
}
7 changes: 7 additions & 0 deletions internal/core/export/testdata/simplify.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ s: strings.MinRunes(4) & strings.MaxRunes(7)
}
s: strings.MinRunes(4) & strings.MaxRunes(7)
}
== Eval
{
x: {
y: >=-9223372036854775808 & <=9223372036854775807 & int
}
s: strings.MinRunes(4) & strings.MaxRunes(7)
}
26 changes: 26 additions & 0 deletions internal/core/export/testdata/strings.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,29 @@ bin2: '''
line!
'''
}
== Eval
{
a: """
multi
line
"""
b: """
message: multi
line!
"""
c: {
d: """
multi
line
"""
}
bin1: '''
multi
line
'''
bin2: '''
multi
message: multi
line!
'''
}
9 changes: 9 additions & 0 deletions internal/core/export/testdata/topo.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ g: 4
f: 4
g: 4
}
== Eval
{
a: 1
b: 1
c: 2
e: 3
f: 4
g: 4
}
20 changes: 10 additions & 10 deletions internal/core/export/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ func (e *exporter) vertex(n *adt.Vertex) (result ast.Expr) {

case !x.IsIncomplete() || len(n.Conjuncts) == 0:
result = e.bottom(x)

default:
// fall back to expression mode
a := []ast.Expr{}
for _, c := range n.Conjuncts {
a = append(a, e.expr(c.Expr()))
}
result = ast.NewBinExpr(token.AND, a...)
}

case adt.Value:
Expand All @@ -84,7 +76,15 @@ func (e *exporter) vertex(n *adt.Vertex) (result ast.Expr) {
}

default:
panic("unknow value")
panic("unknown value")
}
if result == nil {
// fall back to expression mode
a := []ast.Expr{}
for _, c := range n.Conjuncts {
a = append(a, e.expr(c.Expr()))
}
result = ast.NewBinExpr(token.AND, a...)
}
return result
}
Expand Down Expand Up @@ -405,7 +405,7 @@ func (e *exporter) structComposite(v *adt.Vertex) ast.Expr {

arc = &adt.Vertex{Label: label}
v.MatchAndInsert(e.ctx, arc)
if len(v.Conjuncts) == 0 {
if len(arc.Conjuncts) == 0 {
continue
}

Expand Down
15 changes: 15 additions & 0 deletions internal/core/export/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func TestValue(t *testing.T) {
all := export.All
all.ShowErrors = true

evalWithOptions := export.Profile{
TakeDefaults: true,
ShowOptional: true,
ShowDefinitions: true,
ShowAttributes: true,
}

for _, tc := range []struct {
name string
fn func(r adt.Runtime, id string, v adt.Value) (ast.Expr, errors.Error)
Expand All @@ -68,6 +75,7 @@ func TestValue(t *testing.T) {
{"Raw", export.Raw.Value},
{"Final", export.Final.Value},
{"All", all.Value},
{"Eval", evalWithOptions.Value},
} {
fmt.Fprintln(t, "==", tc.name)
x, errs := tc.fn(r, pkgID, v)
Expand Down Expand Up @@ -105,6 +113,13 @@ strings.MinRunes(4) & strings.MaxRunes(7)
p := export.All
p.ShowErrors = true

p = &export.Profile{
TakeDefaults: true,
ShowOptional: true,
ShowDefinitions: true,
ShowAttributes: true,
}

x, errs := p.Value(r, "main", v)
if errs != nil {
t.Fatal(errs)
Expand Down

0 comments on commit 76a72c4

Please sign in to comment.