Skip to content

Commit

Permalink
internal: hoist a single embedding in ToExpr
Browse files Browse the repository at this point in the history
Fixes #368
Fixes #369

Change-Id: I0161fbd14f59685f559c992a82a999df41522224
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7362
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Oct 5, 2020
1 parent b4aa96d commit 737a103
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
8 changes: 2 additions & 6 deletions cmd/cue/cmd/orphans.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ func (b *buildPlan) placeOrphans(i *build.Instance) (ok bool, err error) {
return true, nil
}

func toExpr(f *ast.File) (expr ast.Expr, pkg *ast.Package) {
p := len(f.Preamble())
return &ast.StructLit{Elts: f.Decls[p:]}, pkg
}

func placeOrphans(cmd *Command, filename, pkg string, objs ...*ast.File) (*ast.File, error) {
f := &ast.File{}

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

var pathElems []ast.Label
var pathTokens []token.Token
Expand Down
25 changes: 21 additions & 4 deletions cmd/cue/cmd/testdata/script/import_list.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
cue import -o - -f -l "\(strings.ToLower(kind))" --list ./import
cmp stdout expect-stdout
-- expect-stdout --
cue import -o - -f -l "\(strings.ToLower(kind))" --list ./import1
cmp stdout expect-stdout1

# Issue #368
cue import -o - -f --list ./import2
cmp stdout expect-stdout2

# Issue #369
cue import --with-context -l '"\(path.Ext(filename))": data' ./import3/data.json
cmpenv import3/data.cue expect3
-- expect-stdout1 --
service: [{
kind: "Service"
name: "booster"
Expand All @@ -17,7 +25,11 @@ deployment: [{
name: "booster"
replicas: 1
}]
-- import/services.jsonl --
-- expect-stdout2 --
[[{a: 1}], [{b: 2}]]
-- expect3 --
".json": [1]
-- import1/services.jsonl --
{
"kind": "Service",
"name": "booster"
Expand All @@ -32,4 +44,9 @@ deployment: [{
"name": "supplement\nfoo",
"json": "[1, 2]"
}
-- import2/data.jsonl --
[{"a": 1}]
[{"b": 2}]
-- import3/data.json --
[1]
-- cue.mod --
8 changes: 7 additions & 1 deletion internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,13 @@ func ToExpr(n ast.Node) ast.Expr {
break outer
}
}
return &ast.StructLit{Elts: x.Decls[start:]}
decls := x.Decls[start:]
if len(decls) == 1 {
if e, ok := decls[0].(*ast.EmbedDecl); ok {
return e.Expr
}
}
return &ast.StructLit{Elts: decls}

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

0 comments on commit 737a103

Please sign in to comment.