cue: generate new-style maps when exporting
Change-Id: Ifbc918811bc3939fbddcf025d81063a3afc8f7de
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3864
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/export.go b/cue/export.go
index 83a1922..ae5af74 100644
--- a/cue/export.go
+++ b/cue/export.go
@@ -200,10 +200,32 @@
return short
}
+func mkTemplate(n *ast.Ident) ast.Label {
+ var expr ast.Expr = n
+ switch n.Name {
+ case "":
+ expr = ast.NewIdent("_")
+ case "_":
+ default:
+ expr = &ast.Alias{Ident: n, Expr: ast.NewIdent("_")}
+ }
+ return ast.NewList(expr)
+}
+
func hasTemplate(s *ast.StructLit) bool {
for _, e := range s.Elts {
if f, ok := e.(*ast.Field); ok {
- if _, ok := f.Label.(*ast.TemplateLabel); ok {
+ label := f.Label
+ if _, ok := label.(*ast.TemplateLabel); ok {
+ return true
+ }
+ if a, ok := label.(*ast.Alias); ok {
+ label, ok = a.Expr.(ast.Label)
+ if !ok {
+ return false
+ }
+ }
+ if _, ok := label.(*ast.ListLit); ok {
return true
}
}
@@ -469,9 +491,7 @@
}
expr = &ast.BinaryExpr{X: expr, Op: token.AND, Y: &ast.StructLit{
Elts: []ast.Decl{&ast.Field{
- Label: &ast.TemplateLabel{
- Ident: p.identifier(l.params.arcs[0].feature),
- },
+ Label: mkTemplate(p.identifier(l.params.arcs[0].feature)),
Value: p.expr(l.value),
}},
}}
@@ -676,9 +696,7 @@
break
}
obj.Elts = append(obj.Elts, &ast.Field{
- Label: &ast.TemplateLabel{
- Ident: p.identifier(l.params.arcs[0].feature),
- },
+ Label: mkTemplate(p.identifier(l.params.arcs[0].feature)),
Value: p.expr(l.value),
})
} // TODO: else record error
diff --git a/cue/export_test.go b/cue/export_test.go
index a42f84e..302b1a9 100644
--- a/cue/export_test.go
+++ b/cue/export_test.go
@@ -348,7 +348,7 @@
f
b: int
- <_>: <100
+ [_]: <100
}
}`,
out: unindent(`
@@ -365,7 +365,7 @@
emb
}
e :: {
- <_>: <100
+ [_]: <100
b: int
f
}
@@ -417,7 +417,7 @@
eval: true,
in: `{
b: [{
- <X>: int
+ [X=_]: int
if a > 4 {
f: 4
}
@@ -429,7 +429,7 @@
out: unindent(`
{
b: [{
- <X>: int
+ [X=_]: int
if a > 4 {
f: 4
}
@@ -677,7 +677,7 @@
}`),
}, {
in: `A: {
- <_>: B
+ [_]: B
} @protobuf(1,"test")
B: {}
@@ -686,7 +686,7 @@
out: unindent(`
{
A: {
- <_>: B
+ [_]: B
} @protobuf(1,"test")
B: {
} & ({
@@ -715,7 +715,7 @@
eval: true,
in: `
A :: { b: int }
- a: A & { <_>: <10 }
+ a: A & { [_]: <10 }
B :: a
`,
out: unindent(`
@@ -770,7 +770,7 @@
eval: true,
in: `
T :: {
- <_>: int64
+ [_]: int64
}
X :: {
x: int
@@ -780,7 +780,7 @@
out: unindent(`
{
T :: {
- <_>: int64
+ [_]: int64
}
X :: {
x: int64
@@ -794,7 +794,7 @@
opts: []Option{Optional(false)},
in: `
T :: {
- <_>: int64
+ [_]: int64
}
X :: {
x: int