Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2019 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package value |
| 18 | |
| 19 | import ( |
| 20 | "reflect" |
| 21 | ) |
| 22 | |
| 23 | type listReflect struct { |
| 24 | Value reflect.Value |
| 25 | } |
| 26 | |
| 27 | func (r listReflect) Length() int { |
| 28 | val := r.Value |
| 29 | return val.Len() |
| 30 | } |
| 31 | |
| 32 | func (r listReflect) At(i int) Value { |
| 33 | val := r.Value |
| 34 | return mustWrapValueReflect(val.Index(i), nil, nil) |
| 35 | } |
| 36 | |
| 37 | func (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 | |
| 42 | func (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 | |
| 51 | func (r listReflect) Range() ListRange { |
| 52 | return r.RangeUsing(HeapAllocator) |
| 53 | } |
| 54 | |
| 55 | func (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 | |
| 67 | func (r listReflect) Equals(other List) bool { |
| 68 | return r.EqualsUsing(HeapAllocator, other) |
| 69 | } |
| 70 | func (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 | |
| 77 | type listReflectRange struct { |
| 78 | list reflect.Value |
| 79 | vr *valueReflect |
| 80 | i int |
| 81 | entry *TypeReflectCacheEntry |
| 82 | } |
| 83 | |
| 84 | func (r *listReflectRange) Next() bool { |
| 85 | r.i += 1 |
| 86 | return r.i < r.list.Len() |
| 87 | } |
| 88 | |
| 89 | func (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 | } |