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

Commit

Permalink
cue: add IsClosed method
Browse files Browse the repository at this point in the history
Change-Id: I2303819226b0fe2ee692e2e1abccf938a5de955a
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5400
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Mar 31, 2020
1 parent aa5e0c7 commit 53ca13f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,24 @@ func (v Value) Pos() token.Pos {

// TODO: IsFinal: this value can never be changed.

// IsClosed reports whether a list of struct is closed. It reports false when
// when the value is not a list or struct.
func (v Value) IsClosed() bool {
switch v.Kind() {
case StructKind:
if st, ok := v.path.val().(*structLit); ok {
return st.closeStatus.shouldClose()
}
case ListKind:
if l, ok := v.path.val().(*list); ok {
if n, ok := l.len.(*numLit); ok {
return n.intValue(v.ctx()) == len(l.elem.arcs)
}
}
}
return false
}

// IsConcrete reports whether the current value is a concrete scalar value
// (not relying on default values), a terminal error, a list, or a struct.
// It does not verify that values of lists or structs are concrete themselves.
Expand Down
11 changes: 11 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestValueType(t *testing.T) {
json string
valid bool
concrete bool
closed bool
// pos token.Pos
}{{ // Not a concrete value.
value: `v: _`,
Expand Down Expand Up @@ -151,11 +152,18 @@ func TestValueType(t *testing.T) {
kind: StructKind,
incompleteKind: StructKind,
concrete: true,
}, {
value: `v: close({})`,
kind: StructKind,
incompleteKind: StructKind,
concrete: true,
closed: true,
}, {
value: `v: []`,
kind: ListKind,
incompleteKind: ListKind,
concrete: true,
closed: true,
}, {
value: `v: {a: int, b: [1][a]}.b`,
kind: BottomKind,
Expand Down Expand Up @@ -199,6 +207,9 @@ func TestValueType(t *testing.T) {
if got := v.IsConcrete(); got != tc.concrete {
t.Errorf("IsConcrete: got %v; want %v", got, tc.concrete)
}
if got := v.IsClosed(); got != tc.closed {
t.Errorf("IsClosed: got %v; want %v", got, tc.closed)
}
})
}
}
Expand Down
1 change: 0 additions & 1 deletion cue/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ func (x *numLit) isInt(ctx *context) bool {
func (x *numLit) intValue(ctx *context) int {
v, err := x.v.Int64()
if err != nil {
ctx.mkErr(x, "intValue: %v", err)
return 0
}
return int(v)
Expand Down

0 comments on commit 53ca13f

Please sign in to comment.