Skip to content

Commit

Permalink
cue/ast: allow use of Embed in NewStruct
Browse files Browse the repository at this point in the history
Eventually &ast.EmbedDecl may go. Using Embed
allows for a smooth transition, rather than supporting
EmbedDecl directly. It is also more convenient.

Change-Id: Iec869ff00bf7e11acbce3ed07a41a6fee2934423
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6562
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jul 17, 2020
1 parent c615c91 commit f0f62b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cue/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ func NewStruct(fields ...interface{}) *StructLit {
case *LetClause:
s.Elts = append(s.Elts, x)
continue
case *embedding:
s.Elts = append(s.Elts, (*EmbedDecl)(x))
continue
case Label:
label = x
case string:
Expand Down Expand Up @@ -561,6 +564,13 @@ func NewStruct(fields ...interface{}) *StructLit {
return s
}

// Embed can be used in conjunction with NewStruct to embed values.
func Embed(x Expr) *embedding {
return (*embedding)(&EmbedDecl{Expr: x})
}

type embedding EmbedDecl

// A ListLit node represents a literal list.
type ListLit struct {
Lbrack token.Pos // position of "["
Expand Down
2 changes: 1 addition & 1 deletion cue/ast/astutil/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (z *sanitizer) unshadow(parent ast.Node, base string, link ast.Node) string
case *ast.StructLit:
decls = &x.Elts
default:
panic("impossible scope")
panic(fmt.Sprintf("impossible scope type %T", parent))
}

i := 0
Expand Down

0 comments on commit f0f62b8

Please sign in to comment.