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 | type listUnstructured []interface{} |
| 20 | |
| 21 | func (l listUnstructured) Length() int { |
| 22 | return len(l) |
| 23 | } |
| 24 | |
| 25 | func (l listUnstructured) At(i int) Value { |
| 26 | return NewValueInterface(l[i]) |
| 27 | } |
| 28 | |
| 29 | func (l listUnstructured) AtUsing(a Allocator, i int) Value { |
| 30 | return a.allocValueUnstructured().reuse(l[i]) |
| 31 | } |
| 32 | |
| 33 | func (l listUnstructured) Equals(other List) bool { |
| 34 | return l.EqualsUsing(HeapAllocator, other) |
| 35 | } |
| 36 | |
| 37 | func (l listUnstructured) EqualsUsing(a Allocator, other List) bool { |
| 38 | return ListEqualsUsing(a, &l, other) |
| 39 | } |
| 40 | |
| 41 | func (l listUnstructured) Range() ListRange { |
| 42 | return l.RangeUsing(HeapAllocator) |
| 43 | } |
| 44 | |
| 45 | func (l listUnstructured) RangeUsing(a Allocator) ListRange { |
| 46 | if len(l) == 0 { |
| 47 | return EmptyRange |
| 48 | } |
| 49 | r := a.allocListUnstructuredRange() |
| 50 | r.list = l |
| 51 | r.i = -1 |
| 52 | return r |
| 53 | } |
| 54 | |
| 55 | type listUnstructuredRange struct { |
| 56 | list listUnstructured |
| 57 | vv *valueUnstructured |
| 58 | i int |
| 59 | } |
| 60 | |
| 61 | func (r *listUnstructuredRange) Next() bool { |
| 62 | r.i += 1 |
| 63 | return r.i < len(r.list) |
| 64 | } |
| 65 | |
| 66 | func (r *listUnstructuredRange) Item() (index int, value Value) { |
| 67 | if r.i < 0 { |
| 68 | panic("Item() called before first calling Next()") |
| 69 | } |
| 70 | if r.i >= len(r.list) { |
| 71 | panic("Item() called on ListRange with no more items") |
| 72 | } |
| 73 | return r.i, r.vv.reuse(r.list[r.i]) |
| 74 | } |