Skip to content

Commit 737a103

Browse files
committed
internal: hoist a single embedding in ToExpr
Fixes #368 Fixes #369 Change-Id: I0161fbd14f59685f559c992a82a999df41522224 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7362 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent b4aa96d commit 737a103

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

cmd/cue/cmd/orphans.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ func (b *buildPlan) placeOrphans(i *build.Instance) (ok bool, err error) {
150150
return true, nil
151151
}
152152

153-
func toExpr(f *ast.File) (expr ast.Expr, pkg *ast.Package) {
154-
p := len(f.Preamble())
155-
return &ast.StructLit{Elts: f.Decls[p:]}, pkg
156-
}
157-
158153
func placeOrphans(cmd *Command, filename, pkg string, objs ...*ast.File) (*ast.File, error) {
159154
f := &ast.File{}
160155

@@ -163,7 +158,8 @@ func placeOrphans(cmd *Command, filename, pkg string, objs ...*ast.File) (*ast.F
163158
if i == 0 {
164159
astutil.CopyMeta(f, file)
165160
}
166-
expr, p := toExpr(file)
161+
expr := internal.ToExpr(file)
162+
p, _, _ := internal.PackageInfo(file)
167163

168164
var pathElems []ast.Label
169165
var pathTokens []token.Token

cmd/cue/cmd/testdata/script/import_list.txt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
cue import -o - -f -l "\(strings.ToLower(kind))" --list ./import
2-
cmp stdout expect-stdout
3-
-- expect-stdout --
1+
cue import -o - -f -l "\(strings.ToLower(kind))" --list ./import1
2+
cmp stdout expect-stdout1
3+
4+
# Issue #368
5+
cue import -o - -f --list ./import2
6+
cmp stdout expect-stdout2
7+
8+
# Issue #369
9+
cue import --with-context -l '"\(path.Ext(filename))": data' ./import3/data.json
10+
cmpenv import3/data.cue expect3
11+
-- expect-stdout1 --
412
service: [{
513
kind: "Service"
614
name: "booster"
@@ -17,7 +25,11 @@ deployment: [{
1725
name: "booster"
1826
replicas: 1
1927
}]
20-
-- import/services.jsonl --
28+
-- expect-stdout2 --
29+
[[{a: 1}], [{b: 2}]]
30+
-- expect3 --
31+
".json": [1]
32+
-- import1/services.jsonl --
2133
{
2234
"kind": "Service",
2335
"name": "booster"
@@ -32,4 +44,9 @@ deployment: [{
3244
"name": "supplement\nfoo",
3345
"json": "[1, 2]"
3446
}
47+
-- import2/data.jsonl --
48+
[{"a": 1}]
49+
[{"b": 2}]
50+
-- import3/data.json --
51+
[1]
3552
-- cue.mod --

internal/internal.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,13 @@ func ToExpr(n ast.Node) ast.Expr {
259259
break outer
260260
}
261261
}
262-
return &ast.StructLit{Elts: x.Decls[start:]}
262+
decls := x.Decls[start:]
263+
if len(decls) == 1 {
264+
if e, ok := decls[0].(*ast.EmbedDecl); ok {
265+
return e.Expr
266+
}
267+
}
268+
return &ast.StructLit{Elts: decls}
263269

264270
default:
265271
panic(fmt.Sprintf("Unsupported node type %T", x))

0 commit comments

Comments
 (0)