listunstructured.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package value
  14. type listUnstructured []interface{}
  15. func (l listUnstructured) Length() int {
  16. return len(l)
  17. }
  18. func (l listUnstructured) At(i int) Value {
  19. return NewValueInterface(l[i])
  20. }
  21. func (l listUnstructured) AtUsing(a Allocator, i int) Value {
  22. return a.allocValueUnstructured().reuse(l[i])
  23. }
  24. func (l listUnstructured) Equals(other List) bool {
  25. return l.EqualsUsing(HeapAllocator, other)
  26. }
  27. func (l listUnstructured) EqualsUsing(a Allocator, other List) bool {
  28. return ListEqualsUsing(a, &l, other)
  29. }
  30. func (l listUnstructured) Range() ListRange {
  31. return l.RangeUsing(HeapAllocator)
  32. }
  33. func (l listUnstructured) RangeUsing(a Allocator) ListRange {
  34. if len(l) == 0 {
  35. return EmptyRange
  36. }
  37. r := a.allocListUnstructuredRange()
  38. r.list = l
  39. r.i = -1
  40. return r
  41. }
  42. type listUnstructuredRange struct {
  43. list listUnstructured
  44. vv *valueUnstructured
  45. i int
  46. }
  47. func (r *listUnstructuredRange) Next() bool {
  48. r.i += 1
  49. return r.i < len(r.list)
  50. }
  51. func (r *listUnstructuredRange) Item() (index int, value Value) {
  52. if r.i < 0 {
  53. panic("Item() called before first calling Next()")
  54. }
  55. if r.i >= len(r.list) {
  56. panic("Item() called on ListRange with no more items")
  57. }
  58. return r.i, r.vv.reuse(r.list[r.i])
  59. }