Skip to content

Commit 8ec0479

Browse files
committed
expose *Into() functions directly
1 parent b3065ae commit 8ec0479

16 files changed

+190
-175
lines changed

fieldpath/fromvalue.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ func SetFromValue(v value.Value) *Set {
2525
s := NewSet()
2626

2727
w := objectWalker{
28-
path: Path{},
29-
value: v,
30-
valueAlloc: value.NewValueFreelist(),
31-
do: func(p Path) { s.Insert(p) },
28+
path: Path{},
29+
value: v,
30+
allocator: value.NewValueFreelist(),
31+
do: func(p Path) { s.Insert(p) },
3232
}
3333

3434
w.walk()
3535
return s
3636
}
3737

3838
type objectWalker struct {
39-
path Path
40-
value value.Value
41-
valueAlloc *value.ValueFreelist
39+
path Path
40+
value value.Value
41+
allocator *value.ValueFreelist
4242

4343
do func(Path)
4444
}
@@ -57,10 +57,10 @@ func (w *objectWalker) walk() {
5757
case w.value.IsList():
5858
// If the list were atomic, we'd break here, but we don't have
5959
// a schema, so we can't tell.
60-
l := w.valueAlloc.AsList(w.value)
61-
defer w.valueAlloc.Free(l)
62-
iter := l.Range()
63-
defer iter.Recycle()
60+
l := w.value.AsListInto(w.allocator)
61+
defer w.allocator.Free(l)
62+
iter := l.RangeInto(w.allocator)
63+
defer w.allocator.Free(iter)
6464
for iter.Next() {
6565
i, value := iter.Item()
6666
w2 := *w
@@ -73,9 +73,9 @@ func (w *objectWalker) walk() {
7373
// If the map/struct were atomic, we'd break here, but we don't
7474
// have a schema, so we can't tell.
7575

76-
m := w.valueAlloc.AsMap(w.value)
77-
defer w.valueAlloc.Free(m)
78-
m.Iterate(func(k string, val value.Value) bool {
76+
m := w.value.AsMapInto(w.allocator)
77+
defer w.allocator.Free(m)
78+
m.IterateInto(w.allocator, func(k string, val value.Value) bool {
7979
w2 := *w
8080
w2.path = append(w.path, PathElement{FieldName: &k})
8181
w2.value = val
@@ -112,8 +112,8 @@ func (w *objectWalker) GuessBestListPathElement(index int, item value.Value) Pat
112112
return PathElement{Index: &index}
113113
}
114114

115-
m := w.valueAlloc.AsMap(item)
116-
defer w.valueAlloc.Free(m)
115+
m := item.AsMapInto(w.allocator)
116+
defer w.allocator.Free(m)
117117
var keys value.FieldList
118118
for _, name := range AssociativeListCandidateFieldNames {
119119
f, ok := m.Get(name)

typed/helpers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,19 @@ func handleAtom(a schema.Atom, tr schema.TypeRef, ah atomHandler) ValidationErro
154154
}
155155

156156
// Returns the list, or an error. Reminder: nil is a valid list and might be returned.
157-
func listValue(a *value.ValueFreelist, val value.Value) (value.List, error) {
157+
func listValue(a value.Allocator, val value.Value) (value.List, error) {
158158
if val.IsNull() {
159159
// Null is a valid list.
160160
return nil, nil
161161
}
162162
if !val.IsList() {
163163
return nil, fmt.Errorf("expected list, got %v", val)
164164
}
165-
return a.AsList(val), nil
165+
return val.AsListInto(a), nil
166166
}
167167

168168
// Returns the map, or an error. Reminder: nil is a valid map and might be returned.
169-
func mapValue(a *value.ValueFreelist, val value.Value) (value.Map, error) {
169+
func mapValue(a value.Allocator, val value.Value) (value.Map, error) {
170170
if val == nil {
171171
return nil, fmt.Errorf("expected map, got nil")
172172
}
@@ -177,10 +177,10 @@ func mapValue(a *value.ValueFreelist, val value.Value) (value.Map, error) {
177177
if !val.IsMap() {
178178
return nil, fmt.Errorf("expected map, got %v", val)
179179
}
180-
return a.AsMap(val), nil
180+
return val.AsMapInto(a), nil
181181
}
182182

183-
func keyedAssociativeListItemToPathElement(a *value.ValueFreelist, list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
183+
func keyedAssociativeListItemToPathElement(a value.Allocator, list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
184184
pe := fieldpath.PathElement{}
185185
if child.IsNull() {
186186
// For now, the keys are required which means that null entries
@@ -191,7 +191,7 @@ func keyedAssociativeListItemToPathElement(a *value.ValueFreelist, list *schema.
191191
return pe, errors.New("associative list with keys may not have non-map elements")
192192
}
193193
keyMap := value.FieldList{}
194-
m := a.AsMap(child)
194+
m := child.AsMapInto(a)
195195
defer a.Free(m)
196196
for _, fieldName := range list.Keys {
197197
if val, ok := m.Get(fieldName); ok {

typed/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func (w *mergingWalker) visitMapItem(t *schema.Map, out map[string]interface{},
312312
func (w *mergingWalker) visitMapItems(t *schema.Map, lhs, rhs value.Map) (errs ValidationErrors) {
313313
out := map[string]interface{}{}
314314

315-
w.allocator.MapZip(lhs, rhs, value.Unordered, func(key string, lhsValue, rhsValue value.Value) bool {
315+
value.MapZipInto(w.allocator, lhs, rhs, value.Unordered, func(key string, lhsValue, rhsValue value.Value) bool {
316316
errs = append(errs, w.visitMapItem(t, out, key, lhsValue, rhsValue)...)
317317
return true
318318
})

typed/remove.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ func (w *removingWalker) doScalar(t *schema.Scalar) ValidationErrors {
4444
}
4545

4646
func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
47-
l := w.allocator.AsList(w.value)
47+
l := w.value.AsListInto(w.allocator)
4848
defer w.allocator.Free(l)
4949
// If list is null, empty, or atomic just return
5050
if l == nil || l.Length() == 0 || t.ElementRelationship == schema.Atomic {
5151
return nil
5252
}
5353

5454
var newItems []interface{}
55-
iter := l.Range()
56-
defer iter.Recycle()
55+
iter := l.RangeInto(w.allocator)
56+
defer w.allocator.Free(iter)
5757
for iter.Next() {
5858
i, item := iter.Item()
5959
// Ignore error because we have already validated this list
@@ -74,7 +74,7 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
7474
}
7575

7676
func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
77-
m := w.allocator.AsMap(w.value)
77+
m := w.value.AsMapInto(w.allocator)
7878
if m != nil {
7979
defer w.allocator.Free(m)
8080
}

typed/validate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (v *validatingObjectWalker) doScalar(t *schema.Scalar) ValidationErrors {
116116
func (v *validatingObjectWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
117117
observedKeys := fieldpath.MakePathElementSet(list.Length())
118118
for i := 0; i < list.Length(); i++ {
119-
child := v.allocator.At(list, i)
119+
child := list.AtInto(v.allocator, i)
120120
defer v.allocator.Free(child)
121121
var pe fieldpath.PathElement
122122
if t.ElementRelationship != schema.Associative {
@@ -161,7 +161,7 @@ func (v *validatingObjectWalker) doList(t *schema.List) (errs ValidationErrors)
161161
}
162162

163163
func (v *validatingObjectWalker) visitMapItems(t *schema.Map, m value.Map) (errs ValidationErrors) {
164-
v.allocator.Iterate(m, func(key string, val value.Value) bool {
164+
m.IterateInto(v.allocator, func(key string, val value.Value) bool {
165165
pe := fieldpath.PathElement{FieldName: &key}
166166
tr := t.ElementType
167167
if sf, ok := t.FindField(key); ok {

value/list.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ type List interface {
2323
// At returns the item at the given position in the map. It will
2424
// panic if the index is out of range.
2525
At(int) Value
26-
atInto(Allocator, int) Value
26+
AtInto(Allocator, int) Value
2727
// Range returns a ListRange for iterating over the items in the list.
2828
Range() ListRange
29+
RangeInto(Allocator) ListRange
2930

3031
// Equals compares the two list, and return true if they are the same, false otherwise.
3132
// Implementations can use ListEquals as a general implementation for this methods.
3233
Equals(List) bool
33-
equalsInto(Allocator, List) bool
34+
EqualsInto(Allocator, List) bool
3435
}
3536

3637
// ListRange represents a single iteration across the items of a list.
@@ -42,9 +43,6 @@ type ListRange interface {
4243
// pointers to the value returned by Item() that escape the iteration loop since they become invalid once either
4344
// Item() or Recycle() is called.
4445
Item() (index int, value Value)
45-
// Recycle gives back this ListRange once it is no longer needed. The value returned by Item() becomes invalid once this is
46-
// called.
47-
Recycle()
4846
}
4947

5048
var EmptyRange = &emptyRange{}
@@ -72,10 +70,10 @@ func listEqualsInto(a Allocator, lhs, rhs List) bool {
7270
return false
7371
}
7472

75-
lhsRange := lhs.Range()
76-
defer lhsRange.Recycle()
77-
rhsRange := rhs.Range()
78-
defer rhsRange.Recycle()
73+
lhsRange := lhs.RangeInto(a)
74+
defer a.Free(lhsRange)
75+
rhsRange := rhs.RangeInto(a)
76+
defer a.Free(rhsRange)
7977

8078
for lhsRange.Next() && rhsRange.Next() {
8179
_, lv := lhsRange.Item()
@@ -98,10 +96,10 @@ func ListCompare(lhs, rhs List) int {
9896
return listCompareInto(defaultAllocator, lhs, rhs)
9997
}
10098
func listCompareInto(a Allocator, lhs, rhs List) int {
101-
lhsRange := lhs.Range()
102-
defer lhsRange.Recycle()
103-
rhsRange := rhs.Range()
104-
defer rhsRange.Recycle()
99+
lhsRange := lhs.RangeInto(a)
100+
defer a.Free(lhsRange)
101+
rhsRange := rhs.RangeInto(a)
102+
defer a.Free(rhsRange)
105103

106104
for {
107105
lhsOk := lhsRange.Next()

value/listreflect.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package value
1818

1919
import (
2020
"reflect"
21-
"sync"
2221
)
2322

2423
type listReflect struct {
@@ -35,7 +34,7 @@ func (r listReflect) At(i int) Value {
3534
return mustWrapValueReflect(val.Index(i), nil, nil)
3635
}
3736

38-
func (r listReflect) atInto(a Allocator, i int) Value {
37+
func (r listReflect) AtInto(a Allocator, i int) Value {
3938
val := r.Value
4039
return a.allocValueReflect().mustReuse(val.Index(i), nil, nil, nil)
4140
}
@@ -49,28 +48,26 @@ func (r listReflect) Unstructured() interface{} {
4948
return result
5049
}
5150

52-
var lrrPool = sync.Pool{
53-
New: func() interface{} {
54-
return &listReflectRange{vr: &valueReflect{}}
55-
},
51+
func (r listReflect) Range() ListRange {
52+
return r.RangeInto(defaultAllocator)
5653
}
5754

58-
func (r listReflect) Range() ListRange {
55+
func (r listReflect) RangeInto(a Allocator) ListRange {
5956
length := r.Value.Len()
6057
if length == 0 {
6158
return EmptyRange
6259
}
63-
rr := lrrPool.Get().(*listReflectRange)
60+
rr := a.allocListReflectRange()
6461
rr.list = r.Value
6562
rr.i = -1
6663
rr.entry = TypeReflectEntryOf(r.Value.Type().Elem())
6764
return rr
6865
}
6966

7067
func (r listReflect) Equals(other List) bool {
71-
return r.equalsInto(defaultAllocator, other)
68+
return r.EqualsInto(defaultAllocator, other)
7269
}
73-
func (r listReflect) equalsInto(a Allocator, other List) bool {
70+
func (r listReflect) EqualsInto(a Allocator, other List) bool {
7471
if otherReflectList, ok := other.(*listReflect); ok {
7572
return reflect.DeepEqual(r.Value.Interface(), otherReflectList.Value.Interface())
7673
}
@@ -99,7 +96,3 @@ func (r *listReflectRange) Item() (index int, value Value) {
9996
v := r.list.Index(r.i)
10097
return r.i, r.vr.mustReuse(v, r.entry, nil, nil)
10198
}
102-
103-
func (r *listReflectRange) Recycle() {
104-
lrrPool.Put(r)
105-
}

value/listunstructured.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ limitations under the License.
1616

1717
package value
1818

19-
import "sync"
20-
2119
type listUnstructured []interface{}
2220

2321
func (l listUnstructured) Length() int {
@@ -28,33 +26,27 @@ func (l listUnstructured) At(i int) Value {
2826
return NewValueInterface(l[i])
2927
}
3028

31-
func (l listUnstructured) atInto(a Allocator, i int) Value {
29+
func (l listUnstructured) AtInto(a Allocator, i int) Value {
3230
return a.allocValueUnstructured().reuse(l[i])
3331
}
3432

3533
func (l listUnstructured) Equals(other List) bool {
36-
return l.equalsInto(defaultAllocator, other)
34+
return l.EqualsInto(defaultAllocator, other)
3735
}
3836

39-
func (l listUnstructured) equalsInto(a Allocator, other List) bool {
37+
func (l listUnstructured) EqualsInto(a Allocator, other List) bool {
4038
return listEqualsInto(a, &l, other)
4139
}
4240

43-
var lurPool = sync.Pool{
44-
New: func() interface{} {
45-
return &listUnstructuredRange{vv: &valueUnstructured{}}
46-
},
47-
}
48-
49-
func (_ listUnstructured) Recycle() {
50-
41+
func (l listUnstructured) Range() ListRange {
42+
return l.RangeInto(defaultAllocator)
5143
}
5244

53-
func (l listUnstructured) Range() ListRange {
45+
func (l listUnstructured) RangeInto(a Allocator) ListRange {
5446
if len(l) == 0 {
5547
return EmptyRange
5648
}
57-
r := lurPool.Get().(*listUnstructuredRange)
49+
r := a.allocListUnstructuredRange()
5850
r.list = l
5951
r.i = -1
6052
return r
@@ -80,7 +72,3 @@ func (r *listUnstructuredRange) Item() (index int, value Value) {
8072
}
8173
return r.i, r.vv.reuse(r.list[r.i])
8274
}
83-
84-
func (r *listUnstructuredRange) Recycle() {
85-
lurPool.Put(r)
86-
}

0 commit comments

Comments
 (0)