cue: return pointer from Struct.Fields.
Returning the value type was a mistake, as all the `Iterator`
methods are pointer methods, and the value return makes it
look like the returned iterator can be copied without issues.
Change-Id: I3e4cb95802024408dfc9b3f0ee1aa7bc462ce368
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5520
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cue/types.go b/cue/types.go
index 8da7660..a77b029 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -1418,20 +1418,20 @@
}
// Fields creates an iterator over the Struct's fields.
-func (s *Struct) Fields(opts ...Option) Iterator {
+func (s *Struct) Fields(opts ...Option) *Iterator {
iter, _ := s.v.Fields(opts...)
return iter
}
// Fields creates an iterator over v's fields if v is a struct or an error
// otherwise.
-func (v Value) Fields(opts ...Option) (Iterator, error) {
+func (v Value) Fields(opts ...Option) (*Iterator, error) {
o := options{omitDefinitions: true, omitHidden: true, omitOptional: true}
o.updateOptions(opts)
ctx := v.ctx()
obj, err := v.structValOpts(ctx, o)
if err != nil {
- return Iterator{ctx: ctx}, v.toErr(err)
+ return &Iterator{ctx: ctx}, v.toErr(err)
}
n := &structLit{
obj.obj.baseValue, // baseValue
@@ -1442,7 +1442,7 @@
obj.arcs, // arcs
nil, // attributes
}
- return Iterator{ctx: ctx, val: v, iter: n, len: len(n.arcs)}, nil
+ return &Iterator{ctx: ctx, val: v, iter: n, len: len(n.arcs)}, nil
}
// Lookup reports the value at a path starting from v. The empty path returns v
diff --git a/cue/types_test.go b/cue/types_test.go
index 0160f4f..284f9c2 100644
--- a/cue/types_test.go
+++ b/cue/types_test.go
@@ -680,6 +680,7 @@
t.Run(tc.value, func(t *testing.T) {
obj := getInstance(t, tc.value).Value()
+ var iter *Iterator // Verify that the returned iterator is a pointer.
iter, err := obj.Fields(All())
checkFatal(t, err, tc.err, "init")