Skip to content

Commit

Permalink
cue: remove field type
Browse files Browse the repository at this point in the history
All information is now present and directly
accessible in Vertex.

Issue #2305

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I76f6e0e11c8013bbae904a8d1d068f1ffe3a67a8
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/552168
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mpvl committed Apr 5, 2023
1 parent 143b102 commit d05d9c1
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ func (o *hiddenStructValue) At(i int) (key string, v Value) {
return o.v.idx.LabelStr(f), newChildValue(o, i)
}

func (o *hiddenStructValue) at(i int) (v *adt.Vertex, isOpt bool) {
func (o *hiddenStructValue) at(i int) *adt.Vertex {
f := o.features[i]
arc := o.obj.Lookup(f)
return arc, arc.IsConstraint()
return o.obj.Lookup(f)
}

// Lookup reports the field for the given key. The returned Value is invalid
Expand Down Expand Up @@ -215,7 +214,7 @@ type Iterator struct {
val Value
idx *runtime.Runtime
ctx *adt.OpContext
arcs []field
arcs []*adt.Vertex
p int
cur Value
f adt.Feature
Expand All @@ -224,24 +223,19 @@ type Iterator struct {

type hiddenIterator = Iterator

type field struct {
arc *adt.Vertex
isOptional bool
}

// Next advances the iterator to the next value and reports whether there was
// any. It must be called before the first call to Value or Key.
func (i *Iterator) Next() bool {
if i.p >= len(i.arcs) {
i.cur = Value{}
return false
}
f := i.arcs[i.p]
f.arc.Finalize(i.ctx)
p := linkParent(i.val.parent_, i.val.v, f.arc)
i.cur = makeValue(i.val.idx, f.arc, p)
i.f = f.arc.Label
i.isOpt = f.isOptional
arc := i.arcs[i.p]
arc.Finalize(i.ctx)
p := linkParent(i.val.parent_, i.val.v, arc)
i.cur = makeValue(i.val.idx, arc, p)
i.f = arc.Label
i.isOpt = arc.ArcType == adt.ArcOptional
i.p++
return true
}
Expand Down Expand Up @@ -630,7 +624,7 @@ func newValueRoot(idx *runtime.Runtime, ctx *adt.OpContext, x adt.Expr) Value {
}

func newChildValue(o *structValue, i int) Value {
arc, _ := o.at(i)
arc := o.at(i)
return makeValue(o.v.idx, arc, linkParent(o.v.parent_, o.v.v, arc))
}

Expand Down Expand Up @@ -1284,10 +1278,10 @@ func (v Value) List() (Iterator, error) {
if err := v.checkKind(ctx, adt.ListKind); err != nil {
return Iterator{idx: v.idx, ctx: ctx}, v.toErr(err)
}
arcs := []field{}
arcs := []*adt.Vertex{}
for _, a := range v.v.Elems() {
if a.Label.IsInt() {
arcs = append(arcs, field{arc: a})
arcs = append(arcs, a)
}
}
return Iterator{idx: v.idx, ctx: ctx, val: v, arcs: arcs}, nil
Expand Down Expand Up @@ -1467,7 +1461,8 @@ func (s *hiddenStruct) Len() int {

// field reports information about the ith field, i < o.Len().
func (s *hiddenStruct) Field(i int) FieldInfo {
a, opt := s.at(i)
a := s.at(i)
opt := a.ArcType == adt.ArcOptional
ctx := s.v.ctx()

v := makeChildValue(s.v, a)
Expand Down Expand Up @@ -1506,10 +1501,10 @@ func (v Value) Fields(opts ...Option) (*Iterator, error) {
return &Iterator{idx: v.idx, ctx: ctx}, v.toErr(err)
}

arcs := []field{}
arcs := []*adt.Vertex{}
for i := range obj.features {
arc, isOpt := obj.at(i)
arcs = append(arcs, field{arc: arc, isOptional: isOpt})
arc := obj.at(i)
arcs = append(arcs, arc)
}
return &Iterator{idx: v.idx, ctx: ctx, val: v, arcs: arcs}, nil
}
Expand Down

0 comments on commit d05d9c1

Please sign in to comment.