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

Commit

Permalink
cue: don't extract embedded values
Browse files Browse the repository at this point in the history
Only non-value expressions are extracted.

Change-Id: Iecbcc66036cea68acf0679ef1a6d9a647bba4672
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9861
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed May 20, 2021
1 parent 393ec28 commit cd94426
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
20 changes: 14 additions & 6 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,10 @@ func (v Value) Expr() (Op, []Value) {
ctx := v.ctx()
for _, d := range x.Decls {
switch x := d.(type) {
default:
fields = append(fields, d)
case adt.Value:
fields = append(fields, d)
case adt.Expr:
// embedding
n := &adt.Vertex{Label: v.v.Label}
Expand All @@ -2400,9 +2404,6 @@ func (v Value) Expr() (Op, []Value) {
n.Finalize(ctx)
n.Parent = v.v.Parent
a = append(a, makeValue(v.idx, n, v.parent_))

default:
fields = append(fields, d)
}
}
if len(a) == 0 {
Expand All @@ -2414,9 +2415,16 @@ func (v Value) Expr() (Op, []Value) {
n := &adt.Vertex{
Label: v.v.Label,
}
c := adt.MakeRootConjunct(env, &adt.StructLit{
Decls: fields,
})
s := &adt.StructLit{}
if k := v.v.Kind(); k != adt.StructKind && k != BottomKind {
// TODO: we should also add such a declaration for embeddings
// of structs with definitions. However, this is currently
// also not supported at the CUE level. If we do, it may be
// best handled with a special mode of unification.
s.Decls = append(s.Decls, &adt.BasicType{K: k})
}
s.Decls = append(s.Decls, fields...)
c := adt.MakeRootConjunct(env, s)
n.AddConjunct(c)
n.Finalize(ctx)
n.Parent = v.v.Parent
Expand Down
10 changes: 10 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3331,8 +3331,18 @@ func TestExpr(t *testing.T) {
input: `v: "Hello, \(x)! Welcome to \(place)", place: string, x: string`,
want: `\()("Hello, " .(〈〉 "x") "! Welcome to " .(〈〉 "place") "")`,
}, {
// Split out the reference, but ensure the split-off outer struct
// remains valid.
input: `v: { a, #b: 1 }, a: 2`,
want: `&(.(〈〉 "a") {int,#b:1})`,
}, {
// Result is an error, no need to split off.
input: `v: { a, b: 1 }, a: 2`,
want: `&(.(〈〉 "a") {b:1})`,
}, {
// Don't split of concrete values.
input: `v: { "foo", #def: 1 }`,
want: `{"foo",#def:1}`,
}, {
input: `v: { {c: a}, b: a }, a: int`,
want: `&({c:a} {b:a})`,
Expand Down

0 comments on commit cd94426

Please sign in to comment.