Skip to content

Commit

Permalink
cue: uniform handling of definitions
Browse files Browse the repository at this point in the history
This mostly affects spacing, which will be tested in followup CLs.

Change-Id: I5308aca574308131d737173d6b9d344317a34356
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5941
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
  • Loading branch information
mpvl committed May 12, 2020
1 parent 37b1de2 commit 8837685
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cue/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ func (p *exporter) structure(x *structLit, addTempl bool) (ret *ast.StructLit, e
if p.mode.omitDefinitions || p.mode.concrete {
continue
}
if !isDef(f) {
if !internal.IsDefinition(f.Label) {
f.Token = token.ISA
}
}
Expand Down Expand Up @@ -914,7 +914,7 @@ func (p *exporter) structure(x *structLit, addTempl bool) (ret *ast.StructLit, e
opt = token.NoSpace.Pos() // anything but token.NoPos
}
tok := token.COLON
if c.def {
if c.def && !internal.IsDefinition(label) {
tok = token.ISA
}
f := &ast.Field{
Expand Down
2 changes: 1 addition & 1 deletion cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (f *formatter) walkDeclList(list []ast.Decl) {
if hasDocComments(x) {
switch x := list[i-1].(type) {
case *ast.Field:
if x.Token == token.ISA {
if x.Token == token.ISA || internal.IsDefinition(x.Label) {
f.print(newsection)
}

Expand Down
5 changes: 3 additions & 2 deletions internal/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,9 @@ func (v *validator) validate(n ast.Node) bool {
check(n, i.Imports, "imports", true)

case *ast.Field:
check(n, i.Definitions, "definitions", x.Token == token.ISA)
check(n, i.Data, "regular fields", x.Token != token.ISA)
check(n, i.Definitions, "definitions",
x.Token == token.ISA || internal.IsDefinition(x.Label))
check(n, i.Data, "regular fields", internal.IsRegularField(x))
check(n, constraints, "optional fields", x.Optional != token.NoPos)

_, _, err := ast.LabelName(x.Label)
Expand Down
32 changes: 32 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,38 @@ func IsBulkField(d ast.Decl) bool {
return false
}

func IsDefinition(label ast.Label) bool {
switch x := label.(type) {
case *ast.Alias:
if ident, ok := x.Expr.(*ast.Ident); ok {
return strings.HasPrefix(ident.Name, "#")
}
case *ast.Ident:
return strings.HasPrefix(x.Name, "#")
}
return false
}

func IsRegularField(f *ast.Field) bool {
if f.Token == token.ISA {
return false
}
var ident *ast.Ident
switch x := f.Label.(type) {
case *ast.Alias:
ident, _ = x.Expr.(*ast.Ident)
case *ast.Ident:
ident = x
}
if ident == nil {
return true
}
if strings.HasPrefix(ident.Name, "#") || strings.HasPrefix(ident.Name, "_") {
return false
}
return true
}

func EmbedStruct(s *ast.StructLit) *ast.EmbedDecl {
e := &ast.EmbedDecl{Expr: s}
if len(s.Elts) == 1 {
Expand Down

0 comments on commit 8837685

Please sign in to comment.