blob: 197d4c921d4aa42ce7560657cd3269d18e12d737 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2019 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package value
18
19import (
20 "reflect"
21)
22
23type listReflect struct {
24 Value reflect.Value
25}
26
27func (r listReflect) Length() int {
28 val := r.Value
29 return val.Len()
30}
31
32func (r listReflect) At(i int) Value {
33 val := r.Value
34 return mustWrapValueReflect(val.Index(i), nil, nil)
35}
36
37func (r listReflect) AtUsing(a Allocator, i int) Value {
38 val := r.Value
39 return a.allocValueReflect().mustReuse(val.Index(i), nil, nil, nil)
40}
41
42func (r listReflect) Unstructured() interface{} {
43 l := r.Length()
44 result := make([]interface{}, l)
45 for i := 0; i < l; i++ {
46 result[i] = r.At(i).Unstructured()
47 }
48 return result
49}
50
51func (r listReflect) Range() ListRange {
52 return r.RangeUsing(HeapAllocator)
53}
54
55func (r listReflect) RangeUsing(a Allocator) ListRange {
56 length := r.Value.Len()
57 if length == 0 {
58 return EmptyRange
59 }
60 rr := a.allocListReflectRange()
61 rr.list = r.Value
62 rr.i = -1
63 rr.entry = TypeReflectEntryOf(r.Value.Type().Elem())
64 return rr
65}
66
67func (r listReflect) Equals(other List) bool {
68 return r.EqualsUsing(HeapAllocator, other)
69}
70func (r listReflect) EqualsUsing(a Allocator, other List) bool {
71 if otherReflectList, ok := other.(*listReflect); ok {
72 return reflect.DeepEqual(r.Value.Interface(), otherReflectList.Value.Interface())
73 }
74 return ListEqualsUsing(a, &r, other)
75}
76
77type listReflectRange struct {
78 list reflect.Value
79 vr *valueReflect
80 i int
81 entry *TypeReflectCacheEntry
82}
83
84func (r *listReflectRange) Next() bool {
85 r.i += 1
86 return r.i < r.list.Len()
87}
88
89func (r *listReflectRange) Item() (index int, value Value) {
90 if r.i < 0 {
91 panic("Item() called before first calling Next()")
92 }
93 if r.i >= r.list.Len() {
94 panic("Item() called on ListRange with no more items")
95 }
96 v := r.list.Index(r.i)
97 return r.i, r.vr.mustReuse(v, r.entry, nil, nil)
98}