Skip to content

Commit

Permalink
internal/core/adt: introduce HasConjunct
Browse files Browse the repository at this point in the history
and use it where appropriate.

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I919c129b3b56b4a312bd01e2a7f9741985f30607
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202658
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
  • Loading branch information
mpvl committed Oct 18, 2024
1 parent e9c005d commit 2bce38d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cue/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ outer:
Label: sel.sel.feature(ctx),
}
n.MatchAndInsert(ctx, x)
if len(x.Conjuncts) > 0 {
if x.HasConjuncts() {
x.Finalize(ctx)
parent = linkParent(parent, n, x)
n = x
Expand Down
7 changes: 6 additions & 1 deletion internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,11 @@ func visitAllConjuncts(a []Conjunct, f func(c Conjunct, isLeaf bool)) {
}
}

// HasConjuncts reports whether v has any conjuncts.
func (v *Vertex) HasConjuncts() bool {
return len(v.Conjuncts) > 0
}

// SingleConjunct reports whether there is a single leaf conjunct and returns 1
// if so. It will return 0 if there are no conjuncts or 2 if there are more than
// 1.
Expand Down Expand Up @@ -736,7 +741,7 @@ func (x *Vertex) IsConcrete() bool {
// it tells whether optional field matching and non-regular fields, like
// definitions and hidden fields, should be ignored.
func (v *Vertex) IsData() bool {
return v.isData || len(v.Conjuncts) == 0
return v.isData || !v.HasConjuncts()
}

// ToDataSingle creates a new Vertex that represents just the regular fields
Expand Down
6 changes: 3 additions & 3 deletions internal/core/export/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (e *exporter) expr(env *adt.Environment, v adt.Elem) (result ast.Expr) {
return nil

case *adt.Vertex:
if len(x.Conjuncts) == 0 || x.IsData() {
if x.IsData() {
// Treat as literal value.
return e.value(x)
} // Should this be the arcs label?
Expand Down Expand Up @@ -170,9 +170,9 @@ func (x *exporter) mergeValues(label adt.Feature, src *adt.Vertex, a []conjunct,
}

// Unify values only for one level.
if a := e.values.Conjuncts; len(a) > 0 {
if e.values.HasConjuncts() {
e.values.Finalize(e.ctx)
e.embed = append(e.embed, e.value(e.values, a...))
e.embed = append(e.embed, e.value(e.values, e.values.Conjuncts...))
}

// Collect and order set of fields.
Expand Down
2 changes: 1 addition & 1 deletion internal/core/export/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (e *exporter) vertex(n *adt.Vertex) (result ast.Expr) {
result = e.structComposite(n, attrs)
}

case !x.IsIncomplete() || len(n.Conjuncts) == 0 || e.cfg.Final:
case !x.IsIncomplete() || !n.HasConjuncts() || e.cfg.Final:
result = e.bottom(x)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/core/subsume/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ outer:

a := &adt.Vertex{Label: f}
x.MatchAndInsert(ctx, a)
if len(a.Conjuncts) == 0 {
if !a.HasConjuncts() {
// It is accepted and has no further constraints, so all good.
continue
}
Expand Down Expand Up @@ -413,7 +413,7 @@ outer:

a := &adt.Vertex{Label: f}
x.MatchAndInsert(ctx, a)
if len(a.Conjuncts) == 0 {
if !a.HasConjuncts() {
// It is accepted and has no further constraints, so all good.
continue
}
Expand Down

0 comments on commit 2bce38d

Please sign in to comment.